How do I read an entire InputStream
into a byte array?
You need to read each byte from your InputStream
and write it to a ByteArrayOutputStream
.
You can then retrieve the underlying byte array by calling toByteArray()
:
InputStream is = ...
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
return buffer.toByteArray();