Java Reference
In-Depth Information
System.out.println("The buffer can accommodate " + buf.remaining() +
" more elements.");
Of course, the value returned by the
remaining()
method is the same as the expression
buf.limit()-
buf.position()
.
Creating Buffers
None of the classes that define buffers have public constructors available. Instead, you use a static factory
method to create a buffer. You typically create a buffer object of type
ByteBuffer
by calling the static
al-
locate()
method for the class. You pass a value of type
int
as an argument to the method that defines the
capacity of the buffer — the maximum number of bytes that the buffer must accommodate. For example:
ByteBuffer buf = ByteBuffer.allocate(1024); // Buffer of 1024 bytes capacity
When you create a new buffer using the
allocate()
method for the buffer class, it has a position of zero,
and its limit is set to its capacity. The buffer that the preceding statement creates therefore has a position of
0 and has a limit and capacity of 1024.
You can also create other types of buffers in the same way. For example:
// Buffer stores 100 float values
FloatBuffer floatBuf = FloatBuffer.allocate(100);
This creates a buffer with a capacity to store 100 values of type
float
. Because each element occupies 4
bytes, the data in this buffer occupies 400 bytes. The buffer's initial position is 0, and its limit and capacity
is 100. Note that this is not a view buffer, but an independent buffer to store
float
values.
In practice, you are unlikely to want to create buffers other than
ByteBuffer
objects by calling the
static
allocate()
method because you cannot use them directly for I/O operations. You usually create a
ByteBuffer
object first and then create any view buffers that you need from the byte buffer.
Creating View Buffers
You can use a
ByteBuffer
object to create a buffer of any of the other types I have introduced and the new
buffer shares all or part of the memory that the original
ByteBuffer
uses to store data. Such a buffer is re-
ferred to as a
view buffer
because it provides a view of the contents of the byte buffer as elements of another
data type. Data is always transferred to or from a file as a series of bytes, but it typically consists of data
values of a mix of types other than type
byte
. A view buffer therefore has two primary uses: for loading
data items that are not of type
byte
into a
ByteBuffer
prior to writing it to a file, and for accessing data that
has been read from a file as values that are other than type
byte
.
You could create a view buffer of type
IntBuffer
from a
ByteBuffer
object like this:
ByteBuffer buf = ByteBuffer.allocate(1024); // Buffer of 1024 bytes capacity
IntBuffer intBuf = buf.asIntBuffer(); // Now create a view buffer
The content of the view buffer,
intBuf
, that you create here start at the byte buffer's current position,
which in this case is zero because it is newly created. The remaining bytes in
buf
are effectively shared with
the view buffer. At least, the maximum number of them that is a multiple of 4 , because
intBuf
stores ele-
ments of type
int
that require 4 bytes each. The view buffer has an initial position of 0 and has a capacity
and limit of 256. This is because 256 elements of type
int
completely fill the 1024 bytes remaining in
buf
.