Here you can find the source of inputStreamToOutputStream(final InputStream in, final OutputStream out, int buffer)
InputStream
to an OutputStream
.
Parameter | Description |
---|---|
in | <code>InputStream</code> from which data is read. |
out | <code>OutputStream</code> to which data is written. |
buffer | the number of bytes to buffer reading. |
Parameter | Description |
---|---|
IOException | an exception |
public static long inputStreamToOutputStream(final InputStream in, final OutputStream out, int buffer) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2010-2014 Alexander Kerner. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License./* w w w .jav a 2s . c o m*/ ******************************************************************************/ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /** * The JCL book specifies the default buffer size as 8K characters. */ public static final int DEFAULT_BUFFER = 8192; /** * Copies the content of an <code>InputStream</code> to an * <code>OutputStream</code>. Will flush the <code>OutputStream</code>, but * won't close <code>InputStream</code> or <code>OutputStream</code>. * * @param in * <code>InputStream</code> from which data is read. * @param out * <code>OutputStream</code> to which data is written. * @return number of bytes read/written. * @throws IOException */ public static long inputStreamToOutputStream(final InputStream in, final OutputStream out) throws IOException { return inputStreamToOutputStream(in, out, DEFAULT_BUFFER); } /** * Copies the content of an <code>InputStream</code> to an * <code>OutputStream</code>. Will flush the <code>OutputStream</code>, but * won't close <code>InputStream</code> or <code>OutputStream</code>. * * @param in * <code>InputStream</code> from which data is read. * @param out * <code>OutputStream</code> to which data is written. * @param buffer * the number of bytes to buffer reading. * @return number of bytes read/written. * @throws IOException */ public static long inputStreamToOutputStream(final InputStream in, final OutputStream out, int buffer) throws IOException { if (buffer < 1) buffer = DEFAULT_BUFFER; final byte[] byteBuffer = new byte[buffer]; long count = 0; int n = 0; while ((n = in.read(byteBuffer)) != -1) { out.write(byteBuffer, 0, n); count += n; } out.flush(); return count; } }