One, the difference between Java io and NiO:
(1) Java NIO provides different IO modes of operation than standard IO:
- Channels and Buffers (channels and buffers): Standard IO is based on byte stream and character stream, while NiO is based on channel (channels) and buffers (buffer), data is always read from the channel into the buffer, or written to the channel from the buffer.
- Asynchronous IO (Asynchronous IO ): Java NIO allows you to use IO asynchronously, for example: When a thread reads data from a channel to a buffer, the thread can do something else. When the data is written to the buffer, the thread can continue to process it. Writing a channel from a buffer is similar.
- Selectors (selector): Java NiO introduces the concept of selectors, which are used to listen for events on multiple channels (e.g., connection open, data arrival). Therefore, a single thread can listen on multiple data channels.
(2) main differences between Java NIO and IO
The following table summarizes the main differences between Javanio and Io, and I'll describe in more detail the differences in each section of the table.
|
Io |
Nio |
|
Stream oriented |
Buffer oriented |
|
Blocking IO |
Non Blocking IO |
|
|
Selectors |
stream-oriented and buffer-oriented
The first major difference between Javanio and Io is that IO is stream-oriented and NIO is buffer-oriented. The Java io-oriented stream means that one or more bytes are read from the stream every time, until all bytes are read, and they are not being slowed anywhere. In addition, it cannot move data in the stream back and forth. If you need to move the data read from the stream before and after it, you need to cache it to a buffer first. Java NiO has a slightly different buffer-oriented approach. The data is read to a buffer that it processes later, and can be moved back and forth in the buffer if needed. This increases the flexibility of the process. However, you also need to check if the buffer contains all the data that you need to process. Also, make sure that when more data is read into the buffer, do not overwrite the data that has not been processed in the buffer.
blocking and non-blocking IO
The various flows of Javaio are blocked. This means that when a thread calls read () or write (), the thread is blocked until some data is read, or the data is fully written. The thread can no longer do anything during this time. The non-blocking mode of Java NIO enables a thread to send requests to read data from a channel, but it can only get the data that is currently available and will not get anything if no data is available. Instead of keeping the thread blocked, the thread can continue to do other things until the data becomes readable. The same is true for non-blocking writes. A thread requests to write some data to a channel, but does not have to wait for it to be fully written, and the thread can do something else at the same time. Threads typically use non-blocking IO idle time to perform IO operations on other channels, so a single thread can now manage multiple input and output channels (channel).
Selector ( Selectors )
The Javanio selector allows a single thread to monitor multiple input channels, you can register multiple channels using a selector, and then use a separate thread to "select" the channel: these channels already have inputs that can be processed, or select the channels that are ready to be written. This selection mechanism makes it easy for a single thread to manage multiple channels.
NIO and the IO How to influence the design of an application
Whether you choose IO or the NIO toolbox, you may have an impact on the following aspects of your application design:
- API calls to the NIO or IO classes.
- Data processing.
- The number of threads used to process the data.
API called
Of course, using NIO's API calls may look different than using IO, but it's not surprising that the data must be read into the buffer before it is not read from only one inputstream.
Data Processing
Data processing is also affected by the use of purely nio designs compared to IO designs.
Second, Channel ( Channel )
Javanio channels are similar to streams, but are somewhat different:
- The data can be read from the channel, and the data can be written to the channel. But stream reads and writes are usually one-way.
- Channels can be read and written asynchronously.
- The data in the channel is always read to a buffer first, or it is always written from a buffer.
As mentioned above, the data is read from the channel to the buffer, and the data is written from the buffer to the channel.
Channel the implementation
These are the most important implementations of the channel in Java NIO:
- FileChannel: Read and write data from a file.
- Datagramchannel: Can read and write data on the network through UDP.
- Socketchannel: Data in the network can be read and written through TCP.
- Serversocketchannel: You can listen for incoming TCP connections, like Web servers. A socketchannel is created for each new incoming connection.
Third, Buffer ( Buffer )
The buffer in Javanio is used to interact with the NIO channel. As you know, the data is read from the channel into the buffer, written from the buffer to the channel.
A buffer is essentially a piece of memory in which data can be written and then read from it. This memory is packaged as a Niobuffer object and provides a set of methods for easy access to the block memory.
Buffer Basic usage of
Reading and writing data using buffer typically follows these four steps:
- Write data to Buffer
- Call the Flip () method
- reading data from buffer
- Call the Clear () method or the compact () method
When data is written to buffer, buffer records how much data is written. Once you are reading the data, you need to switch the buffer from write mode to read mode via the Flip () method. In read mode, all data written to buffer can be read.
Once all the data has been read, the buffer needs to be emptied so that it can be written again. There are two ways to clear a buffer: Call the Clear () or compact () method. The clear () method empties the entire buffer. The compact () method clears only the data that has been read. Any unread data is moved to the beginning of the buffer, and the newly written data is placed behind the buffer's unread data.
Buffer of the capacity,position and the Limit
A buffer is essentially a piece of memory in which data can be written and then read from it. This memory is packaged as a Niobuffer object and provides a set of methods for easy access to the block memory.
To understand how the buffer works, you need to familiarize yourself with its three properties:
The meaning of position and limit depends on whether the buffer is in read or write mode. No matter what mode the buffer is in, the meaning of capacity is always the same.
capacity
As a block of memory, buffer has a fixed size value, also known as "capacity". You can only write in capacity a byte, Long,char and other types. Once the buffer is full, it needs to be emptied (by reading the data or clearing the data) to continue writing the data.
position
When you write data into buffer, position represents the current position. The initial position value is 0. When a byte, long, and other data is written to buffer, the position moves forward to the next buffer cell that can insert the data. The position can be capacity–1 maximum.
When reading data, it is also read from a specific location. When the buffer is switched from write mode to read mode, the position is reset to 0. When the data is read from the position in buffer, the position moves forward to the next readable position.
Limit
In write mode, the limit of buffer indicates how much data you can write to buffer. Write mode, limit equals buffer's capacity.
When you switch buffer to read mode, limit indicates how much data you can read. Therefore, when you toggle buffer to read mode, limit is set to the position value in write mode. In other words, you can read all the previously written data (limit is set to the number of written data, this value is position in write mode)
Iv. type of Buffer
The Javanio has the following buffer types:
- Bytebuffer
- Mappedbytebuffer
- Charbuffer
- DoubleBuffer
- Floatbuffer
- Intbuffer
- Longbuffer
- Shortbuffer
As you can see, these buffer types represent different data types. In other words, the bytes in the buffer can be manipulated by a char,short,int,long,float or double type.
Buffer the distribution
To get a buffer object, you first need to allocate it. Each buffer class has a allocate method.
Flip () Method
The flip method switches buffer from write mode to read mode. Calling the flip () method resets position back to 0 and sets the limit to the value of the previous position. In other words, position is now used to mark the read position, and limit indicates how many bytes, char, and so on have been written before--how many bytes, char, and so on can now be read.
Clear () with the Compact () Method
Once you have finished reading the data in buffer, you need to make buffer ready to be written again. Can be done through the clear () or compact () method.
If the call is the clear () method, position will be set back to the value 0,limit to capacity. In other words, Buffer is emptied. The data in buffer is not cleared, but these tags tell us where we can start writing data to buffer.
If there are some unread data in buffer and the clear () method is called, the data will be "forgotten", meaning no more tags will tell you what data has been read or not.
If you still have unread data in buffer and you need that data later, but you want to write some data first, use the compact () method.
The Compact () method copies all unread data to the beginning of the buffer. The position is then set to the last unread element just behind it. The Limit property is still set to capacity, just like the clear () method. Now buffer is ready to write the data, but it will not overwrite the unread data.
Five, transfer of data between channels
In Java NiO, if one of the two channels is FileChannel, you can transfer data directly from one channel to another channel.
Transferfrom () : FileChannel's Transferfrom () method to transfer data from the source channel to the FileChannel
transferTo () : The TransferTo () method transfers data from FileChannel to other channel.
Liu,Selector
Selector allows single-threaded processing of multiple Channel. If your app has multiple connections (channels) open, but the traffic is low for each connection, it's convenient to use selector. For example, in a chat server. Selector class is associated with Unix in the system Poll , Select , Epoll .
To use selector, you have to register the channel with selector and then call its select () method. This method will always block to a registered channel with event readiness. Once this method returns, the thread can handle these events, with examples of events such as new connections coming in, data receiving, and so on.
Java NiO Reading notes