Java Utililty Methods InputStream to Byte Array

List of utility methods to do InputStream to Byte Array

Description

The list of methods to do InputStream to Byte Array are organized into topic(s).

Method

byte[]inputStreamToArray(InputStream is)
Reads the full content of a stream and returns them in a byte array
byte b[] = new byte[8192];
ByteArrayOutputStream out = new ByteArrayOutputStream();
while (true) {
    int read = is.read(b);
    if (read < 1)
        break;
    out.write(b, 0, read);
out.close();
return out.toByteArray();
byte[]InputStreamTOByte(InputStream in)
Input Stream TO Byte
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER_SIZE];
int count = -1;
while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
    outStream.write(data, 0, count);
data = null;
return outStream.toByteArray();
byte[]inputStreamToByte(InputStream in)
input stream to byte
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER_SIZE];
int count = -1;
while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) {
    outStream.write(data, 0, count);
data = null;
return outStream.toByteArray();
...
byte[]inputStreamToByte(InputStream is)
input Stream To Byte
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1) {
    bytestream.write(ch);
byte data[] = bytestream.toByteArray();
bytestream.close();
return data;
...
byte[]inputStreamToByteArray(final InputStream is, final int bufferSize)
input Stream To Byte Array
final byte[] buffer = new byte[bufferSize];
final ByteArrayOutputStream os = new ByteArrayOutputStream();
int length = is.read(buffer);
while (length > 0) {
    os.write(buffer, 0, length);
    length = is.read(buffer);
return os.toByteArray();
...
byte[]inputStreamToByteArray(InputStream in)
Reads the contents of an InputStream into a byte[]
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = in.read(data, 0, data.length)) != -1) {
    buffer.write(data, 0, nRead);
buffer.flush();
return buffer.toByteArray();
...
byte[]inputStreamToByteArray(InputStream in)
input Stream To Byte Array
int len = 0;
int available = in.available();
int bufferSize = 1024;
if (available > bufferSize) {
    bufferSize = available;
ByteArrayOutputStream out = new ByteArrayOutputStream(bufferSize);
byte[] buffer = new byte[bufferSize];
...
byte[]inputStreamToByteArray(InputStream input, int size)
Converts input streams to byte arrays.
byte[] buffer = new byte[size];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
while ((bytesRead = input.read(buffer)) != -1) {
    output.write(buffer, 0, bytesRead);
return output.toByteArray();
byte[]InputStreamToByteArray(InputStream inputStream)
Blocking read of an entire input stream into a byte array.
try {
    int i;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    while ((i = inputStream.read()) != -1) {
        byteArrayOutputStream.write(i);
    return byteArrayOutputStream.toByteArray();
} catch (Exception e) {
...
byte[]inputStreamToByteArray(InputStream ins)
Returns the given java.io.InputStream as a byte array.
byte[] availableBytes = new byte[0];
try {
    byte[] buffer = new byte[4096];
    ByteArrayOutputStream outs = new ByteArrayOutputStream();
    int read = 0;
    while ((read = ins.read(buffer)) != -1) {
        outs.write(buffer, 0, read);
    ins.close();
    outs.close();
    availableBytes = outs.toByteArray();
} catch (Exception e) {
    e.printStackTrace();
return availableBytes;