Java ByteArrayOutputStream Create getStreamData(InputStream is)

Here you can find the source of getStreamData(InputStream is)

Description

Read contents of stream into byte array.

License

Open Source License

Parameter

Parameter Description
is input stream to be read

Exception

Parameter Description
IOException on stream access error

Return

array of bytes containing all data from stream

Declaration

private static byte[] getStreamData(InputStream is) throws IOException 

Method Source Code

//package com.java2s;

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    private static final int COPY_BUFFER_SIZE = 1024;

    /**//from  w  ww .j  a v  a  2  s  .c  om
     * Read contents of stream into byte array.
     *
     * @param is input stream to be read
     * @return array of bytes containing all data from stream
     * @throws IOException on stream access error
     */
    private static byte[] getStreamData(InputStream is) throws IOException {
        byte[] buff = new byte[COPY_BUFFER_SIZE];
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        int count;
        while ((count = is.read(buff)) >= 0) {
            os.write(buff, 0, count);
        }
        return os.toByteArray();
    }
}

Related

  1. getStreamContents(InputStream is)