Java Reference
In-Depth Information
You can create a buffer of a particular kind in many ways. You can create a buffer by using the allocate() factory
method of a particular buffer class as follows:
// Create a byte buffer with the capacity as 8
ByteBuffer bb = ByteBuffer.allocate(8);
// Assigns 8 to the capacity variable
int capacity = bb.capacity();
// Create a character buffer with the capacity as 1024
CharBuffer cb = CharBuffer.allocate(1024);
A byte buffer gets special treatment in NIO. It has an extra method called allocateDirect() that creates a byte
buffer. This method creates a byte buffer for which the memory is allocated from the operating system memory, not from
the JVM heap. This avoids copying the contents to intermediate buffers during I/O operations. A direct buffer has an
additional creation cost. However, it is faster during an I/O operation. You should use a direct byte buffer when a buffer is
long-lived. You can use the isDirect() method of the ByteBuffer class to check if a buffer is direct or non-direct.
// Create a direct byte buffer of 512 bytes capacity
ByteBuffer bbd = ByteBuffer.allocateDirect(512);
Another way to create a buffer is to wrap an array using the buffer's static wrap() method, like so:
// Have an array of bytes
byte[] byteArray = new byte[512];
// Create a byte buffer by wrapping the byteArray
ByteBuffer bb = ByteBuffer.wrap(byteArray);
You can use the same technique to create a buffer to store other primitive values. I will discuss other ways of
creating a buffer later in this section.
When you create a buffer, all elements of the buffer are initialized to a value of zero. Each element of a buffer has
an index. The first element has an index of 0 and the last element has an index of capacity - 1 .
Position and limit are two properties of a buffer. When a buffer is created, its position is set to 0 and its limit is
equal to its capacity. Figure 9-2 shows the state of a buffer with a capacity of 8 just after its creation. All its elements
have a value of 0. Its position is set to zero. Its limit is set to 8, which is equal to its capacity. In the figure, P and L
denote the position and the limit of the buffer, respectively. Note that the figure shows the index at 8, which is out of
range for the buffer, to show the value of the limit.
0
0
0
0
0
0
0
0
Buffer Elements >>
Element's Index >>
012345678
P
L
Figure 9-2. A buffer of capacity 8 after its creation
You can get/set the position of a buffer using its overloaded position() method. The position() method returns
the current value of the position of a buffer. The position(int newPosition) method sets the position of the buffer to
the specified newPosition value and returns the reference of the buffer.
You can get/set the limit of a buffer using its overloaded limit() method. The limit() method returns the
current value of the limit of a buffer. The limit(int newLimit) method sets the limit of a buffer to the specified
newLimit value and returns the reference of the buffer.
 
 
Search WWH ::




Custom Search