Skip to content

Instantly share code, notes, and snippets.

@maikelsperandio
Created February 1, 2017 18:58
Show Gist options
  • Save maikelsperandio/1c7b05bef7241f99ad80051684023e9f to your computer and use it in GitHub Desktop.
Save maikelsperandio/1c7b05bef7241f99ad80051684023e9f to your computer and use it in GitHub Desktop.
package br.com.mcde.sistema.utils;
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;
/**
* Class that manages files using Java 8 NIO
* */
public class IOUtil {
public void copy(final InputStream src, final OutputStream dest) throws IOException {
final ReadableByteChannel inputChannel = Channels.newChannel(src);
final WritableByteChannel outputChannel = Channels.newChannel(dest);
copy(inputChannel, outputChannel);
}
public void copy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException {
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment