Skip to content

Commit 73c5c1e

Browse files
ByteArrayStreamInput: Return -1 when there are no more bytes to read (#112214)
1 parent 25fdcd2 commit 73c5c1e

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

docs/changelog/112214.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 112214
2+
summary: '`ByteArrayStreamInput:` Return -1 when there are no more bytes to read'
3+
area: Infra/Core
4+
type: bug
5+
issues: []

server/src/main/java/org/elasticsearch/common/io/stream/ByteArrayStreamInput.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ public void readBytes(byte[] b, int offset, int len) {
120120

121121
@Override
122122
public int read(byte[] b, int off, int len) throws IOException {
123-
int toRead = Math.min(len, available());
123+
final int available = limit - pos;
124+
if (available <= 0) {
125+
return -1;
126+
}
127+
int toRead = Math.min(len, available);
124128
readBytes(b, off, toRead);
125129
return toRead;
126130
}

server/src/test/java/org/elasticsearch/common/io/stream/AbstractStreamTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ public void testReadAfterReachingEndOfStream() throws IOException {
723723
input.readBytes(new byte[len], 0, len);
724724

725725
assertEquals(-1, input.read());
726+
assertEquals(-1, input.read(new byte[2], 0, 2));
726727
}
727728
}
728729

0 commit comments

Comments
 (0)