InputStream/OutputStream concurrency guarantees
Pavel Rappo
pavel.rappo at gmail.com
Thu Oct 30 17:30:15 UTC 2025
Replies inline.
On Thu, Oct 30, 2025 at 5:10 PM Florian Weimer <fweimer at redhat.com> wrote:
>
> * Viktor Klang:
>
> > Also, since there aren't any consistency effects in place, it is not
> > deterministic which changes made by the writer would be seen by the
> > reader/copier.
>
> Sure, but if a defensive copy were made, at least the output would be
> internally consistent. This would be similar to what various
> byte[]-accepting String methods already do.
Even with a private copy, there's no guarantee that the array will
remain unchanged while you are copying it. What is guaranteed is that
_after_ you have made that copy, no external code will be able to
change it.
You should protect your arrays from unintended modifications, but I'm
unaware of any good way to catch such modifications.
> <snip>
> FileInputStream and FileOutputStream could be changed in ways that make
> multiple byte array reads and writes observable through race conditions.
Could you clarify this by providing an example? What do you mean by
"FileInputStream and FileOutputStream could be changed"?
-Pavel
More information about the core-libs-dev
mailing list