RFR: 8304745: Lazily initialize byte[] in java.io.BufferedInputStream [v2]
Chen Liang
liach at openjdk.org
Thu Mar 23 20:27:13 UTC 2023
On Thu, 23 Mar 2023 19:27:04 GMT, Sergey Tsypanov <stsypanov at openjdk.org> wrote:
>> By default `BufferedInputStream` is constructed with internal buffer with capacity 8192. In some cases this buffer is never used, e.g. when we call `IS.readNBytes()` or `IS.readAllBytes()` (relying on `BIS.read1()`) or when `BufferedInputStream` is cascaded.
>
> Sergey Tsypanov has updated the pull request incrementally with one additional commit since the last revision:
>
> Update src/java.base/share/classes/java/io/BufferedInputStream.java
>
> Co-authored-by: liach <7806504+liach at users.noreply.github.com>
So something like this?
byte[] buffer;
byte[] allocated;
do {
buffer = buf;
if (buffer == null) {
throw new IOException("Stream closed");
}
if (buffer == EMPTY) {
if (allocated == null) {
allocated = new byte[size];
}
// defend against asynchronous close
if (U.compareAndSetReference(this, BUF_OFFSET, EMPTY, allocated) {
return allocated;
} else {
continue;
}
}
return buffer;
}
-------------
PR Comment: https://git.openjdk.org/jdk/pull/13150#issuecomment-1481844595
More information about the core-libs-dev
mailing list