Understanding the Buffer class in Java, Page 2Interaction rules
Here are some rules that apply to the interaction of mark, position, limit, and capacity:
- The mark is not always defined
- When the mark is defined,
- The value of the mark is never negative
- The value of the mark is never greater than the position
- The mark is discarded when the position or the limit is adjusted to a value smaller than the mark
- 0 <= mark <= position <= limit <= capacity
- Invoking the method named mark sets the buffer's mark to its current position
- When the mark is not defined, invoking the reset method causes an exception to be thrown
Setter and getter methods
The Buffer class provides methods for setting and getting the values of the position, and limit properties, and for getting the value of the capacity property. However, these methods do not conform to JavaBeans design patterns for properties. For example, here are descriptions of the methods for getting and setting the limit property of a buffer:
- limit() - Returns this buffer's limit as type int.
- limit(int newLimit) - Sets this buffer's limit and returns type Buffer.
Note that the second method described above returns a reference to the
buffer. Other methods of the Buffer class also return a reference
to the buffer. This makes it possible to use method invocation
chaining syntax such as that shown in Figure 1.
buf.rewind().position(2).mark(); Figure 1
I will have more to say about this later.
Other useful methods
The Buffer class defines several other methods that can be used to operate on a buffer, including the following. The behaviors of these methods, (with respect to changing the values of position and limit), are very important.
- reset - Resets this buffer's position to the previously-marked position.
- clear - Clears the buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded.
- flip - The limit is set to the current position and the position is set to zero. The mark is discarded.
- rewind - Rewinds the buffer. The position is set to zero, the mark is discarded, and the limit is unchanged.
- hasRemaining - Returns boolean to tell whether there are any elements between the current position and the limit.
- remaining - Returns the number of elements between the current position and the limit.
It is possible to create buffers that are readable but not writable.
(For example, see the documentation for the asReadOnlyBuffer method of the ByteBuffer class.)Methods that normally change the contents of a buffer will throw a ReadOnlyBufferException when invoked on a read-only buffer.
While a read-only buffer does not allow its content to be changed, its mark, position, and limit values may be changed. You can determine if a buffer is read-only by invoking its isReadOnly method.
The ByteBuffer class
As mentioned earlier, the ByteBuffer class extends the Buffer class, and as such, inherits the capabilities discussed above. In addition, the ByteBuffer class provides new capabilities that are not defined in the Buffer class.
I will use the ByteBuffer class in a sample program to illustrate the features inherited from the Buffer class. I encourage you to compile and execute this sample program, and to experiment with it by making changes while observing the results of your changes.
Reading and writing single bytes
The ByteBuffer class inherits numerous features from the Buffer
class, and adds new features of its own. I will discuss the new features
added by the ByteBuffer class in a future lesson. In this
lesson, I will concentrate on the features inherited from the Buffer
class, and will limit the use of ByteBuffer features to those required
to illustrate the inherited features.