You can use Apache Commons IO to handle this and similar tasks.
IOUtils type has a static method to read an
InputStream and return a
InputStream is; byte bytes = IOUtils.toByteArray(is);
Internally this creates a
ByteArrayOutputStream and copies the bytes to the output, then calls
toByteArray(). It handles large files by copying the bytes in blocks of 4KiB.