Core: Fix ByteBufferInputStream.read() to return -1 at EOF#16167
Core: Fix ByteBufferInputStream.read() to return -1 at EOF#16167sachinnn99 wants to merge 1 commit intoapache:mainfrom
Conversation
steveloughran
left a comment
There was a problem hiding this comment.
looks good for read(); looking at read(buffer[], offset, len), it seems ok too.
slice() probably needs attention at the same time
|
Thanks for the review! I looked at The fix here is scoped to the no-arg |
Baunsgaard
left a comment
There was a problem hiding this comment.
src LGTM.
But i would suggest a few additional test cases, just to ensure future coverage:
- invariants after the first -1 is encountered:
- an explicit empty case:
we can simply add a utility function to call with any stream to verify:
private static void assertAtEOF(ByteBufferInputStream stream) {
long pos = stream.getPos();
assertThat(stream.read()).as("read() at EOF").isEqualTo(-1);
assertThat(stream.read()).as("read() should keep returning -1 at EOF").isEqualTo(-1);
assertThat(stream.getPos()).as("Position should not advance past EOF").isEqualTo(pos);
assertThat(stream.available()).as("available() should be 0 at EOF").isEqualTo(0);
}
To make full coverage of all paths you can add an additional test that calls the assertAtEOF with empty streams that are empty from the beginning.
… throwing EOFException (apache#16127)
249ad40 to
9ea1f31
Compare
|
Thanks for the suggestions @Baunsgaard! Added in the latest push:
|
Fixes #16127.
SingleBufferInputStream.read()andMultiBufferInputStream.read()throwEOFExceptionwhen the stream is exhausted. This violates thejava.io.InputStreamcontract, which requires the no-argread()to return-1at EOF.The multi-byte
read(byte[], int, int)in both classes already correctly returns-1at EOF — the two overloads were inconsistent.Changes:
SingleBufferInputStream.read(): return-1instead of throwingEOFExceptionMultiBufferInputStream.read(): return-1at both EOF entry points instead of throwingEOFExceptiontestReadByte()to assert-1return (including idempotency check)Other
EOFException-throwing methods (slice(),sliceBuffers(),skipFully()) are unchanged — they request specific byte counts whereEOFExceptionis the correct signal.