Java InputStream to Byte Array getStreamAsBytes(final InputStream is)

Here you can find the source of getStreamAsBytes(final InputStream is)

Description

Converst input stream into the byte array.

License

Apache License

Parameter

Parameter Description
is the input stream

Return

the byte array

Declaration

public static byte[] getStreamAsBytes(final InputStream is) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    /**/*from   ww w  . jav  a  2 s. c  o m*/
     * Converst input stream into the byte array.
     *
     * @param is the input stream
     * @return the byte array
     */
    public static byte[] getStreamAsBytes(final InputStream is) throws IOException {
        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();
    }
}

Related

  1. getBytesFromStream(InputStream is)
  2. getInputStreamAsByteArray(InputStream stream, int length)
  3. getInputStreamAsByteArray(InputStream stream, int length)
  4. getStreamAsByteArray(InputStream input)
  5. getStreamAsByteArray(InputStream stream)
  6. getStreamBytes(InputStream is)
  7. getStreamContent(InputStream stream)
  8. getStreamContentAsBytes(InputStream is)
  9. getStreamToBytes(InputStream stream)