[jdk17] RFR: 6766844: ByteArrayInputStream#read with a byte array of length 0 not consistent with InputStream when at EOF [v2]

Brian Burkhalter bpb at openjdk.java.net
Fri Jul 2 17:42:51 UTC 2021


On Fri, 2 Jul 2021 17:27:34 GMT, Joe Wang <joehw at openjdk.org> wrote:

>> Brian Burkhalter has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   6766844: Correct error messages in test
>
> src/java.base/share/classes/java/io/ByteArrayInputStream.java line 161:
> 
>> 159:      * Unlike the {@link InputStream#read(byte[],int,int) overridden method}
>> 160:      * of {@code InputStream}, this method returns {@code -1} instead of zero
>> 161:      * if the end of the stream has been reached and {@code len == 0}.
> 
> The statement "return -1 if the end of the stream has been reached and len == 0" gives an impression that it requires both conditions to be met: end of the stream && len==0,  but the tests show -1 is expected if len == 0 without an attempt to read the stream. 
> 
> The overridden method stated that "If len is zero, then no bytes are read and 0 is returned", the above note looks like was meant for this statement since the overridden method also return -1 if the stream is at end of file.

Both conditions of the statement are intended to be met. If the stream is at its end then

        if (pos >= count) {
            return -1;
        }

will cause `-1` to be returned because at end of stream the condition `pos >= count` is met.

The overridden method always returns `0` if `len == 0`.:


    public int read(byte b[], int off, int len) throws IOException {
        Objects.checkFromIndexSize(off, len, b.length);
        if (len == 0) {
            return 0;
        }

-------------

PR: https://git.openjdk.java.net/jdk17/pull/189


More information about the core-libs-dev mailing list