Home  >  Core Java

Java NIO Framework with Example

By Arvind Rai, December 29, 2013
After IO, java provides new IO. That is also called NIO and has been introduced from JDK 1.4. NIO brings so many features to read and write to files, channels and sockets. There is two generation of NIO API and that is NIO.1 and NIO.2. In NIO.1 buffer, charset and channels are introduced. In JDK 7, NIO framework has introduced new packages java.nio.file and others. These are called NIO.2 series. It is a measure release in NIO framework. NIO.2 provides Path, Files and other classes that have changed the way of file handling in java. Now we will look at NIO framework in detail and we have provided the page links to get better clarity by example.

Buffers in Java NIO

Java NIO started with buffers. Buffers are the container of data. java.nio package contains all buffer API. There are data type specific buffer like ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer. java.nio.Buffer is the base class which is extended by all data type buffer mentioned above. Buffer is sequence of elements of any specific primitive type. The basic properties of buffer are its capacity, limit and position and buffers are not thread safe.

Capacity in Buffer

Number of elements that a buffer contains is the capacity of buffer. The capacity of buffer cannot be negative and never changes.

Limit in Buffer

This is the index of first element that is not read or written. Limit cannot be negative and cannot be greater than its capacity.

Position in Buffer

It is the index of next element that will be read or written. Position cannot be negative and cannot be greater than its limit.

mark and reset in Buffer

At some point the position of buffer is reset by calling reset method. That position is called mark. reset() can only be called when mark is defined and mark is not defined always. If we have defined a mark and at some point if we adjust position and limit to be lower than mark, then mark will be discarded. The relation between mark, limit, position and capacity is given below.

0 <= mark <= position <= limit <= capacity

Role of clear() method in Java NIO Buffer

clear() method makes buffer ready to accept new stream in read and relative put operation. It sets the limit to the capacity and position becomes zero.

Role of flip() method in Java NIO Buffer

flip() method makes buffer ready in case of write and relative get operation. It sets the limit to current position and position becomes zero.

Role of rewind() method in Java NIO Buffer

rewind() makes buffer ready to re-reading the data from buffer. In this case limit remains unchanged and position becomes zero.

BufferUnderflowException in Java NIO

In read operation which start from current position and position is incremented by the number of transferred element and if the number of elements requested exceeds the limit then relative get operation throws unchecked exception that is java.nio. BufferUnderflowException.

BufferOverflowException in Java NIO

In write operation requested elements are put into buffer and if it exceeds the limit then the put method throws unchecked exception that is java.nio.BufferOverflowException

Now find some some API of java.nio package.

ByteBuffer ByteBuffer has been introduced in JDK 1.4. ByteBuffer is the buffer of bytes. ByteBuffer is abstract class but it has static method. Find the page link for more detail and example.

CharBuffer CharBuffer is the buffer for characters. CharBuffer object is created by calling allocate(). Find the page link for more detail and example.

Charsets in Java NIO

The charsets API are under the package java.nio.charset. The role of charset is named mapping between sequences of bytes and 16 bit unicode characters. There are encoders and decoders in java nio charsets API. Together we call it coders. Encoder transforms character into bytes and decoder transforms bytes into any specific charset. Find the charsets API and its example.

Charset Charset plays the role of encoding and decoding between given charset and UNICODE. Find the page link for more detail and example.

CharsetDecoder and CharsetEncoder In java encode and decode from one charset to another charset can be done using java NIO API. A ByteBuffer with any charset can be changed to CharBuffer in Unicode charset. Find the page link for more detail and example.

Channels in Java

NIO channels are in the package java.nio.channels. A channel is open connection to an entity like file, socket or any hardware device. Channels can be asynchronously closed and interrupted. There is different type of channels.

AsynchronousChannelGroup AsynchronousChannelGroup performs resource sharing in the group of asynchronous channels. Find page link for more detail and example.

Pipe.SinkChannel and Pipe.SourceChannel Pipe ensures that data will be read in same order in which it is written to Pipe. Find the page link for more detail and example.

FileLock FileLock locks or tries for lock for the given part of the file. Find the page link for more detail and example.

FileChannel FileChannel keeps a current position that can be modified. Find the page link for more detail and example.
ReadableByteChannel Channels return IO classes. ReadableByteChannel is obtained by Channels. newChannel(in) by passing Input Stream. Find the page link for more detail and example.

File API in Java NIO

In JDK 7, NIO.2 has been introduced with the package java.nio.file. This API provides classes to access file, file attribute and file system. File API supports symbolic links that is reference link for any file location. There is interoperability between File and Path by toPath() method. We will discuss some classes of java.nio.file API.

Path and Paths The use of Path is to locate a file in file system. Path has flexibility to locate the file by proving path in parts. Find the page link for more detail and example.

Files It can create and delete directory as well as files. It also creates link between the existing files and link directory. Find the page link for more detail and example.

DirectoryStream DirectoryStream iterates over the files and returns Path instance for every file. DirectoryStream must be closed after iteration otherwise there will be resource leak. Find the page link for more detail and example.

WatchService WatchService has the responsibility to watch all the registered objects for their changes. Only watchable object should be registered with watch service to monitor the changes. Find the page link for more detail and example.
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us