diff --git a/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java b/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java index cb07b6e42ca..312d6383cd8 100644 --- a/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java +++ b/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java @@ -72,17 +72,24 @@ public class RAMInputStream extends IndexInput implements Cloneable { if (bufferPosition == bufferLength) { nextBuffer(); } - return currentBuffer[bufferPosition++]; + if (currentBuffer == null) { + throw new EOFException(); + } else { + return currentBuffer[bufferPosition++]; + } } @Override public void readBytes(byte[] b, int offset, int len) throws IOException { while (len > 0) { - if (bufferPosition == bufferLength) { nextBuffer(); } + if (currentBuffer == null) { + throw new EOFException(); + } + int remainInBuffer = bufferLength - bufferPosition; int bytesToCopy = len < remainInBuffer ? len : remainInBuffer; System.arraycopy(currentBuffer, bufferPosition, b, offset, bytesToCopy); diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java index e49000b5597..d9eeb88aea1 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java +++ b/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java @@ -522,6 +522,9 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase { // But any read following the seek(len) should throw an EOFException. expectThrows(EOFException.class, i::readByte); + expectThrows(EOFException.class, () -> { + i.readBytes(new byte [1], 0, 1); + }); i.close(); }