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[]getStreamAsByteArray(InputStream input)
get Stream As Byte Array
ByteArrayOutputStream bos = null;
BufferedInputStream bis = null;
byte[] buffer = null;
try {
    bos = new ByteArrayOutputStream();
    bis = new BufferedInputStream(input);
    buffer = new byte[1024];
    int length = -1;
...
byte[]getStreamAsByteArray(InputStream stream)
Returns the contents of the input stream as byte array.
return getStreamAsByteArray(stream, -1);
byte[]getStreamAsBytes(final InputStream is)
Converst input stream into the byte array.
final BufferedInputStream bis = new BufferedInputStream(is);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final byte[] buffer = new byte[2048];
while (true) {
    final int n = bis.read(buffer);
    if (n == -1) {
        break;
    baos.write(buffer, 0, n);
bis.close();
baos.close();
return baos.toByteArray();
byte[]getStreamBytes(InputStream is)

Lee los bytes del fichero indicado.

final int BUF_SIZE = 512;
ByteArrayOutputStream bos = null;
int bytesRead = 0;
byte[] data = null;
data = new byte[BUF_SIZE];
bos = new ByteArrayOutputStream();
while ((bytesRead = is.read(data, 0, BUF_SIZE)) >= 0) {
    bos.write(data, 0, bytesRead);
...
byte[]getStreamContent(InputStream stream)
get Stream Content
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
    byte[] buffer = new byte[4096];
    int len = 0;
    while ((len = stream.read(buffer)) > 0) {
        output.write(buffer, 0, len);
} finally {
...
byte[]getStreamContentAsBytes(InputStream is)
Gets the stream content as bytes.
BufferedInputStream buffer = new BufferedInputStream(is);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] data = new byte[buffer.available()];
if (buffer.available() == 0) {
    return data;
int available = -1;
while ((available = buffer.read(data)) > -1) {
...
byte[]getStreamToBytes(InputStream stream)
get Stream To Bytes
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = stream.read(buffer)) != -1) {
    baos.write(buffer, 0, bytesRead);
return baos.toByteArray();
byte[]inputStream2ByteArray(InputStream input)
input Stream Byte Array
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
    output.write(buffer, 0, n);
return output.toByteArray();
ByteArrayOutputStreaminputStream2ByteArray(InputStream is)
input Stream Byte Array
int nRead;
byte[] data = new byte[16384];
ByteArrayOutputStream bs = new ByteArrayOutputStream();
while ((nRead = is.read(data, 0, data.length)) != -1) {
    bs.write(data, 0, nRead);
bs.flush();
return bs;
...
byte[]inputStreamAsBytes(InputStream stream)
Reads the entire input stream and returns it as an array of bytes.
if (stream == null)
    throw new IllegalArgumentException("Null stream was passed to inputStreamAsBytes");
ByteArrayOutputStream allBytes = new ByteArrayOutputStream(stream.available());
try {
    writeInputToOutput(stream, allBytes, CHUNK_SIZE);
} finally {
    allBytes.close();
return allBytes.toByteArray();