RFR 8193832: Performance of InputStream.readAllBytes() could be improved

Brian Burkhalter brian.burkhalter at oracle.com
Thu Dec 21 18:26:59 UTC 2017

On Dec 21, 2017, at 10:23 AM, Peter Levart <peter.levart at gmail.com> wrote:

>> Or I suppose a single List containing an object containing both the bytes and the length would work. One could for example us
> I don't think this would be necessary. All buffers but the last one are fully filled. The inner reading loop guarantees that the buffer is either fully read or the stream is at EOF. So in final gathering loop you could maintain a 'remaining' value, initialized to 'total' and decremented by DEFAULT_BUFFER_SIZE at each iteration. The number of bytes to copy for each buffer would then be Math.min(DEFAULT_BUFFER_SIZE, remaining). That's one possibility. There are others. But no new structures are necessary.

What about the case where read() returns 0, e.g., when reading from a socket, but subsequent reads return positive values?

            // read to EOF which may read more or less than buffer size
            while ((n = read(buf, nread, buf.length - nread)) > 0) {
                nread += n;

Then it does not look as if buffer is full or am I mistaken?



More information about the core-libs-dev mailing list