Created
February 1, 2017 18:58
-
-
Save maikelsperandio/1c7b05bef7241f99ad80051684023e9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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