Java Reference
In-Depth Information
capacity = 32
ByteBuffer
element
element
element
element
element
element
element
element
IntBuffer
capacity = 4
element
element
element
element
DoubleBuffer
capacity = 16
element
element
element element
element element element
element
element element
element element element
element element
CharBuffer
These buffers have different capacities but occupy the same amount of memory
Of course, for a buffer that stores bytes, the capacity will be the maximum number of bytes it can hold,
but for a buffer of type DoubleBuffer for instance that stores double values, the capacity will be the
maximum number of double values you can put in it. Elements in a buffer are indexed from zero so
the index position for referencing elements in a buffer runs from 0 to capacity-1 .
Buffer Position and Limit
A buffer also has a limit and a position , both of which affect read/write operations executed by a
channel using the buffer.
The position is the index position of the next buffer element to be read or written. This sounds a little
strange, but keep in mind that a buffer can be for file input or output. For example, with a buffer used
for output, the position identifies the next element to be written to the file. For a buffer used for file
input, the position identifies where the next element read from the file will be stored.
Search WWH ::




Custom Search