Can using IOUtils.ToByteArray
leads to concurrency problems?
private byte[] saveLetterContent(InputStream input) {
...
byte[] letterContent = IOUtils.toByteArray(input);
...
}
I mean is it possible that letterContent
in this method change incorrectly because of concurrency?
Absolutely, calling
toByteArray(InputStream)
without guarding the inputtedInputStream
can lead to undefined behavior. It is also easily demonstrable.With the above example, you'll surely notice the second
System.out
will print a lower bytes length than the first one. You can even play withThread.sleep
and see what happens.