March 1, 2021
Hot Topics:

Understanding the Buffer class in Java

  • By Richard G. Baldwin
  • Send Email »
  • More Articles »
Interaction 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
A new buffer always has a position of zero and a mark that is undefined. The initial limit and capacity of a new buffer depend on the type of the buffer and its construction.

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.
As you can see, unlike JavaBeans design patterns, the difference between setting and getting in this case is based on overloading the method name.  This is typical of the setter and getter methods for each of the three properties listed earlier.

Method chaining

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.


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.
I will illustrate most of these methods in the sample program later in this lesson.

Read-only buffers

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.

Page 2 of 7

This article was originally published on June 17, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date