Java InputStream to Byte Array getBytes(InputStream input)

Here you can find the source of getBytes(InputStream input)

Description

Drains the given InputStream of all data and returns a byte array which contains all of that data.

License

Open Source License

Parameter

Parameter Description
input The InputStream to drain of all data.

Exception

Parameter Description
IOException An error occurred reading from the given InputStream.

Return

The byte array containing all of the data.

Declaration

public static byte[] getBytes(InputStream input) throws IOException 

Method Source Code


//package com.java2s;
/* /*from   www.  ja v  a 2 s .c  o  m*/
 * NOTICE OF LICENSE
 * 
 * This source file is subject to the Open Software License (OSL 3.0) that is 
 * bundled with this package in the file LICENSE.txt. It is also available 
 * through the world-wide-web at http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to obtain it 
 * through the world-wide-web, please send an email to magnos.software@gmail.com 
 * so we can send you a copy immediately. If you use any of this software please
 * notify me via our website or email, your feedback is much appreciated. 
 * 
 * @copyright   Copyright (c) 2011 Magnos Software (http://www.magnos.org)
 * @license     http://opensource.org/licenses/osl-3.0.php
 *             Open Software License (OSL 3.0)
 */

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

public class Main {
    /**
     * Drains the given InputStream of all data and returns a byte array which
     * contains all of that data.
     * 
     * @param input
     *        The InputStream to drain of all data.
     * @return The byte array containing all of the data.
     * @throws IOException
     *         An error occurred reading from the given InputStream.
     */
    public static byte[] getBytes(InputStream input) throws IOException {
        return getOutput(input).toByteArray();
    }

    /**
     * Drains the given InputStream of all data and returns an OutputStream which
     * contains all of that data.
     * 
     * @param input
     *        The InputStream to drain of all data.
     * @return The OutputStream containing all of the data drained.
     * @throws IOException
     *         An error occurred reading from the given InputStream.
     */
    public static ByteArrayOutputStream getOutput(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream(input.available());

        int read = 0;
        byte[] data = new byte[4096];

        while ((read = input.read(data)) > 0) {
            output.write(data, 0, read);
        }
        output.flush();

        return output;
    }
}

Related

  1. getBytes(InputStream in)
  2. getBytes(InputStream in)
  3. getBytes(InputStream in)
  4. getBytes(InputStream in, int length, int BUF_SIZE)
  5. getBytes(InputStream in, OutputStream out)
  6. getBytes(InputStream input)
  7. getBytes(InputStream input)
  8. getBytes(InputStream input)
  9. getBytes(InputStream inputStream)