Java ByteArrayOutputStream Write readAll(InputStream stream)

Here you can find the source of readAll(InputStream stream)

Description

Reads all the available bytes in the provided stream to a byte array.

License

Open Source License

Parameter

Parameter Description
stream the stream to read

Exception

Parameter Description
IOException an exception

Return

the read bytes

Declaration

public static byte[] readAll(InputStream stream) 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  ww.  j ava  2 s .  c  o m*/
     * Reads all the available bytes in the provided stream to a byte array.
     * <p>
     * The implementation of this method will cause a temporary memory use
     * of up to the equivalent of three times the size of the resulting array.
     * @param stream the stream to read
     * @return the read bytes
     * @throws IOException
     */
    public static byte[] readAll(InputStream stream) throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[100];
        int len = 0;
        while ((len = stream.read(buffer)) != -1)
            outStream.write(buffer, 0, len);
        return outStream.toByteArray();
    }
}

Related

  1. readAll(InputStream in)
  2. readAll(InputStream in)
  3. readAll(InputStream inputStream)
  4. readAll(InputStream is)
  5. readAll(InputStream s)
  6. readAllAndClose(InputStream is)
  7. readAllAndKeepOpen(InputStream bytes, int bufferSize)
  8. readAllBinary(InputStream stream)
  9. readAllFrom(InputStream in, boolean chunked)