Understanding the Buffer class in JavaChaining and marking
As explained earlier, several of the methods of the Buffer class
return a reference to the buffer. This makes it possible to chain
method invocations as shown by the boldface code in Listing 13.
System.out.println( "Mark at index 2 using chaining"); buf.rewind().position(2).mark(); Listing 13
The boldface statement in Listing 13 is executed from left to right. The behavior of the statement accomplishes the following operations in order:
- Rewind the buffer
- Set the value of the position property to 2.
- Set the mark to the current value of the position property (2).
System.out.println( "Set position to 4"); buf.position(4); showBufferData(buf); Listing 14
Having changed the value of the position property, Listing 14
invokes the showBufferData method to display the contents of the
buffer. The screen output is shown in Figure 8.
|Mark at index 2 using chaining
Set position to 4
Show buffer data
4 5 6 7
Recall that the showBufferData method displays the data from the current position to the limit. Therefore, in this case, the display does not begin with the element at index zero. Rather, it begins with the element at index 4, which is the current value of the position property.
Reset to a previous mark
Listing 15 invokes the reset method on the buffer, and then displays
its contents again.
System.out.println( "Reset to previous mark"); buf.reset(); showBufferData(buf); Listing 15
The reset method is a method of the buffer class. According to Sun, invocation of the reset method "Resets this buffer's position to the previously-marked position."
Recall that the previously marked position was the element at index
value 2. Thus, when the showBufferData method is used to display
the contents of the buffer, the screen output is as shown in Figure 9.
|Reset to previous mark
Show buffer data
2 20 4 5 6 7
The output shows that the value of the position property is set to element index 2 when the reset method is invoked. Then the showBufferData method displays the contents of the buffer from index 2 to the limit.
Thus, the ability to mark and reset makes it possible for your program to remember a position and return to that position later. You need to exercise caution, however, because several of the operations that you can perform on a buffer cause the mark to be discarded. If you invoke reset on a buffer for which the mark has been discarded, an exception will be thrown.
Is this a read-only buffer?
Although it is possible to create read-only buffers, the output produced
by the code in Listing 16 shows that this is not a read-only buffer.
System.out.println( "Buffer is read only: " + buf.isReadOnly()); Listing 16
Listing 16 produces the output shown in Figure 10.
Buffer is read only: false Figure 10
So there you have it
By now you should understand a lot about the new Buffer class in the new java.nio package.
Future articles will discuss ByteBuffer, Channels, and
other new I/O features introduced in Java version 1.4.0.
Page 6 of 7