Java InputStream to Byte Array InputStreamToByteArray(InputStream inputStream)

Here you can find the source of InputStreamToByteArray(InputStream inputStream)

Description

Blocking read of an entire input stream into a byte array.

License

Open Source License

Parameter

Parameter Description
inputStream Input stream to be read.

Return

Byte array contents of an input stream.

Declaration

public static byte[] InputStreamToByteArray(InputStream inputStream) 

Method Source Code

//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class Main {
    /**/*  w w w.  j av  a  2  s .  c o  m*/
     * Blocking read of an entire input stream into a byte array.
     * @param inputStream Input stream to be read.
     * @return Byte array contents of an input stream.
     */
    public static byte[] InputStreamToByteArray(InputStream inputStream) {
        try {

            int i;
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            while ((i = inputStream.read()) != -1) {
                byteArrayOutputStream.write(i);
            }

            return byteArrayOutputStream.toByteArray();

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            try {
                inputStream.close();
            } catch (Exception exception) {
                exception.printStackTrace();
            }

        }
    }
}

Related

  1. inputStreamToByte(InputStream is)
  2. inputStreamToByteArray(final InputStream is, final int bufferSize)
  3. inputStreamToByteArray(InputStream in)
  4. inputStreamToByteArray(InputStream in)
  5. inputStreamToByteArray(InputStream input, int size)
  6. inputStreamToByteArray(InputStream ins)
  7. inputStreamToByteArray(InputStream is)
  8. inputStreamToByteArray(InputStream is)
  9. inputStreamToByteArray(InputStream is)