Buffer in Java NIO (2) create/copy a Buffer, niobuffer
How to create a buffer
You can create a buffer in either of the following ways:
1. Call the allocate Method
2. Call the wrap Method
The following uses charBuffer as an example to describe the meaning of each method;
Create a buffer using the allocate Method
Calling the allocate method will actually return the new HeapCharBuffer (capacity, capacity) object;
The cache space is stored in the member attribute char [] hb array of the CharBuffer class, that is, in the JVM heap;
In the following example, A CharBuffer with a capacity of 10 is created:
CharBuffer bf = CharBuffer.allocate(10);
The allocate method is actually relatively simple, but it should be noted that the allocate method can allocate both JVM heap space and direct memory space (such as ByteBuffer, you can call the allocateDirect method to allocate Direct Memory.) The internal function is to call the unsafe method. the allocatemory method implements direct memory allocation. This space is not in the JVM heap and will be described in more detail later. However, you can remind me that if it is a direct memory space, hasArray () is called () the method returns false;
Use the wrap method to create a buffer
Calling the wrap method will actually return the new HeapCharBuffer (array, offset, length) object;
The difference between the allocate method and the allocate method is that its cache storage space is passed in externally;
In the following example, A CharBuffer with a capacity of 10 is created:
char[] myArray = new char[10]; CharBuffer charbuffer = CharBuffer.wrap(myArray);
In addition, wrap also has an overload method: the wrap () method with offset and length as the parameter. The following is an example of this method:
char[] myArray = new char[10]; CharBuffer charbuffer = CharBuffer.wrap (myArray, 2, 3);
The above Code creates a Buffer with position = 2, limit = 5, capacity = 10;
Replication buffer method
There are three main ways to copy the buffer:
1. Call the duplicate Method
2. Call the asReadOnlyBuffer method.
3. Call the slice Method
The following uses charBuffer as an example to describe the meaning of each method;
Duplicate method replication Buffer
Calling the duplicate method will actually create a copy of the original cache area. It is not a deep copy, it is a shallow copy. What does it mean that these two cache areas will share data elements, however, the upper bound, capacity, and location attributes of each cache area are independent of each other;
Modifying the elements in one cache area affects the other copy cache area, as shown in the following example:
CharBuffer charbuffer1 = CharBuffer.allocate(10); CharBuffer charbuffer2 = charbuffer1.duplicate(); charbuffer1.put('a').put('b').put('c'); charbuffer1.flip(); System.out.println(charbuffer1); System.out.println(charbuffer2);
The charbuffer2 cache area copies the charbuffer1 cache area. At the beginning and end, we only operate on the charbuffer1 cache area. When printing it, we found that charbuffer2 cache area already contains charbuffer1 data, the result is printed as follows:
Abc
AbcasReadOnlyBuffer
Calling the asReadOnlyBuffer method will generate a read-only cache area, which is basically the same as calling the duplicate method. The only difference is that the cache area is read-only. If you put it, it will throw ReadOnlyBufferException;
Example:
CharBuffer charbuffer1 = CharBuffer.allocate(10); CharBuffer charbuffer2 = charbuffer1.asReadOnlyBuffer(); charbuffer1.put('a').put('b').put('c'); charbuffer1.flip(); System.out.println(charbuffer1); System.out.println(charbuffer2); charbuffer2.put('c');//ReadOnlyBufferException
Output result:
Abc
Abc
Exception in thread "main" java. nio. ReadOnlyBufferException
At java. nio. HeapCharBufferR. put (HeapCharBufferR. java: 166)
At nio. Main. main (Main. java: 21) Server Load balancer method copy Buffer
The slice method is used to split the cache. This method creates a new buffer starting from the current position of the original buffer, and its capacity is the number of remaining elements of the original buffer (limit-position );
This cache area shares a sequence with the original cache area;
Example:
1. Create a cache zone charbuffer1 with a capacity of 10.
CharBuffer charbuffer1 = CharBuffer.allocate(10);
In this case, mark =-1; position = 0; limit = 10; capacity = 10;
2. Modify the position and limit values of charbuffer1.
charbuffer1.position(2).limit(5);
In this case, mark =-1; position = 2; limit = 5; capacity = 10;
3. Call the slice Method to split the charbuffer1 cache.
CharBuffer charbuffer2 = charbuffer1.slice();
At this point:
Charbuffer1: mark =-1; position = 2; limit = 5; capacity = 10;
Charbuffer2: mark =-1; position = 0; limit = 3; capacity = 3;
References
Java NIO