Here you can find the source of inputStreamToByteArray(InputStream in)
Parameter | Description |
---|---|
in | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static byte[] inputStreamToByteArray(InputStream in) throws IOException
//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(); } }