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

Source: Internet
Author: User
Tags rewind
Buffer class

Defines a container interface that can be primitive type data in a linear location. The buffer mainly contains the type (Byte, char ...). ) unrelated features.

It is worth noting that the buffer and its subclasses are not thread-safe.

Each buffer has the following properties:

Capacity
The maximum amount of data this buffer can put. Capacity are typically specified when the buffer is created.

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

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

Mark
A temporary storage position subscript. Calling Mark () will set mark as the current position value, and later call Reset () will set the Position property
To Mark's value. Mark's value is always less than or equal to the position value, and if the value of the position is set to be 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 by the limit () and position () functions, or they can be changed by the following functions:

Buffer Clear ()

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

Buffer Flip ()

Public final Buffer Flip() {
limit = position;
Position = 0;
Mark =-1;
return this;
}
The limit is set to the current position, and the position is set to 0, which is usually called before the data is read from the 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 the buffer.

The buffer object may be read-only, and any write to the object will trigger a readonlybufferexception.
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
The operations are basically spread around Bytebuffer.

Bytebuffer defines 4 static methods for creating work:

Bytebuffer allocate (int capacity)//Create a bytebuffer of 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 part of a byte array or byte array into Bytebuffer.

Bytebuffer defines a series of get and put operations from which to read and write byte data, 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 the relative set of two, relative positioning of the reading and writing operations rely on position to locate in the buffer, and in the
The position value is updated when the completion is done. In other types of buffer, the same function is defined to read and write data, and the only difference is a
The types of parameters and return values.

In addition to the function of reading and writing byte 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)//write an int value into Bytebuffer.

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

Another special place for Bytebuffer is to get other types of buffer on its basis. Such as:
Charbuffer Ascharbuffer ()
Creates a Charbuffer view for the current bytebuffer. The read and write operations in the view buffer will follow the Bytebuffer byte
Sequence to the data in the Bytebuffer.

The buffer created with this kind of method will start at the position position of the bytebuffer to the end of the limit position, which can be considered as the data
The view. The readonly and direct properties of view buffer are consistent with the bytebuffer, and only in this way can
To get direct buffer of other data types.

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: 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.