Java Reference
In-Depth Information
specific example. First consider an operation that writes data from the buffer to a file. This is illustrated in
Figure 10-2 .
FIGURE 10-2
When a file write operation is executed by a channel using a given ByteBuffer , elements from the buffer
are written to the file starting at the index specified by the position. Successive bytes are written to the file
up to, and including, the byte at index position limit-1 . Thus with the buffer shown in Figure 10-2 , 60
bytes are written to the file. When you want to write all the data from a buffer, you should set the buffer
position to 0 and the limit to the buffer capacity. In this case the limit is an index value that is one beyond
the index value for the last byte in the buffer, so limit-1 refers to the last byte.
For a read operation, data that is read from the file is stored in a ByteBuffer starting at the byte at the
index given by the buffer's position. Assuming that the end of the file is not reached before all the bytes
are read, bytes continue to be read up to and including the byte at the index limit-1 . Thus, the number of
bytes read is limit-position , and the bytes are stored in the buffer from the byte at position up to and
including the byte at limit-1 .
As I said at the beginning of this section, the position and limit are involved when you load data into
a buffer before writing it to a file or retrieving data from it after reading from a file. This applies for any
type of buffer. The position specifies where the next value is inserted in a buffer or retrieved from it. As you
see later, the position is usually automatically incremented to point to the next available position when you
insert or extract values in a buffer. The limit acts as a constraint to indicate where the data in a buffer ends,
a bit like an end-of-file marker. You cannot insert or extract elements beyond the position specified by the
limit.
Because a buffer's position is an index, it must be greater than or equal to zero. You can also deduce that
it must also be less than or equal to the limit. Clearly, the limit cannot be greater than the capacity of a buffer.
Otherwise, you could be trying to write elements to positions beyond the end of the buffer. However, as you
have seen, it can be equal to it. These relationships can be expressed as the following:
0 <Symbol>≤</Symbol> position <Symbol>≤</Symbol> limit <Symbol>≤</Symbol> capacity
As a general rule, if your code attempts to do things directly or indirectly that result in these relationships
being violated, an exception is thrown.
When you create a new buffer that is not a view buffer, its capacity is fixed at the value that you specify. It
also has a position of zero and its limit is set to its capacity. When you create a view buffer from an existing
ByteBuffer , the contents of the view buffer start at the current position for the ByteBuffer . The capacity
and limit for the view buffer are set to the limit for the original buffer, divided by the number of bytes in an
element in the view buffer. The limit and position for the view buffer are subsequently independent of the
limit and position for the original buffer.
 
 
Search WWH ::




Custom Search