Java – How to Convert InputStream to Byte Array arraysinputstreamjava How do I read an entire InputStream into a byte array? Best Answer You can use Apache Commons IO to handle this and similar tasks. The IOUtils type has a static method to read an InputStream and return a byte[]. 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. Related SolutionsJava – How to Convert Byte Array to ByteArrayOutputStream byte[] bytes = ....; ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length); baos.write(bytes, 0, bytes.length); Method description: Writes len bytes from the specified byte array starting at offset off to this byte array output stream. Related Question
Best Answer
You can use Apache Commons IO to handle this and similar tasks.
The
IOUtils
type has a static method to read anInputStream
and return abyte[]
.Internally this creates a
ByteArrayOutputStream
and copies the bytes to the output, then callstoByteArray()
. It handles large files by copying the bytes in blocks of 4KiB.