Simple stream copy function
Simple function to copy a stream to another.
CopyStream.java
public static void copyStream(InputStream is, OutputStream os) throws IOException {
try (
ReadableByteChannel input = Channels.newChannel(is);
WritableByteChannel output = Channels.newChannel(os)
) {
final ByteBuffer buffer = ByteBuffer.allocateDirect(65536);
while (input.read(buffer) != -1) {
buffer.flip();
output.write(buffer);
buffer.compact();
}
buffer.flip();
while (buffer.hasRemaining())
output.write(buffer);
}
}