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 inputtedInputStreamcan lead to undefined behavior. It is also easily demonstrable.With the above example, you'll surely notice the second
System.outwill print a lower bytes length than the first one. You can even play withThread.sleepand see what happens.