September 2, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Understanding the Buffer class in Java

  • June 17, 2002
  • By Richard G. Baldwin
  • Send Email »
  • More Articles »

Flip the buffer

Before we can use the showBufferData method to display the contents of the buffer, we must do something about the position property whose value is currently 8.  There are several ways to do this, but I took this opportunity to illustrate the use of the flip method of the Buffer class.  The use of the flip method is shown in Listing 9.
 

    System.out.println(
        "Flip the buffer");
    buf.flip();

Listing 9

According to Sun, the flip method "makes a buffer ready for a new sequence of ... relative get operations.  It sets the limit to the current position and then sets the position to zero."

That is exactly what I needed to do in this case, so the flip method worked quite nicely.

Display buffer properties and data

Listing 10 displays the new property values for the buffer, and then displays the contents of the buffer.
 

    showBufferProperties(buf);
    showBufferData(buf);

Listing 10

The output produced by Listings 8, 9, and 10 is shown in Figure 5.
 

Modify first array element
Show array data
10 1 2 3 4 5 6 7
Flip the buffer
Buffer Properties:
 capacity=8 limit=8 position=0
Show buffer data
10 1 2 3 4 5 6 7

Figure 5

The important things to note in Figure 5 are:

  • Invocation of the flip method caused the position property value to be set to zero.
  • The value in the first element of the buffer was changed when the value in the first element of the wrapped array was changed.
Rewind the buffer

A little later, I will illustrate that changing the contents of the buffer causes the corresponding contents of the wrapped array to change accordingly.  First, however, I need to illustrate the rewind operation on the buffer.  This is accomplished in Listing 11.
 

    System.out.println(
                  "Rewind the buffer");
    buf.rewind();
    showBufferProperties(buf);
    showBufferData(buf);

Listing 11

The code in Listing 11 invokes the rewind method on the buffer and then displays the properties and contents of the buffer.

The rewind method is a method of the Buffer class.  According to Sun, the rewind method "makes a buffer ready for re-reading the data that it already contains: It leaves the limit unchanged and sets the position to zero."  You might equate this to rewinding a VCR tape in order to play it again.

And the output is ...

The code in Listing 11 produces the output shown in Figure 6.
 

Rewind the buffer
Buffer Properties:
 capacity=8 limit=8 position=0
Show buffer data
10 1 2 3 4 5 6 7

Figure 6

There are no surprises here.  By now, you probably knew what to expect as output from this operation.

The absolute put method

As I explained earlier, the ByteBuffer class provides both absolute and relative versions of the put and get methods.  So far, we have seen the use of the relative version of the get method only.  The boldface code in Listing 12 uses the absolute version of the put method to modify the contents of the buffer at index 3.  In particular, the value stored at index 3 in the buffer is overwritten by the value 20.
 

    System.out.println(
            "Modify the buffer using");
    System.out.println(
                "absolute put method");
    buf.put(3,(byte)20);
    buf.rewind();
    showBufferData(buf);
    showArrayData(array);

Listing 12

After the value is modified, the buffer is rewound.  Then the data in the buffer and the data in the array are displayed for comparison.  The output produced by Listing 12 is shown in Figure 7.
 

Modify the buffer using
absolute put method
Show buffer data
10 1 2 20 4 5 6 7
Show array data
10 1 2 20 4 5 6 7

Figure 7

Perhaps the most important things to observe in this output are:

  • The value of the element at index 3 in the buffer is changed to 20.
  • The value of the element at index 3 in the array is also changed to 20.




Page 5 of 7



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel