Guest User

Untitled

a guest
Aug 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. I/O Operation On Channels Using ByteBuffer - Performance Comparision
  2. FileChannel inChannel = new FileInputStream("input.txt").getChannel();
  3. FileChannel outChannel = new FileOutputStream("output.txt").getChannel();
  4. ByteBuffer buf = ByteBuffer.allocate(1024 * 1024);
  5.  
  6. while (inChannel.read(buf) != -1) {
  7. buf.flip();
  8. while (buf.hasRemaining()) {
  9. outChannel.write(buf);
  10. }
  11. buf.clear();
  12. }
  13.  
  14. while (inChannel.read(buf) != -1 || buf.position() > 0) {
  15. buf.flip();
  16. outChannel.write(buf);
  17. buf.compact();
  18. }
  19.  
  20. while (inChannel.read(buf) != -1) {
  21. buf.flip();
  22. outChannel.write(buf);
  23. buf.compact();
  24. }
  25. // final flush of pending output
  26. while (buf.hasRemaining())
  27. outChannel.write(buf);
Add Comment
Please, Sign In to add comment