Java Reference
In-Depth Information
In each case, the view buffer's contents start at the current position of the original byte buffer. The
position of the view buffer itself is initially set to zero, and its capacity and limit are set to the number of
bytes remaining in the original byte buffer divided by the number of bytes in the type of element that
the view buffer holds. The diagram below illustrates a view buffer of type IntBuffer that is created
after the initial position of the byte buffer has been incremented by 2, possibly after inserting a char
value into the byte buffer:
capacity = 20
position = 2
limit = 12
Byte Buffer buf
buf .asIntBuffer()
IntBuffer
position = 0
limit = 2
capacity = 2
Creating a View Buffer
You can create as many view buffers from a buffer of type ByteBuffer as you want, and they can
overlap or not as you require. A view buffer always maps to bytes in the byte buffer starting at the
current position. You will frequently want to map several different view buffers to a single byte buffer
each providing a view of a different segment of the byte buffer:
ByteBuffer buf capacity = 20
buf .asIntBuffer()
buf .asCharBuffer()
IntBuffe capacity = 2r
Char Buffer capacity = 6
A Byte Buffer with two View Buffers
Search WWH ::




Custom Search