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[]getBytesFromStream(InputStream input)
Read a stream (usually small) completely in to a byte array.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int byteCount = 0;
byteCount = input.read(buf);
while (byteCount > 0) {
    baos.write(buf, 0, byteCount);
    byteCount = input.read(buf);
return baos.toByteArray();
byte[]getBytesFromStream(InputStream inputStream)
get Bytes From Stream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buf = new byte[BUFFER_SIZE];
int len;
while ((len = inputStream.read(buf)) > 0) {
    outputStream.write(buf, 0, len);
outputStream.close();
inputStream.close();
...
byte[]getBytesFromStream(InputStream is)
get Bytes From Stream
byte[] bytes = new byte[is.available()];
is.read(bytes);
return bytes;
byte[]getBytesFromStream(InputStream is)
Extracts a byte array from an InputStream
if (is == null) {
    return null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int b;
while ((b = is.read()) != -1) {
    bos.write(b);
return bos.toByteArray();
byte[]getBytesFromStream(InputStream is)
get Bytes From Stream
int BUFFER_SIZE = 8192;
byte[] buffer = new byte[BUFFER_SIZE];
ByteArrayOutputStream os = new ByteArrayOutputStream();
int numRead = 0;
while ((numRead = is.read(buffer)) > 0) {
    os.write(buffer, 0, numRead);
return os.toByteArray();
...
byte[]getBytesFromStream(InputStream is)
return byte array from reading entire inputstream given
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] byteBuf = new byte[1024 * 64];
while (true) {
    int bytesRead = bis.read(byteBuf);
    if (bytesRead <= 0)
        break;
    baos.write(byteBuf, 0, bytesRead);
...
byte[]getBytesFromStream(InputStream is)
get Bytes From Stream
InputStreamReader isr = new InputStreamReader(is);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
    int bytesRead = 0;
    int chunkSize = 10000000;
    byte[] chunk = new byte[chunkSize];
    while ((bytesRead = is.read(chunk)) > 0) {
        byte[] ba = new byte[bytesRead];
...
byte[]getBytesFromStream(InputStream is)
Convert the inputstream into a byte array.
return getBytesFromStream(is, 2048);
byte[]getInputStreamAsByteArray(InputStream stream, int length)
get Input Stream As Byte Array
byte[] contents;
if (length == -1) {
    contents = new byte[0];
    int contentsLength = 0;
    int bytesRead = -1;
    do {
        int available = stream.available();
        if (contentsLength + available > contents.length) {
...
byte[]getInputStreamAsByteArray(InputStream stream, int length)
Returns the given input stream as a byte array
byte[] contents;
if (length == -1) {
    contents = new byte[0];
    int contentsLength = 0;
    int amountRead = -1;
    do {
        int amountRequested = Math.max(stream.available(), DEFAULT_READING_SIZE);
        if (contentsLength + amountRequested > contents.length) {
...