fanruan-tuco opened a new issue, #16127:
URL: https://github.com/apache/iceberg/issues/16127

   ## Summary
   
   `SingleBufferInputStream` and `MultiBufferInputStream` (the  concrete 
implementations of `ByteBufferInputStream`) throw `EOFException` from `read()` 
(the single-byte overload) when the stream is exhausted. This violates the 
`java.io.InputStream` contract, which requires `read()` to return `-1` at EOF.
   
   Interestingly, `read(byte[], int, int)` in both implementations correctly 
returns `-1` at EOF. The two overloads are inconsistent.
   
   ## Impact
   
   The mismatch creates a subtle trap when wrapping `ByteBufferInputStream`. If 
a subclass overrides only `read()` (e.g. delegating to a lazily-loaded 
`ByteBufferInputStream`), it inherits the `EOFException`-throwing behaviour. 
Any caller that then invokes the default `InputStream.read(byte[], off, len)` 
on that wrapper hits the following path in `InputStream`'s default 
implementation:
   
   ```java
   int c = read();   // first call — if at EOF, EOFException propagates here,
                     // outside the inner try-catch, and escapes to the caller
   b[off] = (byte) c;
   // ...
   ```
   
   The exception escapes uncaught because `InputStream.read(byte[], int, int)` 
only catches `IOException` **after** the first byte is read, not before. 
Callers that only guard against a `-1` return value will see an unexpected 
exception.
   
   ## Question
   
   Is this intentional? If `ByteBufferInputStream.read()` is *meant* to throw 
rather than return -1, is there a design rationale we should be aware of? If it 
is unintentional, we would be happy to contribute a fix that makes `read()` 
return `-1` consistently with `read(byte[], int, int)`.
   
   ## Affected classes
   
   - `org.apache.iceberg.io.SingleBufferInputStream`
   - `org.apache.iceberg.io.MultiBufferInputStream`
   
   ## Versions
   
   Observed in `iceberg-core 1.8.x`. The behaviour appears to have been present 
for a long time.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to