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[]getBytes(InputStream is)
get Bytes
try {
    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);
    buffer.flush();
...
byte[]getBytes(InputStream is)
get Bytes
byte[] data = null;
Collection chunks = new ArrayList();
byte[] buffer = new byte[1024 * 1000];
int read = -1;
int size = 0;
while ((read = is.read(buffer)) != -1) {
    if (read > 0) {
        byte[] chunk = new byte[read];
...
byte[]getBytes(InputStream is, int max_len)
get Bytes
ByteArrayOutputStream baos = new ByteArrayOutputStream(max_len);
for (int i = 0, b = is.read(); b >= 0 && i < max_len; b = is.read(), ++i)
    baos.write(b);
return baos.toByteArray();
byte[]getBytes(InputStream stream)
get Bytes
byte[] result = null;
if (stream != null) {
    try {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int read;
        byte[] data = new byte[16384];
        while ((read = stream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, read);
...
byte[]getBytes(InputStream stream)
Return the contents of an InputStream as a byte arrary
ByteArrayOutputStream baos = new ByteArrayOutputStream();
copy(stream, baos);
return baos.toByteArray();
byte[]getBytesFromInputStream(InputStream inputStream)
Extracts the bytes out of an InputStream.
int size;
final List<byte[]> data = new LinkedList<byte[]>();
final int bufferSize = 4096;
byte[] tmpByte = new byte[bufferSize];
int offset = 0;
int total = 0;
for (;;) {
    size = inputStream.read(tmpByte, offset, bufferSize - offset);
...
byte[]getBytesFromInputStream(InputStream inputStream)
get Bytes From Input Stream
try {
    byte[] buffer = new byte[8192];
    int bytesRead;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    return output.toByteArray();
...
byte[]getBytesFromInputStream(InputStream inputStream)
Get an array of bytes from an input stream.
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
    byte[] buffer = new byte[0xFFFF];
    for (int len; (len = inputStream.read(buffer)) != -1;)
        os.write(buffer, 0, len);
    os.flush();
    return os.toByteArray();
byte[]getBytesFromInputStream(InputStream is)
get Bytes From Input Stream
try (ByteArrayOutputStream os = new ByteArrayOutputStream();) {
    byte[] buffer = new byte[0xFFFF];
    for (int len; (len = is.read(buffer)) != -1;)
        os.write(buffer, 0, len);
    os.flush();
    return os.toByteArray();
byte[]getBytesFromInputStream(InputStream is)
get Bytes From Input Stream
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);
buffer.flush();
return buffer.toByteArray();
...