Index: lucene/src/test/org/apache/lucene/store/TestByteArrayDataInput.java =================================================================== --- lucene/src/test/org/apache/lucene/store/TestByteArrayDataInput.java (revision 0) +++ lucene/src/test/org/apache/lucene/store/TestByteArrayDataInput.java (revision 0) @@ -0,0 +1,33 @@ +package org.apache.lucene.store; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.util.LuceneTestCase; + +public class TestByteArrayDataInput extends LuceneTestCase { + + public void testBasic() throws Exception { + byte[] bytes = new byte[] {1, 65}; + ByteArrayDataInput in = new ByteArrayDataInput(bytes); + assertEquals("A", in.readString()); + + bytes = new byte[] {1, 1, 65}; + in.reset(bytes, 1, 2); + assertEquals("A", in.readString()); + } +} Property changes on: lucene/src/test/org/apache/lucene/store/TestByteArrayDataInput.java ___________________________________________________________________ Added: svn:eol-style + native Index: lucene/src/java/org/apache/lucene/index/codecs/pulsing/PulsingPostingsReaderImpl.java =================================================================== --- lucene/src/java/org/apache/lucene/index/codecs/pulsing/PulsingPostingsReaderImpl.java (revision 1138085) +++ lucene/src/java/org/apache/lucene/index/codecs/pulsing/PulsingPostingsReaderImpl.java (working copy) @@ -110,7 +110,7 @@ final PulsingTermState termState = (PulsingTermState) _termState; if (termState.inlinedBytes == null) { termState.inlinedBytes = new byte[128]; - termState.inlinedBytesReader = new ByteArrayDataInput(null); + termState.inlinedBytesReader = new ByteArrayDataInput(); } int len = termsIn.readVInt(); if (termState.inlinedBytes.length < len) { @@ -222,7 +222,7 @@ } private static class PulsingDocsEnum extends DocsEnum { - private final ByteArrayDataInput postings = new ByteArrayDataInput(null); + private final ByteArrayDataInput postings = new ByteArrayDataInput(); private final boolean omitTF; private final boolean storePayloads; private Bits skipDocs; @@ -320,7 +320,7 @@ } private static class PulsingDocsAndPositionsEnum extends DocsAndPositionsEnum { - private final ByteArrayDataInput postings = new ByteArrayDataInput(null); + private final ByteArrayDataInput postings = new ByteArrayDataInput(); private final boolean storePayloads; private Bits skipDocs; Index: lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsReader.java =================================================================== --- lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsReader.java (revision 1138085) +++ lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsReader.java (working copy) @@ -156,7 +156,7 @@ //System.out.println("SPR.readTermsBlock termsIn.fp=" + termsIn.getFilePointer()); if (termState.bytes == null) { termState.bytes = new byte[ArrayUtil.oversize(len, 1)]; - termState.bytesReader = new ByteArrayDataInput(null); + termState.bytesReader = new ByteArrayDataInput(); } else if (termState.bytes.length < len) { termState.bytes = new byte[ArrayUtil.oversize(len, 1)]; } Index: lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.java =================================================================== --- lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.java (revision 1138085) +++ lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.java (working copy) @@ -533,7 +533,7 @@ private final static class FSTTermsEnum extends TermsEnum { private final FieldInfo field; private final BytesRefFSTEnum fstEnum; - private final ByteArrayDataInput buffer = new ByteArrayDataInput(null); + private final ByteArrayDataInput buffer = new ByteArrayDataInput(); private boolean didDecode; private int docFreq; Index: lucene/src/java/org/apache/lucene/index/codecs/BlockTermsReader.java =================================================================== --- lucene/src/java/org/apache/lucene/index/codecs/BlockTermsReader.java (revision 1138085) +++ lucene/src/java/org/apache/lucene/index/codecs/BlockTermsReader.java (working copy) @@ -312,13 +312,13 @@ private int blocksSinceSeek; private byte[] termSuffixes; - private ByteArrayDataInput termSuffixesReader = new ByteArrayDataInput(null); + private ByteArrayDataInput termSuffixesReader = new ByteArrayDataInput(); /* Common prefix used for all terms in this block. */ private int termBlockPrefix; private byte[] docFreqBytes; - private final ByteArrayDataInput freqReader = new ByteArrayDataInput(null); + private final ByteArrayDataInput freqReader = new ByteArrayDataInput(); private int metaDataUpto; public SegmentTermsEnum() throws IOException { Index: lucene/src/java/org/apache/lucene/store/ByteArrayDataInput.java =================================================================== --- lucene/src/java/org/apache/lucene/store/ByteArrayDataInput.java (revision 1138085) +++ lucene/src/java/org/apache/lucene/store/ByteArrayDataInput.java (working copy) @@ -17,6 +17,8 @@ * limitations under the License. */ +import org.apache.lucene.util.BytesRef; + /** @lucene.experimental */ public final class ByteArrayDataInput extends DataInput { @@ -25,11 +27,18 @@ private int pos; private int limit; - // TODO: allow BytesRef (slice) too public ByteArrayDataInput(byte[] bytes) { - this.bytes = bytes; + reset(bytes); } + public ByteArrayDataInput(byte[] bytes, int offset, int len) { + reset(bytes, offset, len); + } + + public ByteArrayDataInput() { + reset(BytesRef.EMPTY_BYTES); + } + public void reset(byte[] bytes) { reset(bytes, 0, bytes.length); } @@ -41,7 +50,7 @@ public void reset(byte[] bytes, int offset, int len) { this.bytes = bytes; pos = offset; - limit = len; + limit = offset + len; } public boolean eof() { @@ -59,12 +68,14 @@ @Override public int readInt() { + assert pos+4 <= limit; return ((bytes[pos++] & 0xFF) << 24) | ((bytes[pos++] & 0xFF) << 16) | ((bytes[pos++] & 0xFF) << 8) | (bytes[pos++] & 0xFF); } @Override public long readLong() { + assert pos+8 <= limit; final int i1 = ((bytes[pos++] & 0xff) << 24) | ((bytes[pos++] & 0xff) << 16) | ((bytes[pos++] & 0xff) << 8) | (bytes[pos++] & 0xff); final int i2 = ((bytes[pos++] & 0xff) << 24) | ((bytes[pos++] & 0xff) << 16) | @@ -74,9 +85,11 @@ @Override public int readVInt() { + checkBounds(); byte b = bytes[pos++]; int i = b & 0x7F; for (int shift = 7; (b & 0x80) != 0; shift += 7) { + checkBounds(); b = bytes[pos++]; i |= (b & 0x7F) << shift; } @@ -85,9 +98,11 @@ @Override public long readVLong() { + checkBounds(); byte b = bytes[pos++]; long i = b & 0x7F; for (int shift = 7; (b & 0x80) != 0; shift += 7) { + checkBounds(); b = bytes[pos++]; i |= (b & 0x7FL) << shift; } @@ -97,7 +112,7 @@ // NOTE: AIOOBE not EOF if you read too much @Override public byte readByte() { - assert pos < limit; + checkBounds(); return bytes[pos++]; } @@ -108,4 +123,9 @@ System.arraycopy(bytes, pos, b, offset, len); pos += len; } + + private boolean checkBounds() { + assert pos < limit; + return true; + } }