--- java/tests/src/org/apache/xindice/core/meta/inline/ResourceTypeReaderTest.java (revision 474103) +++ java/tests/src/org/apache/xindice/core/meta/inline/ResourceTypeReaderTest.java (working copy) @@ -18,9 +18,7 @@ package org.apache.xindice.core.meta.inline; -import org.apache.xindice.core.meta.inline.InlineMetaException; -import org.apache.xindice.core.meta.inline.InlineMetaMap; -import org.apache.xindice.core.meta.inline.ResourceTypeReader; +import org.apache.xindice.core.data.Value; import junit.framework.TestCase; @@ -43,14 +41,14 @@ InlineMetaMap map; - map = reader.read(binaryData, 2, 1); + map = reader.read(new Value(binaryData, 2, 1)); assertEquals(ResourceTypeReader.BINARY, map.get("type")); - map = reader.read(xmlData, 2, 1); + map = reader.read(new Value(xmlData, 2, 1)); assertEquals(ResourceTypeReader.XML, map.get("type")); try { - reader.read(evilData, 2, 1); + reader.read(new Value(evilData, 2, 1)); fail("failed to throw InlineMetaException on bad type value (3)"); } catch (InlineMetaException e) { // expected exception --- java/src/org/apache/xindice/core/meta/inline/InlineMetaService.java (revision 474103) +++ java/src/org/apache/xindice/core/meta/inline/InlineMetaService.java (working copy) @@ -22,7 +22,6 @@ import org.apache.commons.logging.LogFactory; import org.apache.xindice.core.FaultCodes; import org.apache.xindice.core.data.Value; -import org.apache.xindice.core.data.Record; /** * If the documents in a collection have headers, the Collection @@ -119,22 +118,23 @@ * the header is corrupted. */ public DatabaseEntry readDatabaseEntry(Value rawValue) throws InlineMetaException { - byte[] rawData = rawValue.getData(); if (log.isDebugEnabled()) { - log.debug("readDatabaseEntry: rawData: length=" + rawData.length + " byte 0: " + rawData[0] + " byte 1: " + rawData[1]); + log.debug("readDatabaseEntry: rawData: length=" + rawValue.getLength() + + " byte 0: " + rawValue.byteAt(0) + " byte 1: " + rawValue.byteAt(1)); } /* * Read the header. */ - int version = rawData[1]; + int headerLen = rawValue.byteAt(0); + int version = rawValue.byteAt(1); if (!haveReaderForVersion(version)) { throw new InlineMetaException(FaultCodes.GEN_CRITICAL_ERROR, "No inline metadata reader available for version " + version); } final InlineMetaReader reader = readerByVersion[version]; - InlineMetaMap map = reader.read(rawData, 2, rawData[0] - 2); + InlineMetaMap map = reader.read(rawValue.getSubvalue(2, headerLen - 2)); if (log.isDebugEnabled()) { log.debug("readDatabaseEntry: map: type=" + map.get("type")); } @@ -143,7 +143,7 @@ * Exract the data into a Value object. */ - Value value = new Value(rawData, rawData[0], rawData.length - rawData[0]); + Value value = rawValue.getSubvalue(headerLen, rawValue.getLength() - headerLen); // FIXME: May be Record should be used instead? new Record(null, value, map); return new DatabaseEntry(map, value); } --- java/src/org/apache/xindice/core/meta/inline/InlineMetaReader.java (revision 474103) +++ java/src/org/apache/xindice/core/meta/inline/InlineMetaReader.java (working copy) @@ -18,6 +18,8 @@ package org.apache.xindice.core.meta.inline; +import org.apache.xindice.core.data.Value; + /** * The Value associated with a Record * can be prefixed by a header containing 'inline' metadata. @@ -40,13 +42,10 @@ * When a header is read, it generates a Map * containing the attributes carried by the header. * - * @param data array in which the header is embedded - * @param offset from the beginning of the data array to the - * beginning of the header. - * @param length of the header data + * @param data Value object in which the header is embedded * @return Map containing the attributes read from the header * @throws InlineMetaException if the header data is corrupted or of * the wrong length */ - InlineMetaMap read(byte[] data, int offset, int length) throws InlineMetaException; + InlineMetaMap read(Value data) throws InlineMetaException; } --- java/src/org/apache/xindice/core/meta/inline/ResourceTypeReader.java (revision 474103) +++ java/src/org/apache/xindice/core/meta/inline/ResourceTypeReader.java (working copy) @@ -21,6 +21,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.xindice.core.FaultCodes; +import org.apache.xindice.core.data.Value; /** * @@ -41,26 +42,19 @@ } /** - * @see org.apache.xindice.core.meta.inline.InlineMetaReader#read(byte[], int, int) + * @see org.apache.xindice.core.meta.inline.InlineMetaReader#read(Value) */ - public InlineMetaMap read(byte[] data, int offset, int length) throws InlineMetaException { + public InlineMetaMap read(Value data) throws InlineMetaException { if (log.isDebugEnabled()) { - log.debug("ResourceTypeReader.read: data length=" + data.length + " offset=" + offset + " length=" + length); + log.debug("ResourceTypeReader.read: data length=" + data.getLength()); } - if (length != 1) { + if (data.getLength() != 1) { throw new InlineMetaException(FaultCodes.COL_DOCUMENT_MALFORMED, "Expecting header length of 1"); } Integer type; - try { - type = new Integer(data[offset]); - } catch (ArrayIndexOutOfBoundsException e) { - throw new InlineMetaException( - FaultCodes.COL_DOCUMENT_MALFORMED, - "Error reading from data (data length " + data.length + ", offset=" + offset + ", header length " + length + ")", - e); - } + type = new Integer(data.byteAt(0)); if (!XML.equals(type) && !BINARY.equals(type)) { throw new InlineMetaException(FaultCodes.COL_DOCUMENT_MALFORMED, "Unexpected type value: " + type); --- java/src/org/apache/xindice/core/meta/inline/NullReader.java (revision 474103) +++ java/src/org/apache/xindice/core/meta/inline/NullReader.java (working copy) @@ -19,6 +19,7 @@ package org.apache.xindice.core.meta.inline; import org.apache.xindice.core.FaultCodes; +import org.apache.xindice.core.data.Value; /** * Read metadata of length zero. Handy for comparing performance @@ -37,11 +38,11 @@ } /** - * @see org.apache.xindice.core.meta.inline.InlineMetaReader#read(byte[], int, int) + * @see org.apache.xindice.core.meta.inline.InlineMetaReader#read(Value) */ - public InlineMetaMap read(byte[] data, int offset, int length) throws InlineMetaException { + public InlineMetaMap read(Value data) throws InlineMetaException { - if (length != 0) { + if (data.getLength() != 0) { throw new InlineMetaException(FaultCodes.COL_DOCUMENT_MALFORMED, "Expecting header length of 0"); } --- java/src/org/apache/xindice/core/data/Value.java (revision 474103) +++ java/src/org/apache/xindice/core/data/Value.java (working copy) @@ -52,6 +52,9 @@ } public Value(byte[] data, int pos, int len) { + if (pos >= data.length || pos < 0 || pos + len > data.length) { + throw new ArrayIndexOutOfBoundsException("Value cannot be created"); + } this.data = data; this.pos = pos; this.len = len; @@ -82,6 +85,34 @@ } /** + * Returns the byte at the specified index. + * + * @param index byte index + * @return the byte at the specified index. + * @throws ArrayIndexOutOfBoundsException if index is negative number or + * is not less that the length of Value data + */ + public final byte byteAt(int index) { + if (index < 0 || index >= len) { + throw new ArrayIndexOutOfBoundsException(index); + } + return data[pos + index]; + } + + /** + * Get a new Value that is part of this Value object. + * + * @param start beginning index + * @param len length of the new Value + * @return Value object + * @throws ArrayIndexOutOfBoundsException if start index is either negative + * or isn't less then length of original Value + */ + public final Value getSubvalue(int start, int len) { + return new Value(data, start, len); + } + + /** * getLength retrieves the length of the data being stored by the Value. * * @return The Value length @@ -129,7 +160,7 @@ } public boolean equals(Value value) { - return len == value.len ? compareTo(value) == 0 : false; + return len == value.len && compareTo(value) == 0; } public boolean equals(Object obj) {