Tuesday 4 June 2019

Introduction to Java NIO and NIO2


Java NIO stands for Non-Blocking Input-output, it is enhanced version of IO package, by using this library you can develop high-performance IO applications.

NIO packages is available from JDK 1.4 (or) higher platforms.

JAVA NIO2 (It is an extension of IO and NIO. It offers native file access, Watcher service to watch directory for CRUD events, directory structure exploration API etc.,) is introduced in JDK 1.7. In this tutorial series, you are going to learn Java NIO and NIO2.

What is input stream?
Input stream is something that is used to read data from an entity like a file, socket, url etc.,

What is output stream?
output stream is something that is used to write data to a file, socket etc.,

Nio vs IO
a.   Major difference between NIO and IO is, IO is blocking, whereas NIO is non-blocking.

b.   All the operations in IO are synchronous. Whereas NIO support asynchronous operations.

c.    NIO uses Buffer I/O (bulk access to raw bytes), whereas standard IO package uses stream I/O.

In Stream oriented I/O, applications deal with one byte at a time. Input stream produces one byte of data and output stream consumes one byte of data. Since data is read from or written to stream, you can’t move forth and back in the stream.

In Buffer oriented I/O, applications deal with a block of data. You can assume a block of is a collection of bytes of data. Data is read into a buffer and can be available for later processing.

d.   Streams are basic building blocks in IO. Where as Channels are basic building blocks in NIO. Channels are bi-directional, whereas streams are in one-direction. You can open a channel for reading, writing (or) for both.

e.   IOStreams like BufferedInputStream, BufferedOutputStream consumes java heap memory (which may leads to HeapOverflow erros, if you are dealing with file processing applications), whereas NIO support non-heap memory allocation.

f.     All the standard character sets are supported by NIO.

What is Blocked IO?
In standard IO, whenever a thread invokes read() or write() method on a stream, the thread gets blocked until there is some data to be read or the data is written to. Thread gets unblocked after the read() or write() operation finishes.

In case of Non-blocking IO, thread requests for data to be written to a channel and can-do other work meantime (until data is written to channel). With this approach, a single thread can able to handle multiple channels of input and output.

Channels and Buffers
Channels and Buffers are the basic building blocks of Java NIO.

Buffer is a container that holds some data.

Channels are analogous to the streams in IO. All the data should go via channels. If you send any data to channel, the data is placed in a buffer. Any data that is read from a channel is read into a buffer.

Channels vs Streams
Channels are bi-directional, whereas streams are in one-direction. You can open a channel for reading, writing (or) for both.

You are going to learn.

Introduction to NIO and NIO2

      Introduction to Java NIO and NIO2
      NIO:Buffers, Channels and Selectors
      Exploring Channel
      FileChannel: Reading data from a file
      FileChannel: Write to a file
      FileChannel: Read and write together
      What will happen when application request I/O operation?

Working with buffers and selectors

      Working with Buffers
      Buffer capacity, size, position, flip and clear methods
      Writing data to buffers
      Reading data from buffers
      Buffer: Marking and resetting
      Nio: Buffer: clear() : set the current position back to 0
      When will you get InvalidMarkException?
      Nio: Buffer: flip(): Flip the stream
      Buffer: rewind(): rewind the buffer
      nio: Buffer: read only buffers
      When will you get ‘ReadOnlyBufferException’?
      nio: Buffer apis chaining
      Get the buffer capacity
      Get the current position of the buffe
      Set the current position of the buffer
      Set and get the buffer limit
      Get the remaining element in the buffer
      Check remaining elements exist or not
      check whether the buffer is read only or not
      Check whether buffer is backed by array or not
      Get the buffer contents as an array
      wrap existing array into a buffer
      Buffer slicing
      FileChannel.open method
      Exploring OpenOption
      Direct and indirect buffers
      Reading and writing to Multiple Buffers
      MappedByteBuffer
      Convert string to ByteBuffer
      Convert ByteBuffer to a string
      Convert ByteBuffer to CharBuffer
      Convert CharBuffer to ByteBuffer
      Convert ByteBuffer to IntBuffer
      Selectors: Asynchronous Operations with NIO
      How to read contents of file asynchronously?
      Write to a file asynchronously in Java

Introduction to NIO2

      NIO2 Introduction
      Introducing FileSystemProvider class
      How to change default file system provider?
      FileSystemProvider: getScheme: Get the scheme that identifies this provider
      Get all the installed file system providers
      Get FileSystem from the File system provider
      Get default file system
      getFileSystem: Get FileSystem for given scheme
      Working with FileStore object
      getFileSystem: Get FileSystem for given scheme
      Working with FileStore object
      WatcherService: Watching a directory for changes in Java

Miscellaneous

      Implement ByteBuffer input stream


Previous                                                 Next                                                 Home

No comments:

Post a Comment