Index: src/test/org/apache/lucene/index/store/TestRAMDirectory.java =================================================================== --- src/test/org/apache/lucene/index/store/TestRAMDirectory.java (revision 631223) +++ src/test/org/apache/lucene/index/store/TestRAMDirectory.java (working copy) @@ -33,6 +33,8 @@ 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 @@ 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(); Index: src/java/org/apache/lucene/store/RAMInputStream.java =================================================================== --- src/java/org/apache/lucene/store/RAMInputStream.java (revision 631223) +++ src/java/org/apache/lucene/store/RAMInputStream.java (working copy) @@ -62,7 +62,7 @@ public byte readByte() throws IOException { if (bufferPosition >= bufferLength) { currentBufferIndex++; - switchCurrentBuffer(); + switchCurrentBuffer(true); } return currentBuffer[bufferPosition++]; } @@ -71,7 +71,7 @@ while (len > 0) { if (bufferPosition >= bufferLength) { currentBufferIndex++; - switchCurrentBuffer(); + switchCurrentBuffer(true); } int remainInBuffer = bufferLength - bufferPosition; @@ -83,10 +83,15 @@ } } - 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 { + currentBufferIndex--; + bufferPosition = BUFFER_SIZE; + } } else { currentBuffer = (byte[]) file.getBuffer(currentBufferIndex); bufferPosition = 0; @@ -103,7 +108,7 @@ 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); }