Java.nio.ByteBuffer and flip,clear and rewind differences

Source: Internet
Author: User

Buffer class

Defines a container interface that can store primitive type data linearly. Buffer primarily contains a type (Byte, char ... ) unrelated functions.

It is important to note that both buffer and its subclasses are not thread-safe.

Each buffer has the following properties:

Capacity
How much data this buffer can put up. Capacity is typically specified when buffer is created.

Limit
Read and write operations on buffer cannot cross this subscript. When writing data to buffer, the limit is generally equal to capacity, and when the data is read,
The limit represents the length of valid data in buffer.

Position
The current subscript for the read/write operation. When a read/write operation is performed using the relative position of buffer, the read/write is performed from this subscript, and after the operation is completed,
Buffer updates the underlying value.

Mark
A temporary storage location subscript. Calling Mark () sets mark as the value of the current position, and a later call to reset () sets the Position property
The value to mark. Mark's value is always less than or equal to position, and if the value of position is set smaller than mark, the current mark value is discarded.

These properties always meet the following criteria:
0 <= Mark <= position <= limit <= capacity

The values of limit and position are set in addition to the limit () and position () functions, which can also be changed by the following functions:

Buffer Clear ()

Public final Buffer Clear() {
Position = 0; Set to 0
Limit = capacity; The same limit and capacity
Mark =-1; Cancellation token
return this;
}
Set position to 0 and set limit to capacity, which is usually called before data is written to buffer.

Buffer Flip ()

Public final Buffer Flip() {
limit = position;
Position = 0;
Mark =-1;
return this;
}
Set the limit to the current position and set the position to 0, which is usually called before the data is read from buffer.

Buffer Rewind ()

Public final Buffer Rewind() {
Position = 0;
Mark =-1;
return this;
}
The position is set to 0,limit, which is usually called before the data is rewritten into buffer.

The buffer object may be read-only, and any write to the object will trigger a readonlybufferexception.
The IsReadOnly () method can be used to determine whether a buffer is read-only

Bytebuffer class

In the subclass of buffer, Bytebuffer is a special class, because the IO of various channel defined in Java.io.channels
Operations are basically carried out around the bytebuffer.

Bytebuffer defines 4 static methods to do the creation work:

Bytebuffer allocate (int capacity)//Create a Bytebuffer for the specified capacity.
Bytebuffer allocatedirect (int capacity)//create a direct bytebuffer so that the Bytebuffer performs better when participating in IO operations
Bytebuffer Wrap (Byte [] array)
Bytebuffer Wrap (Byte [] array, int offset, int length)//wrap a byte array or a portion of a byte array into Bytebuffer.

Bytebuffer defines a series of get and put operations to read and write byte data from, such as the following:
BYTE get ()
Bytebuffer get (Byte [] DST)
BYTE get (int index)
Bytebuffer put (Byte b)
Bytebuffer put (byte [] src)
Bytebuffer put (int index, byte b)
These operations can be divided into absolute positioning and relative to the two, relative positioning of the read and write operations rely on position to locate the position in the buffer, and in the operating
The value of position is updated when it is completed. In other types of buffer, the same function is defined to read and write data, the only difference is a
Types of parameters and return values.

In addition to reading and writing a function of byte type data, one of the special bytebuffer is that it also defines methods for reading and writing other primitive data, such as:

int getInt ()//reads an int value from the Bytebuffer.
Bytebuffer putint (int value)//writes an int value into Bytebuffer.

Reading and writing other types of data involves a byte-order problem, and Bytebuffer writes or reads a different byte-order (large-endian or small-endian)
Type of data (Int,long ... )。 The byte order can be obtained and set using the order method:
Byteorder order ()//returns the byte order of the Bytebuffer.
Bytebuffer order (Byteorder bo)//Set the Bytebuffer byte order.

Another special area of Bytebuffer is the ability to get other types of buffer on its basis. Such as:
Charbuffer Ascharbuffer ()
Creates a charbuffer view of the current bytebuffer. Read and write operations in this view buffer will follow Bytebuffer bytes
To the data in the Bytebuffer.

The buffer created with this type of method will start at the position position of the bytebuffer to the end of the limit position, which can be considered as this data
's view. The ReadOnly property and the direct property of the view buffer are consistent with the bytebuffer, and this is the only way to
To get the other data type of direct buffer.

Transferred from: http://xiachaofeng.iteye.com/blog/1416634

Java.nio.ByteBuffer and flip,clear and rewind differences

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.