NIO and NIO2


Published on

Whats new in NIO and NIO2

Published in: Technology, Education
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

NIO and NIO2

  1. 1. What's New in NIO 2<br />Ranjith Kumar N<br />JUG-Chennai <br />9th June 2011<br />
  2. 2. Agenda:<br />NIO <br /><ul><li>Buffers
  3. 3. Channels
  4. 4. Selectors</li></ul>NIO 2.0 <br /><ul><li>Update
  5. 5. New File System API
  6. 6. Asynchronous I/O</li></li></ul><li>Why NIO?<br />JSR - 51<br />
  7. 7. NIO Features<br />Channel and Buffers<br />File locking<br />Memory Mapped Files<br />Scatter and Gather<br />Channel to Channel Transfer<br />Non-Blocking Sockets<br />Multiplexed I/O<br />
  8. 8. Buffersjava.nio<br />
  9. 9. Buffer's Basic Attributes:<br />Capacity<br />Limit<br />Position <br />Mark<br />
  10. 10. Buffer Basic Operations:<br />Creating<br />Filling and Draining<br />Flipping and Rewind<br />Marking<br />Comparing<br />Duplicating<br />
  11. 11. Creating<br />public static XXXBuffer allocate (int capacity)<br />public static XXXBuffer wrap (XXX [] array)<br />public static XXXBuffer wrap (XXX [] array, int offset,int length)<br />For e.g.<br />CharBuffer charBuffer= CharBuffer.allocate(10);<br />char [] charArray = new char [10];<br />CharBuffercharbuffer = CharBuffer.wrap (charArray );<br />CharBuffercharbuffer = CharBuffer.wrap (charArray, 2, 7);<br />
  12. 12. Filling and Draining<br />XXXBuffer put (XXX b);<br />XXXBuffer put (int index, XXX b);<br />XXX get( );<br />XXX get(int index);<br />XXXBufferput (XXX[] src);<br />XXXBuffer put(XXX [] src, int offset, int length);<br />XXXBuffer get(XXX[] dest);<br />XXXBuffer get(XXX [] dest, int offset, int length);<br />
  13. 13. Flipping and Rewind<br />Manually Flipping a Buffer:<br />buffer.limit(buffer.position( )).position(0)<br />API provides Flip and rewind method:<br />Buffer flip()<br />Buffer rewind()<br />
  14. 14. Marking<br />Buffer mark()<br />Buffer reset()<br />For e.g<br />buffer.position(2).mark( ).position(4);<br />
  15. 15. Marking - cont.<br />After calling reset method<br />
  16. 16. Comparing<br />boolean equals (Object ob)<br />int compareTo (Object ob)<br />Two buffers are considered to be equal if and only if:<br /><ul><li>Both objects are the same type
  17. 17. Both buffers have the same number of remaining elements.
  18. 18. The sequence of remaining data elements, which would be returned from get( ), must be identical in each buffer.</li></li></ul><li>Comparing – cont. <br />Two buffers considered to be equal<br />
  19. 19. Comparing – cont.<br />Two buffers considered to be unequal<br />
  20. 20. Duplicating<br />CharBuffer duplicate( );<br />CharBuffer asReadOnlyBuffer( );<br />CharBuffer slice( );<br />For e.g<br />CharBuffer buffer = CharBuffer.allocate (8);<br />buffer.position(3).limit (6).mark( ).position (5);<br /> CharBuffer dupeBuffer = buffer.duplicate( );<br />buffer.clear( );<br />
  21. 21. Duplicating – cont.<br />
  22. 22. Duplicating – cont.<br />e.g. <br />CharBuffer buffer = CharBuffer.allocate (8);<br />buffer.position(3).limit (5);<br /> CharBuffer sliceBuffer = buffer.slice( );<br />
  23. 23. Byte Buffers<br />
  24. 24. Direct Buffers:<br />ByteBuffer allocateDirect (int capacity)<br />boolean isDirect( );<br />If Direct Buffers are not used<br /><ul><li>Create a temporary Direct ByteBuffer object.
  25. 25. Copy the content of the nondirect buffer to the temporary buffer.
  26. 26. Perform the low-level I/O operation using the temporary buffer.
  27. 27. The temporary buffer object goes out of scope and is eventually garbage collected.</li></li></ul><li>Duplicating:<br />CharBuffer asCharBuffer( );<br />ShortBuffer asShortBuffer( );<br />IntBuffer asIntBuffer( );<br />LongBuffer asLongBuffer( );<br />FloatBuffer asFloatBuffer( );<br />DoubleBuffer asDoubleBuffer( );<br />
  28. 28. View Buffers: cont.<br />ByteBuffer byteBuffer =ByteBuffer.allocate (7);<br />CharBuffer charBuffer = byteBuffer.asCharBuffer( );<br />
  29. 29. Channeljava.nio.channels<br />
  30. 30. Channel basics<br />Creating<br />Reading/Writing<br />Scatter/Gather<br />Closing<br />
  31. 31. Creating <br />SocketChannel sc = );<br />ServerSocketChannelssc = );<br />DatagramChannel dc = );<br />RandomAccessFileraf = new RandomAccessFile ("somefile", "r");<br /> FileChannel fc = raf.getChannel( );<br />
  32. 32. Reading/Writing<br />ReadableByteChannel<br /><ul><li>read (ByteBuffer dst) </li></ul>WritableByteChannel<br /><ul><li>write (ByteBuffer src)</li></ul>E.g..<br />FileInputStream input = new FileInputStream(fileName);<br /> FileChannel channel = input.getChannel( );<br />;<br />
  33. 33. Scatter/Gather<br />ScatteringByteChannel<br />read (ByteBuffer [] dsts)<br />read (ByteBuffer [] dsts, int offset, int length)<br />GatheringByteChannel<br />write(ByteBuffer[] srcs)<br />write(ByteBuffer[] srcs, int offset, int length)<br />
  34. 34. Closing<br />Channel<br /><ul><li>boolean isOpen( );
  35. 35. void close( )</li></li></ul><li>FileChanneljava.nio.channels<br />
  36. 36. File Locking<br />FileLock lock( )<br />FileLock lock (long position, long size,boolean shared)<br />FileLocktryLock( )<br />FileLocktryLock (long position, long size,boolean shared)<br />
  37. 37. Memory-Mapped Files<br />MappedByteBuffer map (MapMode mode, long position,long size)<br />
  38. 38. Channel 2 Channel transfer<br />transferTo (long position, long count, WritableByteChannel target)<br />transferFrom (ReadableByteChannel src,long position, long count)<br />
  39. 39. Socket Channelsjava.nio.channels<br />
  40. 40. Non-blocking Mode<br />SelectableChannel<br />configureBlocking (boolean block)<br />boolean isBlocking( )<br />Object blockingLock( )<br />e.g.<br />SocketChannel sc = );<br />sc.configureBlocking (false); // nonblocking mode<br />sc.configureBlocking (true); // blocking mode<br />
  41. 41. Readiness Selection<br />Socket channels can be check for readiness<br />A single thread can monitor a large number of socket<br />Steps to do readiness selection<br />Create a Selector instance<br />Register one or more non-blocking channels with it<br />Implement a infinite loop and wait for events<br />Get the selected keys list and iterate the keys<br />Process the events.<br />Remove the key from the list<br />
  42. 42. Readiness Selection<br />Selector selector =;<br />ServerSocketChannelserverChannel =;<br />serverChannel.socket().bind (new InetSocketAddress (port));<br />serverChannel.configureBlocking (false);<br />SelectionKeyregisteredkey = serverChannel.register (selector, SelectionKey.OP_ACCEPT);<br />while (true) {<br />;<br />Iterator it = selector.selectedKeys().iterator();<br />while (it.hasNext()) {<br />SelectionKey key = (SelectionKey);<br />//process <br />it.remove();<br /> }<br />}<br />
  43. 43. Selector<br />Key Methods:<br />Selector open( )<br />int select( )<br />int select (long timeout)<br />int selectNow( )<br />Set keys( )<br />Set selectedKeys( )<br />
  44. 44. SelectableChannel<br />Key methods:<br />SelectionKey register (Selector sel, int ops)<br />booleanisRegistered( );<br />SelectionKeykeyFor (Selector sel);<br />int validOps( );<br />
  45. 45. SelectionKey<br />Key methods:<br />SelectableChannel channel( )<br />Selector selector( )<br /> void cancel( )<br />boolean isValid( )<br />booleanisReadable( )<br />boolean isWritable( )<br />boolean isConnectable( )<br />boolean isAcceptable( )<br />Operations to select:<br />int OP_READ<br />int OP_WRITE<br />int OP_CONNECT<br />int OP_ACCEPT<br />
  46. 46. NIO 2JSR-203<br />
  47. 47. FileChanneljava.nio.channels<br />FileChannel open(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs)<br /> FileChannel open(Path path, OpenOption... options)<br />e.g.<br />Path file= Paths.get("C:homejugcread.txt");<br />FileChannelfc = (, WRITE, APPEND ));<br />
  48. 48. StandardOpenOption Enumjava.nio.file<br />
  49. 49. SeekableByteChannel<br />A byte channel that maintains a current position and allows the position to be changed. <br />Key Methods<br />SeekableByteChannel position(long newPosition) <br />SeekableByteChannel truncate(long size) <br />
  50. 50. NetworkChannel & MulticastChanneljava.nio.channels<br />NetworkChannels:<br />All the network-oriented channels implements the new NetworkChannel interface<br />We can easily bind the channel socket, set and query for socket options<br />MulticastChannels:<br />We can send and receive IP datagrams from a complete group<br />Implement by DatagramChannel and AsynchronousDatagramChannel<br />
  51. 51. Pathjava.nio.file<br />Locates a file using a system dependent path<br />Defines methods to access and manipulate paths<br />Defines methods to access files<br />
  52. 52. Creating a Path<br />// Microsoft Windows<br />Path p1 = Paths.get("C:homejoefoo"); <br />// Solaris syntax<br />Path path = Paths.get("/home/joe/foo");<br />Path p4 = FileSystems.getDefault().getPath("/users/sally");<br />
  53. 53. Key Methods<br />Path normalize()<br />Path toAbsolutePath()<br />Path toRealPath(LinkOption... options)<br />Path resolve(Path other)<br />Path relativize(Path other)<br />Pathjava.nio.file<br />
  54. 54. Files java.nio.file<br />Helper Class <br />Copy<br />Move<br />Delete<br />Create file, directory and links<br />
  55. 55. Copy<br />long copy(InputStream in, Path target, CopyOption... options)<br />long copy(Path source, OutputStream out)<br />Path copy(Path source, Path target, CopyOption... options)<br />
  56. 56. Move & Delete <br />void delete(Path path)<br />booleandeleteIfExists(Path path)<br />Path move(Path source, Path target, CopyOption... options)<br />
  57. 57. File, Directory and link<br />Path createFile(Path path, FileAttribute<?>... attrs)<br />Path createTempFile(String prefix, String suffix, FileAttribute<?>... attrs)<br />Path createDirectory(Path dir, FileAttribute<?>... attrs)<br />Path createDirectories(Path dir, FileAttribute<?>... attrs)<br />Path createTempDirectory(Path dir, String prefix, FileAttribute<?>... attrs)<br />Path createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)<br />
  58. 58. Walk File Tree<br />Walk a file tree rooted at a given starting file<br />Implement FileVisitor interface<br />Initiate the process by calling anyone of the below method of java.nio.file.FilesClass<br />walkFileTree(Path start, FileVisitor<? super Path> visitor)<br />walkFileTree(Path start, Set<FileVisitOption> options, intmaxDepth, FileVisitor<? super Path> visitor)<br />
  59. 59. FileVisitorjava.nio.file<br />FileVisitResultpreVisitDirectory(T dir);<br />FileVisitResultvisitFile(T file, BasicFileAttributes attrs);<br />FileVisitResultpostVisitDirectory(T dir, IOExceptionexc);<br />FileVisitResultpreVisitDirectoryFailed(T dir, IOExceptionexc);<br />FileVisitResultvisitFileFailed(T file, IOExceptionexc);<br />
  60. 60. FileVisitor Callback Methods<br />Start<br />Previsit Directory<br />postVisitDirectory<br />File 1<br />Link<br />Root 1<br />visitFile<br />visitFile<br />Previsit Directory<br />postVisitDirectory<br />File 2<br />File 3<br />visitFile<br />visitFile<br />
  61. 61. File Change Notification<br />Looking for file changes in FileSystem<br />Using the native event facility whenever available<br />Steps required to implement a watch service<br />Create a WatchService "watcher" for the file system.<br />For each directory that you want monitored, register it with the watcher.<br />Implement an infinite loop to wait for incoming events<br />Retrieve the key from the watcher's queue.<br />Process all the events associated with the Key.<br />Reset the key, and resume waiting for events.<br />Close the service<br />
  62. 62. File Change Notification – cont.<br />WatchService watcher = FileSystems.getDefault().newWatchService();<br />Path dir= Paths.get("C:homewatchdir"); <br />WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);<br />for( ; ; ){<br />WatchKeysignalledkey = watcher.take();<br /> for (WatchEvent<?> event: signalledkey .pollEvents()) {<br />WatchEvent.Kind<?> kind = event.kind();<br /> // process the event<br /> }<br />boolean valid = signalledkey .reset();<br /> if (!valid) {<br /> break;<br /> }<br />}<br />
  63. 63. WatchServicejava.nio.file<br />Watch registered objects for events and changes<br />Key Methods<br />WatchKey poll()<br />WatchKey poll(long timeout, TimeUnit unit)<br />WatchKey take()<br />void close()<br />
  64. 64. WatchKeyjava.nio.file<br />WatchKey represents registration of a watchable<br /> object with a WatchService.<br />Key Methods:<br />List<WatchEvent<?>> pollEvents()<br />Watchable watchable()<br />boolean reset()<br />boolean isValid()<br />void cancel()<br />
  65. 65. File Attributesjava.nio.file.attribute<br />Meta-data associated with file<br />Generalized metadata API<br />Fetch a file's attributes in one bulk operation <br />Map<String,Object> readAttributes(Path, String, LinkOption...)<br /><A extends BasicFileAttributes> readAttributes(Path, Class<A>, LinkOption...)<br />Grouping of related attributes and Views to access the attribute groups. <br />
  66. 66. Attributes View<br />BasicFileAttributeView<br />DosFileAttributeView<br />PosixFileAttributeView<br />FileOwnerAttributeView<br />UserDefinedFileAttributeView<br />File Attributesjava.nio.file.attribute<br />
  67. 67. File Attributesjava.nio.file.attribute<br />Path file = Paths.get("C:homefile.txt"); ;<br />BasicFileAttributesattr = Files.readAttributes(file, BasicFileAttributes.class);<br />DosFileAttributesdosattr = Files.readAttributes(file, DosFileAttributes.class);<br />PosixFileAttributesposixattr = Files.readAttributes(file, PosixFileAttributes.class);<br />
  68. 68. Asynchronous I/Ojava.nio.channels<br />They provide asynchronous operations for both sockets and files.<br />All operations work in non-blocking mode.<br />All the asynchronous I/O operations have one of two forms :<br />The first one returns a java.util.concurrent.Future that represent the pending result<br />The second one is created using a CompletionHandler. <br />
  69. 69. Futurejava.util.concurrent<br />AsynchronousFileChannel channel =“C:homeasyncfile.txt "));<br />ByteBuffer buffer = ByteBuffer.allocate(100);<br />Future result =, 100); <br />boolean done = result.isDone();<br />
  70. 70. CompletionHandler<br />Future result =, 100, buffer, new CompletionHandler(){<br />public void completed(Integer result, ByteBuffer buffer){<br />//Compute the result<br />}<br />public void failed(Throwable exception, ByteBuffer buffer){<br /> // Handle the error<br />}<br />});<br />
  71. 71. References:<br />Java NIO by Ron Hitchens Publisher: O’Reilly<br /><br /><br />
  72. 72. Thanks U!!!!<br />