LUCENE-1196: don't throw EOF when seeking to the end of RAMFile when file is multiple of 1024 in length

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@632120 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-02-28 21:13:59 +00:00
parent d7d1d680f2
commit 8f9781bbdb
2 changed files with 26 additions and 5 deletions

View File

@ -62,7 +62,7 @@ class RAMInputStream extends IndexInput implements Cloneable {
public byte readByte() throws IOException { public byte readByte() throws IOException {
if (bufferPosition >= bufferLength) { if (bufferPosition >= bufferLength) {
currentBufferIndex++; currentBufferIndex++;
switchCurrentBuffer(); switchCurrentBuffer(true);
} }
return currentBuffer[bufferPosition++]; return currentBuffer[bufferPosition++];
} }
@ -71,7 +71,7 @@ class RAMInputStream extends IndexInput implements Cloneable {
while (len > 0) { while (len > 0) {
if (bufferPosition >= bufferLength) { if (bufferPosition >= bufferLength) {
currentBufferIndex++; currentBufferIndex++;
switchCurrentBuffer(); switchCurrentBuffer(true);
} }
int remainInBuffer = bufferLength - bufferPosition; 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()) { if (currentBufferIndex >= file.numBuffers()) {
// end of file reached, no more buffers left // end of file reached, no more buffers left
if (enforceEOF)
throw new IOException("Read past EOF"); throw new IOException("Read past EOF");
else {
// Force EOF if a read takes place at this position
currentBufferIndex--;
bufferPosition = BUFFER_SIZE;
}
} else { } else {
currentBuffer = (byte[]) file.getBuffer(currentBufferIndex); currentBuffer = (byte[]) file.getBuffer(currentBufferIndex);
bufferPosition = 0; bufferPosition = 0;
@ -103,7 +109,7 @@ class RAMInputStream extends IndexInput implements Cloneable {
public void seek(long pos) throws IOException { public void seek(long pos) throws IOException {
if (currentBuffer==null || pos < bufferStart || pos >= bufferStart + BUFFER_SIZE) { if (currentBuffer==null || pos < bufferStart || pos >= bufferStart + BUFFER_SIZE) {
currentBufferIndex = (int) (pos / BUFFER_SIZE); currentBufferIndex = (int) (pos / BUFFER_SIZE);
switchCurrentBuffer(); switchCurrentBuffer(false);
} }
bufferPosition = (int) (pos % BUFFER_SIZE); bufferPosition = (int) (pos % BUFFER_SIZE);
} }

View File

@ -33,6 +33,8 @@ import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory; 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.FSDirectory;
import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.English; import org.apache.lucene.util.English;
@ -215,6 +217,19 @@ public class TestRAMDirectory extends LuceneTestCase {
} }
} }
// 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) { private void rmDir(File dir) {
File[] files = dir.listFiles(); File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) { for (int i = 0; i < files.length; i++) {