Java InputStream to Byte Array inputStreamAsBytes(InputStream stream)

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

Description

Reads the entire input stream and returns it as an array of bytes.

License

Open Source License

Parameter

Parameter Description
stream The stream to read from

Exception

Parameter Description

Return

The read bytes

Declaration

static public byte[] inputStreamAsBytes(InputStream stream) throws IOException 

Method Source Code


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

import java.io.*;

public class Main {
    /** The default size to use for buffers when reading from InputStreams - 8192 bytes. */
    public static final int CHUNK_SIZE = 8192;

    /**//from  ww w . jav  a2 s .co m
     * Reads the entire input stream and returns it as an array of bytes. It does NOT close the input stream.
     * @param stream The stream to read from
     * @return The read bytes
     * @throws java.io.IOException
     */
    static public byte[] inputStreamAsBytes(InputStream stream) throws IOException {
        if (stream == null)
            throw new IllegalArgumentException("Null stream was passed to inputStreamAsBytes");
        ByteArrayOutputStream allBytes = new ByteArrayOutputStream(stream.available());
        try {
            writeInputToOutput(stream, allBytes, CHUNK_SIZE);
        } finally {
            allBytes.close();
        }
        return allBytes.toByteArray();
    }

    /** Reads bytes from an InputStream and transfers them to an OutputStream.
     * It will block and keep reading until the InputStream is exhausted and returns a -1 on read().
     * It will flush() the OutputStream, but it does NOT close the InputStream or OutputStream.
     * @param bufferSize The size of the chunks that will be used for the transfer. Large buffers take more memory, but are generally more efficient. Use IOUtils.CHUNK_SIZE when in doubt.
     * @param in The stream to read from.
     * @param out The stream to write to.
     * @return The actual number of bytes transferred.
     * @throws java.io.IOException
     */
    static public long writeInputToOutput(InputStream in, OutputStream out, int bufferSize) throws IOException {
        if (in == null)
            throw new IllegalArgumentException("You cannot pass a null InputStream");
        if (out == null)
            throw new IllegalArgumentException("You cannot pass a null OutputStream");
        byte[] buffer = new byte[bufferSize];
        int lastBytesRead;
        long totalBytesRead = 0;
        while ((lastBytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, lastBytesRead);
            totalBytesRead += lastBytesRead;
        }
        out.flush();
        return totalBytesRead;
    }

    /**
     * Reads an InputStream and writes it to an OutputStream. Stops after encountering stopSequence. This reads and writes a single byte at a time, so it is a good idea for performance purposes to use buffered streams.
     * @param in The InputStream to read. Must be non-null.
     * @param out the OutputStream to write to. If null, then the input will still be scanned, but nothing will be written.
     * @param stopSequence Reading will stop after encountering this byte sequence.
     * @return The total number of bytes read.
     * @throws IOException
     */
    static public long writeInputToOutput(InputStream in, OutputStream out, byte[] stopSequence)
            throws IOException {
        int result = 0;
        int sequenceMark = 0;
        int sequenceLength = stopSequence.length;
        int eachByte;
        while ((sequenceMark < sequenceLength) && (result < 1600)) { //FIX! Temporary stopgap to prevent crash; stop after reading 1600 bytes
            eachByte = in.read();
            if (eachByte == -1)
                break;
            //System.out.print( (char)eachByte );
            out.write(eachByte);
            if (eachByte == stopSequence[sequenceMark])
                sequenceMark++;
            else
                sequenceMark = 0;
            result++;
        }
        return result;
    }
}

Related

  1. getStreamContent(InputStream stream)
  2. getStreamContentAsBytes(InputStream is)
  3. getStreamToBytes(InputStream stream)
  4. inputStream2ByteArray(InputStream input)
  5. inputStream2ByteArray(InputStream is)
  6. inputStreamToArray(InputStream is)
  7. InputStreamTOByte(InputStream in)
  8. inputStreamToByte(InputStream in)
  9. inputStreamToByte(InputStream is)