Java InputStream to Byte Array inputStreamToByteArray(InputStream in)

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

Description

input Stream To Byte Array

License

Open Source License

Parameter

Parameter Description
in a parameter

Exception

Parameter Description
IOException an exception

Declaration

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

Method Source Code

//package com.java2s;
/**/*from   w  w  w  .j  av a 2s.c  om*/
 * Copyright (c) 2009-2011 SKRATCHDOT.COM
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Initial modeling finished using information provided by:
 *    http://www.sonicspot.com/guide/wavefiles.html
 * 
 * Contributors:
 *     JEFF |:at:| SKRATCHDOT |:dot:| COM
 *
 * $Id$
 */

import java.io.ByteArrayOutputStream;

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

public class Main {
    /**
     * @param in
     * @return
     * @throws IOException
     */
    public static byte[] inputStreamToByteArray(InputStream in) throws IOException {
        int len = 0;
        int available = in.available();
        int bufferSize = 1024;

        // Set bigger bufferSize
        if (available > bufferSize) {
            bufferSize = available;
        }

        // Allocate buffers
        ByteArrayOutputStream out = new ByteArrayOutputStream(bufferSize);
        byte[] buffer = new byte[bufferSize];

        // Read in data
        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
        }

        // Close streams
        in.close();
        out.close();

        return out.toByteArray();
    }
}

Related

  1. inputStreamToByte(InputStream in)
  2. InputStreamTOByte(InputStream in)
  3. inputStreamToByte(InputStream is)
  4. inputStreamToByteArray(final InputStream is, final int bufferSize)
  5. inputStreamToByteArray(InputStream in)
  6. inputStreamToByteArray(InputStream input, int size)
  7. InputStreamToByteArray(InputStream inputStream)
  8. inputStreamToByteArray(InputStream ins)
  9. inputStreamToByteArray(InputStream is)