A buffer is a piece of data that is being read from a channel that is about to be written to or just out of the channel. It is an object that holds data and plays the end of the NIO channel. Buffers provide a formal mechanism for data access and read and write processes.
It is one of the main differences between NIO and older Java I/O. Before data is read and written directly from the stream, data can now be read and written from the buffer. In NiO, a channel is a synonym for a stream. To learn more about the NIO channel, please read the previous tutorial Java NIO Channel.
NIO buffer characteristics
- The basic constituent module of Java NiO is the buffer.
- Buffers provide a fixed-size container to read data.
- Each buffer is readable, but only a specific buffer is writable.
- The buffer is the endpoint of the channel.
- The contents of a read-only buffer are immutable, but their mark, position, and limit are mutable.
- By default, buffers are not thread-safe.
Buffer type
Each primitive type has a corresponding buffer type. All buffer classes implement a buffer interface. The most commonly used buffer type is bytebuffer. The following are the buffer types provided in the Java NIO package.
- Bytebuffer
- Charbuffer
- Shortbuffer
- Intbuffer
- Longbuffer
- Floatbuffer
- DoubleBuffer
- Mappedbytebuffer
Buffer capacity
Buffer has a fixed size, we can only store less than "fixed size" of the data, fixed-size value is called the capacity of the buffer. Once the buffer fills up, it must be emptied to write again. Once the capacity is set, it will not change during the life cycle of the buffer.
Buffer bounds
In write mode, the bounds of the buffer are equal to the capacity. In read mode, the bounds point to the next bit of the last data bit in the buffer. When the buffer is written, the bounds are incremented. The bounds of the buffer are always greater than or equal to zero and less than or equal to the capacity, 0 <= bounds <= capacity.
Buffer position
The position points to the current address of the buffer. When the buffer is created, the position is set to zero. During the read and write process, the position is incremented to the next index position. The position is always between 0 and bounds.
Buffer flags
The tag is similar to setting a bookmark to a buffer. The current position is recorded when you call Mark (), and the position of the marker when you call Reset () is restored.
Buffer flip, clear, and rewind
Buffer Flip ()
The flip () method is used to prepare the buffer for a get operation or to prepare a new write sequence. Flip () Sets the bounds to the current position and then positions the position to 0.
Buffer Clear ()
The clear () method is used to prepare the buffer for a put operation or to prepare a new read sequence. Clear () Sets the bounds to the position of the capacity and places the position at 0.
Buffer Rewind ()
The rewind () method is used to read the data that has been acquired again. Rewind () Place the buffer position at 0.
How to read the NIO buffers
- The buffer is created first, and the capacity is allocated. Buffer has a allocate (size) method that can return a buffer object. Bytebuffer Bytebuffer = bytebuffer.allocate (512);
- Make a flip operation to prepare for the read operation. Bytebuffer.flip ();
- The following can be read into the data. int numberofbytes = Filechannel.read (Bytebuffer);
- The data can then be read from the buffer. char C = (char) bytebuffer.get ();
How to write a NIO buffer
- Create buffers to allocate capacity. Bytebuffer Bytebuffer = bytebuffer.allocate (512); Capacity is set to 512
- Write the data. Bytebuffer.put ((byte) 0xff);
These are the two examples of read-write buffers. There are many types of buffers and many kinds of reading and writing methods. You can choose according to the requirements of the use.
Java NIO Buffering Technology detailed