"Java" "NiO" 4, Java NIO Buffer

Source: Internet
Author: User
Tags compact

The buffer of Java NiO is used to interact with the channel.
Buffer is essentially a block of memory, you can write the data and then read it out.
This block of memory is wrapped by NiO's buffer object, which provides a series of methods that make access to the block of memory much easier.

Basic buffer Use

Reading and writing data using buffer generally has the following 4 steps:
1. Write Data to Buffer
2. Call the Buffer.flip () method
3. Read the data from the buffer
4. Call the Buffer.clear () method or the Buffer.compact () method

When you write data to buffer, it records how much data you write. Once you have read the data, you need to invert buffer and convert the buffer from write mode to read mode with the Flip method. In read mode, buffer allows you to read all the data written in.

Once you have read all the data, you need to clear buffer and let it be ready for writing again in two ways: clear () or compact (). Clear clears the entire buffer,compact and clears only the data that you have read. Other unread data will be moved to the head of buffer, and now the data written will be after the unread data.

Instance:

Note the order of calls in the code, such as Flip,clear

Capacity,position and limit for buffer

Buffer has three properties, as shown above
Position and limit depend on whether buffer is in read or write mode. Capacity irrelevant mode, always the same.
Look at a picture.

Capacity

Buffer has a fixed size, called capacity, can also write capacity byte type, long integer type, character type and other data, write buffer. Once the buffer is full, you need to empty it (read or clear) before writing the data.

Position

When you write data to buffer, you write a position. The position is initially 0. When the data is written, position points to the location where the data was inserted. The position maximum value is capacity-1.
When you read data from buffer, you also start reading from a position. When you flip a buffer from write mode to read mode, position is reset to 0. When reading the data, the position will always move, pointing to the next read position.

Limit

In write mode, the limit represents how much data you can write, equal to the capacity of buffer.
When you flip to write mode, limit represents how much data you can read, so when Flip is in read mode, limit is set to position in write mode. In other words, you can read all the data you've written.

Buffer Types

The following buffer types are available in Java NiO:
· Bytebuffer
· Mappedbytebuffer
· Charbuffer
· DoubleBuffer
· Floatbuffer
· Intbuffer
· Longbuffer
· Shortbuffer
These buffer types represent different data types. Mappedbytebuffer will say in subsequent chapters.

Allocating a Buffer

In order to get a buffer object, you must first allocate a piece of space. Each buffer class has a allocate method.

Bytebuffer buf = Bytebuffer.allocate (48);

Charbuffer buf = charbuffer.allocate (1024);

Writing Data to a Buffer

There are two ways of writing data to buffer:
1. Write data from channel to buffer
2. Write data to yourself via the put method of buffer

int bytesread = Inchannel.read (BUF); Read into buffer.

Buf.put (127);

Flip ()

The flip method turns the buffer from write mode to read mode. Call the Flip method to set position to 0 and set limit to the position position just before.
In other words, position is now a read position and limit is the most restricted read.

Reading Data from a Buffer

The same two ways to read data from buffer:
1. Read data from buffer to channel
2. Read from yourself by the Get method

Read from buffer into channel.
int byteswritten = Inchannel.write (BUF);

byte Abyte = Buf.get ();

Rewind ()

This method sets position to 0, so you can reread all the data in the buffer. Limit remains unchanged, which still indicates how much data can be read from buffer.

Clear () and compact ()

Once you have finished reading the buffer data, you must once again be ready to write buffer, through the clear or compact method.
The Clear method sets position to 0,limit to capacity. In other words, buffer clears, the data in buffer is not cleared, but the tag tells you where to start writing the data.

If there is any unread data, when you call the clear method, the data is ignored. cannot be retrieved.

If there is still data unread, but you want to read it later, you can call the compact instead of clear.

The Compact method moves all unread data to the head of buffer, setting the last position of position unread data, limit=capacity. Now buffer is ready to write, but unread data will not be rewritten.

Mark () and Reset ()

You can use the Mark method to mark a buffer in position, and later you can set position by the Reset method to the location you just tagged.

Buffer.mark ();
Call Buffer.get () a couple of times, e.g. during parsing.
Buffer.reset (); Set position back to mark.

Equals () and CompareTo ()

Compares two buffer for equality.

Equals ()

Two buffer if equal, the following conditions are met:
1, the same type
2. The same content space in buffer
3. Same content in buffer

CompareTo ()

This method compares the contents of the buffer, such as sorting, etc.
1.

"Java" "NiO" 4, Java NIO Buffer

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: info-contact@alibabacloud.com 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.