Apache Solr open-source search software
LUCENE-1196: don't throw EOF when seeking to the end of RAMFile when file is multiple of 1024 in length
Michael McCandless8f9781bbdb6d
diff --git a/src/java/org/apache/lucene/store/RAMInputStream.java b/src/java/org/apache/lucene/store/RAMInputStream.javaindex c2a3a72ce3d..ba2afd13caa 100644--- a/src/java/org/apache/lucene/store/RAMInputStream.java+++ b/src/java/org/apache/lucene/store/RAMInputStream.java@@ -62,7 +62,7 @@ class RAMInputStream extends IndexInput implements Cloneable {public byte readByte() throws IOException {if (bufferPosition >= bufferLength) {currentBufferIndex++;- switchCurrentBuffer();+ switchCurrentBuffer(true);}return currentBuffer[bufferPosition++];}@@ -71,7 +71,7 @@ class RAMInputStream extends IndexInput implements Cloneable {while (len > 0) {if (bufferPosition >= bufferLength) {currentBufferIndex++;- switchCurrentBuffer();+ switchCurrentBuffer(true);}int remainInBuffer = bufferLength - bufferPosition;@@ -83,10 +83,16 @@ class RAMInputStream extends IndexInput implements Cloneable {}}- private final void switchCurrentBuffer() throws IOException {+ private final void switchCurrentBuffer(boolean enforceEOF) throws IOException {if (currentBufferIndex >= file.numBuffers()) {// end of file reached, no more buffers left- throw new IOException("Read past EOF");+ if (enforceEOF)+ throw new IOException("Read past EOF");+ else {+ // Force EOF if a read takes place at this position+ currentBufferIndex--;+ bufferPosition = BUFFER_SIZE;+ }} else {currentBuffer = (byte[]) file.getBuffer(currentBufferIndex);bufferPosition = 0;@@ -103,7 +109,7 @@ class RAMInputStream extends IndexInput implements Cloneable {public void seek(long pos) throws IOException {if (currentBuffer==null || pos < bufferStart || pos >= bufferStart + BUFFER_SIZE) {currentBufferIndex = (int) (pos / BUFFER_SIZE);- switchCurrentBuffer();+ switchCurrentBuffer(false);}bufferPosition = (int) (pos % BUFFER_SIZE);}diff --git a/src/test/org/apache/lucene/index/store/TestRAMDirectory.java b/src/test/org/apache/lucene/index/store/TestRAMDirectory.javaindex 5e80e39c356..cb767dbd25c 100644--- a/src/test/org/apache/lucene/index/store/TestRAMDirectory.java+++ b/src/test/org/apache/lucene/index/store/TestRAMDirectory.java@@ -33,6 +33,8 @@ import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.store.Directory;+import org.apache.lucene.store.IndexInput;+import org.apache.lucene.store.IndexOutput;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.store.RAMDirectory;import org.apache.lucene.util.English;@@ -214,6 +216,19 @@ public class TestRAMDirectory extends LuceneTestCase {rmDir (indexDir);}}++ // LUCENE-1196+ public void testIllegalEOF() throws Exception {+ RAMDirectory dir = new RAMDirectory();+ IndexOutput o = dir.createOutput("out");+ byte[] b = new byte[1024];+ o.writeBytes(b, 0, 1024);+ o.close();+ IndexInput i = dir.openInput("out");+ i.seek(1024);+ i.close();+ dir.close();+ }private void rmDir(File dir) {File[] files = dir.listFiles();