Here you can find the source of copyStream(final InputStream in)
public static InputStream copyStream(final InputStream in) throws IOException
//package com.java2s; //License from project: Apache License import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; public class Main { public static InputStream copyStream(final InputStream in) throws IOException { final byte[] bs = streamToByteArray(in); return new ByteArrayInputStream(bs); }/*from w w w . j a va 2 s .co m*/ public static byte[] streamToByteArray(final InputStream in) throws IOException { final ByteArrayOutputStream result = new ByteArrayOutputStream(); copy(in, result); result.close(); return result.toByteArray(); } private static void copy(final InputStream input, final OutputStream output) throws IOException { final ReadableByteChannel src = Channels.newChannel(input); final WritableByteChannel dest = Channels.newChannel(output); final 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); } } }