Java NIO Buffering Technology detailed

Source: Internet
Author: User
Tags rewind

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
    1. 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);
    2. Make a flip operation to prepare for the read operation. Bytebuffer.flip ();
    3. The following can be read into the data. int numberofbytes = (Bytebuffer);
    4. The data can then be read from the buffer. char C = (char) bytebuffer.get ();
How to write a NIO buffer
    1. Create buffers to allocate capacity. Bytebuffer Bytebuffer = bytebuffer.allocate (512); Capacity is set to 512
    2. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.