Here you can find the source of copyStream(InputStream in, OutputStream out)
public static void copyStream(InputStream in, OutputStream out) throws IOException
//package com.java2s; /* /*from ww w . j a v a 2s . c o m*/ * The Fascinator - Solr Portal * Copyright (C) 2008 University of Southern Queensland * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; public class Main { public static void copyStream(InputStream in, OutputStream out) throws IOException { ReadableByteChannel src = Channels.newChannel(in); WritableByteChannel dest = Channels.newChannel(out); ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); while (src.read(buffer) != -1) { buffer.flip(); dest.write(buffer); buffer.compact(); } buffer.flip(); while (buffer.hasRemaining()) { dest.write(buffer); } } public static void copyStream(Reader in, Writer out) throws IOException { char[] buffer = new char[4096]; int n = 0; while (-1 != (n = in.read(buffer))) { out.write(buffer, 0, n); } } }