Java InputStream to Byte Array inputStreamToByteArray(InputStream is)

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

Description

Generates byte array with contents of a stream

License

Open Source License

Parameter

Parameter Description
is input stream for converting

Exception

Parameter Description
IOException If the first byte cannot be read for any reasonother than end of file, or if the input stream has been closed, or ifsome other I/O error occurs.

Return

byte array with contents of a stream

Declaration

public static byte[] inputStreamToByteArray(InputStream is)
        throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/*from  w  w w  .ja va  2 s .  c om*/
     * Generates byte array with contents of a stream
     *
     * @param is input stream for converting
     * @return byte array with contents of a stream
     * @throws IOException If the first byte cannot be read for any reason
     *                     other than end of file, or if the input stream has been closed, or if
     *                     some other I/O error occurs.
     */
    public static byte[] inputStreamToByteArray(InputStream is)
            throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int length;
        while ((length = is.read(bytes)) != -1) {
            baos.write(bytes, 0, length);
        }
        return baos.toByteArray();
    }
}

Related

  1. InputStreamToByteArray(InputStream inputStream)
  2. inputStreamToByteArray(InputStream ins)
  3. inputStreamToByteArray(InputStream is)
  4. inputStreamToByteArray(InputStream is)
  5. inputStreamToByteArray(InputStream is)
  6. inputStreamToByteArray(InputStream is)
  7. inputStreamToByteArray(InputStream stream)
  8. inputStreamToByteArrayOutputStream(final InputStream is)
  9. inputStreamToBytes(final InputStream aInputStream)