IO and NIO in Java

Difference between IO and NIO

  • Java IO (input-output) is thread-safe and Java NIO (new/non-blocking io) is buffer-oriented. Stream-oriented I/O means reading/writing from/to a stream of one or more bytes per unit of time in turn. This information is not cached anywhere. Thus, it is impossible to arbitrarily move forward or backward in the data stream. In Java NIO, data is read into a buffer first, which gives more flexibility when processing data.
  • I/O streams in Java IO are blocking. This means that when the read() or write() method of any class from the java.io.* package is called on the thread of execution, a blocking occurs until the data is read or written. The thread of execution cannot do anything else at the moment. Java NIO's non-blocking mode allows you to request read data from a channel and receive only what is currently available, or nothing at all if there is no data available yet. Instead of staying locked until data becomes readable, the thread of execution can do something else. The same is true for non-blocking output. The thread of execution can request to write some data to the pipe, but does not wait until it is completely written.
  • Java NIO provides selectors that allow a single thread of execution to monitor multiple input channels. It is possible to register several channels with a selector, and then use one thread of execution to serve channels that have data available for processing, or to select channels ready for recording.

Features of NIO

  • Channels and Selectors: NIO supports various types of channels. A pipe is an abstraction of lower-level objects in the file system (for example, memory-mapped files and file locks), which allows data to be transferred at a faster rate. Channels are not blocked, and therefore Java also provides tools such as a selector, which allows you to select a ready channel for data transfer, and a socket, which is a tool for blocking.
  • Buffers: has buffering for all primitive wrapper classes (except Boolean). The abstract Buffer class was introduced, which provides operations such as clear, flip, mark, etc. Its subclasses provide methods for getting and setting data.
  • Encodings: Encoders and decoders have appeared to display Unicode bytes and characters.

Channels

Channels are logical (not physical) portals, abstractions of lower-level objects in the file system (for example, memory-mapped files and file locks) through which data input/output is carried out, and buffers are sources or destinations of this transmitted data. When organizing the output, the data to be sent is placed in a buffer, which is then transferred to the pipe. On input, data from the pipe is placed in a pre-allocated buffer.

Pipes efficiently transport data between byte buffers and entities on the other side of the pipes. Pipes are gateways that allow you to access operating system I/O services with minimal overhead, and buffers are the internal endpoints of these gateways used to send and receive data.


Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed