Java NIO Buffers

Source: Internet
Author: User
Tags comparable rewind

A Buffer object is a container for a fixed number of data. A channel is the entry through which I/O transmissions occur, and buffers are the source or destination of these data transmissions.

Buffer base

All buffers have four properties for information about the data elements that they contain.

    • Capacity (capacity): The buffer can hold the maximum value of the data and cannot be changed after the buffer is created.
    • Limit (upper bound): The first element of a buffer that cannot be read or written. Or, the count of existing elements in the buffer.
    • Position (position): The index of the next element to be read or written. The call to get or put functions is updated.
    • Mark (Mark): a memo location. Call Mark () to set the mark=postion. Call Reset () to set position= mark. The tag is undefined before setting (undefined).

These four attributes always follow the following relationship:

0 <= Mark <= position <= limit <= capacity

is a newly created Bytebuffer:

The position is set to 0, and the capacity and upper bounds are set to 10, so that the last byte that the buffer can hold. The tag was not originally defined. The capacity is fixed, but the other three properties can be changed when the buffer is used.

The following is the method signature for Buffer:

Public abstract class Buffer {public    final int capacity () {    } public    final int position () {    }    Public Final buffer position (int newposition) {    } public    final int. limit () {    } public    final buffer limit (int newl  Imit) {    } public    final buffer mark () {    } public    final buffer reset () {    } public    final buffer Clear () {    } public final    buffer Flip () {    } publicly final    buffer rewind () {    }    public final in T remaining () {    } public    final Boolean hasremaining () {    } public    abstract Boolean isreadonly ();    Public abstract Boolean HasArray ();    Public abstract Object Array ();    public abstract int Arrayoffset ();    Public abstract Boolean Isdirect ();}

The Buffer API listed above does not include a get () or put () function. Each Buffer class has these two functions, but the type of arguments they take, and the type of data they return, are unique to each subclass, so they cannot be declared abstractly in the top-level buffer class.

The following is the declaration of Bytebuffer:

Public abstract class Bytebuffer extends Buffer implements comparable{    //This is a partial API listing public    ABS tract byte get ();    public abstract byte get (int index);    Public abstract bytebuffer put (byte b);    Public abstract bytebuffer put (int index, byte b);}

An example looks at the storage of Bytebuffer:

Buffer.put ((byte) ' H '). Put ((byte) ' E '). put ((byte) ' L '). Put ((byte) ' O ');

If you do the following:

Buffer.put (0, (byte) ' M '). Put ((byte) ' W ');

When the buffer is full, to read the contents, we need to flip the buffer, we can call the Flip method, the following is the flip method in Bytebuffer:

Public final Buffer Flip () {        limit = position;        Position = 0;        Mark =-1;        return this;    }

The rewind () function is similar to flip (), but does not affect the upper bound property. It simply sets the position value back to 0. You can use rewind () to reread the data in the buffer that has been flipped.

The Hasremaining () method returns whether the upper bound of the buffer is currently reached.

The remaining () method returns the distance that the upper bound is reached.

The token of the buffer is undefined before the mark () function is called, and the value at the time of the call is set to the current position.

The reset () function sets the position to the current tag value. If the tag value is undefined, calling reset () causes the invalidmarkexception exception.

Rewind (), clear (), and flip () always discard the tag, which is set to-1.

The necessary and sufficient conditions for two buffers to be considered equal are:

    • Two objects of the same type. Buffer with different data types is far from equal, and buffer is never equal to a non-buffer object.
    • Two objects have the same number of elements remaining. The capacity of buffer does not need to be the same, and the index of the remaining data in the buffer does not have to be the same. However, the number of remaining elements in each buffer (from position to upper bound) must be the same.
    • The sequence of remaining data elements that should be returned by the Get () function in each buffer must be consistent.

Creating buffers
Assign a capacity of 100 char variables Charbuffercharbuffer charbuffer = charbuffer.allocate (+); char [] myArray = new char [100]; Charbuffer Charbuffer = Charbuffer.wrap (MyArray);//This code constructs a new buffer object, but the data element is present in the array. This means that changes to the buffer caused by invoking the put () function directly affect the array, and any changes to the array will also be visible to the buffer object. Charbuffer.wrap (array, offset, length); You can specify position and length

Buffers created by the allocate () or wrap () functions are usually indirect, with an indirect buffer using a backup array. HasArray () returns whether the buffer has an accessible backup array.

Copy buffer

Some of the API for replication buffers:

Public abstract class Charbuffer extends Buffer implements Charsequence, comparable {//This is a partial API listingpubli C Abstract Charbuffer Duplicate ();p ublic abstract Charbuffer asreadonlybuffer ();p ublic abstract Charbuffer Slice ();}

The duplicate () function creates a new buffer similar to the original buffer. Two buffers share data elements with the same capacity, but each buffer has its own position, upper bounds, and tag attributes. Changes to the data elements in one buffer are reflected in the other buffer. If the original buffer is read-only, or is a direct buffer, the new buffers inherit these properties.

The following example:

public static void Main (string[] args) {Charbuffer buffer = charbuffer.allocate (8); Buffer.position (3). Limit (6). Mark (). Position (5); Charbuffer Dupebuffer = Buffer.duplicate (); System.out.println (Dupebuffer.position ()); System.out.println (Dupebuffer.limit ());d upebuffer.clear (); System.out.println (Dupebuffer.position ()); System.out.println (Dupebuffer.limit ());} out5608

Asreadonlybuffer () function to generate a read-only buffer graph.

The code is described below:

public static void Main (string[] args) {Charbuffer buffer = charbuffer.allocate (8); Charbuffer Dupebuffer = Buffer.asreadonlybuffer (); System.out.println (Dupebuffer.isreadonly ());d upebuffer.put (' S ');//read-only buffer call throws exception}//outtrueexception in thread " Main "Java.nio.ReadOnlyBufferExceptionat Java.nio.HeapCharBufferR.put (heapcharbufferr.java:172) at Nio.test.TestMain.main (TESTMAIN.JAVA:10)

Slice () Creates a new buffer starting at the current position of the original buffer, and its capacity is the number of remaining elements of the original buffer (limit-position). This new buffer shares a sub-sequence of data elements with the original buffer. The separated buffers also inherit read-only and direct attributes.

public static void Main (string[] args) {Charbuffer buffer = charbuffer.allocate (8); Buffer.position (3). Limit (5); Charbuffer Slicebuffer = Buffer.slice ();}

byte buffers

In Java.nio, the byte order is encapsulated by the Byteorder class.

The Byteorder.nativeorder () method returns the hardware platform byte order that the JVM runs.

Direct buffers

Only byte buffers are eligible to participate in I/O operations.

The destination memory area of the I/O operation must be a contiguous sequence of bytes.

Direct buffers are used to interact with channels and intrinsic I/O routines.

Direct byte buffers are usually the best choice for I/O operations. The direct byte buffers support the most efficient I/O mechanisms available to the JVM. Non-direct byte buffers can be passed to the channel, but this can result in performance loss. Usually non-direct buffering cannot be the target of a local I/O operation. If a non-direct Bytebuffer object is passed to a channel for writing, the following action is called each time it is suppressed:

    1. Creates a temporary direct Bytebuffer object.
    2. Copies the contents of the non-direct buffer into a direct temporary buffer.
    3. Perform low-level I/O operations using a direct temporary buffer.
    4. The direct temporary buffer object leaves the scope and eventually becomes the useless data being returned.

The direct buffer is the best choice for I/O, but may cost more than creating a non-direct buffer. The memory used by the direct buffers is allocated by calling code on the local operating system, over the standard JVM.

Bytebuffer.allocatedirect () creates a direct buffer. Isdirect () returns whether the buffer is direct.

View Buffers

The view buffer is created by the working method of an existing buffer object instance. This Diagram object maintains its own properties, capacity, position, upper bounds, and tags, but shares data elements with the original buffer.

The Bytebuffer class allows the Create graph to map byte buffer byte data to other raw data types.

Public abstract Charbuffer Ascharbuffer ();p ublic abstract Shortbuffer asshortbuffer ();p ublic abstract Intbuffer ASINTBU Ffer ();p ublic abstract Longbuffer aslongbuffer ();p ublic abstract Floatbuffer asfloatbuffer ();p ublic abstract Doublebuf Fer Asdoublebuffer ();

Look at one of the following examples:

Bytebuffer Bytebuffer = bytebuffer.allocate (7). order (Byteorder.big_endian); Charbuffer Charbuffer = Bytebuffer.ascharbuffer ();

public class Buffercharview {public static void main (String [] argv) throws Exception {Bytebuffer byte          Buffer = bytebuffer.allocate (7). order (Byteorder.big_endian);          Charbuffer Charbuffer = Bytebuffer.ascharbuffer ();          Load the Bytebuffer with some bytes bytebuffer.put (0, (byte) 0);          Bytebuffer.put (1, (byte) ' H ');          Bytebuffer.put (2, (byte) 0);          Bytebuffer.put (3, (byte) ' I ');          Bytebuffer.put (4, (byte) 0);          Bytebuffer.put (5, (byte) '! ');          Bytebuffer.put (6, (byte) 0);          println (Bytebuffer);       println (Charbuffer); }//Print info about a buffer private static void println (buffer buffer) {System.out.println ("pos=" + bu Ffer.position () + ", limit=" + buffer.limit () + ", capacity=" + buffer.capacity () + ":" + buffer.tostring () + "‘");}} The output of running the Buffercharview program is://pos=0, limit=7, capacity=7: ' java.nio.heapbytebuffer[pos=0 lim=7 cap=7] '//pos=0, limit=3, capacity=3: ' hi! 
Data element View

The Bytebuffer class provides access and conversion methods for each primitive data type:

Public abstract class Bytebuffer extends Buffer implements comparable {public abstract char GetChar ();p ublic Abstract Char GetChar (int index);p ublic abstract Short getshort ();p ublic abstract short getshort (int index);p ublic abstract int getInt ( );p ublic abstract int getInt (int index);p ublic abstract Long Getlong ();p ublic abstract long getlong (int index);p ublic Abst  Ract float getfloat ();p ublic abstract float getfloat (int index);p ublic abstract Double getdouble ();p ublic abstract Double getdouble (int index);p ublic abstract Bytebuffer putchar (char value);p ublic abstract bytebuffer putchar (int index, char VA Lue);p ublic abstract Bytebuffer putshort (short value);p ublic abstract bytebuffer putshort (int index, short value);p ublic Abstract Bytebuffer putint (int value);p ublic abstract bytebuffer putint (int index, int value);p ublic abstract Bytebuffer p Utlong (Long value);p ublic abstract bytebuffer putlong (int index, Long value);p ublic abstract bytebuffer putfloat (float VA Lue);p ublic abstract BytebuFfer putfloat (int index, float value);p ublic abstract Bytebuffer putdouble (double value);p ublic abstract Bytebuffer putdo uble (int index, double value);}

If a bytebuffer is in the following state:

then int value = Buffer.getint ();

The actual return value depends on the current bit ordering (Byte-order) setting of the buffer. The more specific wording is:

int value = Buffer.order (Byteorder.big_endian). GetInt ();

This will return the value 0x3bc5315e, while:

int value = Buffer.order (Byteorder.little_endian). GetInt ();

return value 0x5e31c53b

If the original type you are trying to get requires more bytes than the number of bytes present in the buffer, Bufferunderflowexception will be thrown.

Java NIO Buffers

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.