There are many places involved in the problem, and a good fix might be problematic performance wise.
IOUtils.toByteArray(InputStream input) invoked with a Stream which feeds more than Integer.MAX_VALUE bytes will either crash with NegativeArraySizeException or maybe worse overflow in such a way that it returns fine (but only with partial data)
The ByteArrayOutputStream will happily consume the full stream but "int count" will overflow. At some point then toByteArray is invoked which will do like new byte[count].
maybe "needNewBuffer" can throw the IllegalArgumentException, as it gets the count and could check for the overflow.