InputStream/OutputStream concurrency guarantees
Pavel Rappo
pavel.rappo at gmail.com
Mon Oct 27 11:47:20 UTC 2025
The specification of the read/write methods, their classes, and the
package is silent on multithreading. In which case you should always
assume that the methods are **not** thread-safe.
On Mon, Oct 27, 2025 at 7:51 AM Florian Weimer <fweimer at redhat.com> wrote:
>
> I assume that the buffer-based read() methods of InputStream are allowed
> to write to the byte array multiple times (within the byte range that is
> specified by the caller and indicated by the method result).
>
The default implementation of read(byte[] b, int off, int len) writes
into the array **repeatedly**, until all len bytes are written, EOF
occurs, or an exception is thrown.
> Similarly, the buffer-based write() methods of OutputStream are not
> required to behave correctly if the byte array is concurrently modified.
> They may produce a corrupted output stream, for example because a
> checksum does not match the data that has been written.
Correct. Intuitions are as follows: It's only synchronization or copy.
Neither is specified. Furthermore, array-copying would defeat the
purpose for which the method was introduced.
> Are these assumptions correct?
Your assumptions are correct.
-Pavel
More information about the core-libs-dev
mailing list