Apache Lucene open-source search software
LUCENE-3224: fix silly bugs; add assertions in ByteArrayDataInput
Michael McCandless6b41b73894d3
diff --git a/lucene/src/java/org/apache/lucene/index/codecs/BlockTermsReader.java b/lucene/src/java/org/apache/lucene/index/codecs/BlockTermsReader.javaindex 9d045ec7c5e..dec87dcda38 100644--- a/lucene/src/java/org/apache/lucene/index/codecs/BlockTermsReader.java+++ b/lucene/src/java/org/apache/lucene/index/codecs/BlockTermsReader.java@@ -312,13 +312,13 @@ public class BlockTermsReader extends FieldsProducer {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 {diff --git a/lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.java b/lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.javaindex f753066d33d..6034c443e84 100644--- a/lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.java+++ b/lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.java@@ -533,7 +533,7 @@ public class MemoryCodec extends Codec {private final static class FSTTermsEnum extends TermsEnum {private final FieldInfo field;private final BytesRefFSTEnum<BytesRef> fstEnum;- private final ByteArrayDataInput buffer = new ByteArrayDataInput(null);+ private final ByteArrayDataInput buffer = new ByteArrayDataInput();private boolean didDecode;private int docFreq;diff --git a/lucene/src/java/org/apache/lucene/index/codecs/pulsing/PulsingPostingsReaderImpl.java b/lucene/src/java/org/apache/lucene/index/codecs/pulsing/PulsingPostingsReaderImpl.javaindex 9acb75e1d85..eeb2192ed94 100644--- a/lucene/src/java/org/apache/lucene/index/codecs/pulsing/PulsingPostingsReaderImpl.java+++ b/lucene/src/java/org/apache/lucene/index/codecs/pulsing/PulsingPostingsReaderImpl.java@@ -110,7 +110,7 @@ public class PulsingPostingsReaderImpl extends PostingsReaderBase {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 @@ public class PulsingPostingsReaderImpl extends PostingsReaderBase {}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 @@ public class PulsingPostingsReaderImpl extends PostingsReaderBase {}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;diff --git a/lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsReader.java b/lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsReader.javaindex 3ef44dd33c0..52717615b22 100644--- a/lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsReader.java+++ b/lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsReader.java@@ -156,7 +156,7 @@ public class StandardPostingsReader extends PostingsReaderBase {//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)];}diff --git a/lucene/src/java/org/apache/lucene/store/ByteArrayDataInput.java b/lucene/src/java/org/apache/lucene/store/ByteArrayDataInput.javaindex dee9c5de073..9caed4ab899 100644--- a/lucene/src/java/org/apache/lucene/store/ByteArrayDataInput.java+++ b/lucene/src/java/org/apache/lucene/store/ByteArrayDataInput.java@@ -17,6 +17,8 @@ package org.apache.lucene.store;* limitations under the License.*/+import org.apache.lucene.util.BytesRef;+/** @lucene.experimental */public final class ByteArrayDataInput extends DataInput {@@ -25,9 +27,16 @@ public final class ByteArrayDataInput extends DataInput {private int pos;private int limit;- // TODO: allow BytesRef (slice) toopublic 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) {@@ -41,7 +50,7 @@ public final class ByteArrayDataInput extends DataInput {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 @@ public final class ByteArrayDataInput extends DataInput {@Overridepublic int readInt() {+ assert pos+4 <= limit;return ((bytes[pos++] & 0xFF) << 24) | ((bytes[pos++] & 0xFF) << 16)| ((bytes[pos++] & 0xFF) << 8) | (bytes[pos++] & 0xFF);}@Overridepublic 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 @@ public final class ByteArrayDataInput extends DataInput {@Overridepublic 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 @@ public final class ByteArrayDataInput extends DataInput {@Overridepublic 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 @@ public final class ByteArrayDataInput extends DataInput {// NOTE: AIOOBE not EOF if you read too much@Overridepublic byte readByte() {- assert pos < limit;+ checkBounds();return bytes[pos++];}@@ -108,4 +123,9 @@ public final class ByteArrayDataInput extends DataInput {System.arraycopy(bytes, pos, b, offset, len);pos += len;}++ private boolean checkBounds() {+ assert pos < limit;+ return true;+ }}diff --git a/lucene/src/test/org/apache/lucene/store/TestByteArrayDataInput.java b/lucene/src/test/org/apache/lucene/store/TestByteArrayDataInput.javanew file mode 100644index 00000000000..fb14c4552cc--- /dev/null+++ b/lucene/src/test/org/apache/lucene/store/TestByteArrayDataInput.java@@ -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());+ }+}