Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I/O Operation On Channels Using ByteBuffer - Performance Comparision
- FileChannel inChannel = new FileInputStream("input.txt").getChannel();
- FileChannel outChannel = new FileOutputStream("output.txt").getChannel();
- ByteBuffer buf = ByteBuffer.allocate(1024 * 1024);
- while (inChannel.read(buf) != -1) {
- buf.flip();
- while (buf.hasRemaining()) {
- outChannel.write(buf);
- }
- buf.clear();
- }
- while (inChannel.read(buf) != -1 || buf.position() > 0) {
- buf.flip();
- outChannel.write(buf);
- buf.compact();
- }
- while (inChannel.read(buf) != -1) {
- buf.flip();
- outChannel.write(buf);
- buf.compact();
- }
- // final flush of pending output
- while (buf.hasRemaining())
- outChannel.write(buf);
Add Comment
Please, Sign In to add comment