Java Reference
In-Depth Information
If you have not set the mark, or if it has been discarded by an operation to set the limit or the position, the
reset() method throws an exception of type InvalidMarkException . The mark for a view buffer operates
independently of the mark for the buffer from which it was created.
You probably don't need to mark a buffer most of the time. The sort of situation where you could use it
is where you are scanning some part of a buffer to determine what kind of data it contains — after reading a
file, for example. You could mark the point where you started the analysis, and then return to that point by
calling reset() for the buffer when you have figured out how to handle the data.
Buffer Data Transfers
Of course, before you can write the contents of a buffer to a file, you must load the buffer with the data.
Methods for loading data into a buffer are referred to as put methods. Similarly, when you have read data
from a file into a buffer, you use the buffer's get methods to retrieve the data.
Two kinds of operations transfer data values to or from a buffer.
• A relative put or get operation transfers one or more values starting at the buffer's current position.
In this case, the position is automatically incremented by the number of values transferred.
• In an absolute put or get operation, you explicitly specify an index for the position in the buffer
where the data transfer is to begin. In this case the buffer's position is not updated, so it remains at
the index value it was before the operation was executed.
Transferring Data into a Buffer
The ByteBuffer class and all the view buffer classes have two put() methods for transferring a single value
of the buffer's type to the buffer. One is a relative put() method that transfers an element to a given index
position in the buffer, and the other is an absolute put method that places the element at an index position
that you specify as an argument. All the buffer classes also have three relative put() methods for bulk trans-
fer of elements of the given type. Let's consider the put() methods for a ByteBuffer object that are shown
in Table 10-7 as an example.
TABLE 10-7 : ByteBuffer Class put() Methods
METHOD
DESCRIPTION
Transfers the byte specified by the argument to the buffer at the current position and increments the
position by 1. An exception of type BufferOverflowException is thrown if the buffer's position is
not less than its limit.
put(byte b)
Transfers the byte specified by the second argument to the buffer at the index position specified by
the first argument. The buffer position is unchanged. An exception of type IndexOutOfBoundsEx-
ception is thrown if the index value is negative or greater than or equal to the buffer's limit.
put(int index,
byte b)
Transfers all the elements of array to this buffer starting at the current position. The position is in-
cremented by the length of the array. An exception of type BufferOverflowException is thrown if
there is insufficient space in the buffer to accommodate the contents of the array.
put(byte[] ar-
ray)
Transfers bytes from array[offset] to array[offset+length-1] inclusive to the buffer. If there
is insufficient space for them in the buffer, an exception of type BufferOverflowException is
thrown.
put(byte[] ar-
ray,
Transfers the bytes remaining in src to the buffer. This is src.remaining() elements from the buf-
fer src from its position index to limit-1 . If there is insufficient space to accommodate these, then
an exception of type BufferOverflowException is thrown. If src is identical to the current buffer
put(ByteBuffer
src)
 
 
Search WWH ::




Custom Search