April 16, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Understanding the Buffer class in Java, Page 4

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

The main method creates an array object

There are several ways to create and populate a buffer in Java.  One of those ways is to wrap an existing array object in a buffer.  To do that, you need an existing array object.  (I will discuss the other ways to create a buffer in a future lesson.)

Listing 5 shows the beginning of the main method.  The code in Listing 5 creates, populates, and displays an eight-element array object containing data of type byte.
 

  public static void main(
                        String[] args){
    System.out.println(
      "Create, populate, and display "
       + "\nan 8-element byte array");
    byte[] array = {0,1,2,3,4,5,6,7};
    showArrayData(array);

Listing 5

Again, I am assuming that you are already familiar with the use of array objects in Java, and therefore, I won't discuss this code in detail.  The code in Listing 5 produces the output shown in Figure 3.
 

Create, populate, and display
an 8-element byte array
Show array data
0 1 2 3 4 5 6 7

Figure 3

I show this here because we will want to compare it with the data stored in our buffer object later.

Create the buffer

As mentioned above, there are several ways to create a buffer, and one of them is shown in Listing 6.
 

    System.out.println(
                 "Wrap the byte array "
                      + "in a buffer");
    ByteBuffer buf = 
                ByteBuffer.wrap(array);

Listing 6

Listing 6 invokes the static wrap method of the ByteBuffer class to create a buffer that wraps an existing array object.

What is the significance of wrapping an array object?

There are two overloaded versions of the wrap method, one that requires incoming offset and length parameters, and one that does not.  (Both versions require an incoming reference to an array object.)  I used the simpler of the two versions, which does not require offset and length.

For both versions, the new buffer is backed up by, or connected to, the byte array, which it wraps.  Modifications to the buffer cause the array contents to be modified.  Modifications to the array cause the buffer contents to be modified.  (It appears as though they are really the same set of data.)

For the version of the wrap method that I used, the capacity and limit of the new buffer is the same as array.length.  The initial value of the position property of the new buffer is zero, and its mark is undefined.

For the more complex version, the initial values of the buffer properties are determined by the values of the offset and length parameters passed to the wrap method.

Show buffer properties and data

The code in Listing 7 displays the properties of the new buffer.  Then it uses the relative get method to display the contents of the buffer.  After that, it displays the properties again.
 

    showBufferProperties(buf);
    showBufferData(buf);
    showBufferProperties(buf);

Listing 7

The output produced by the code in Listing 7 is shown in Figure 4.
 

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

Figure 4

There are several important things to note about this output:

  • The initial values of the buffer properties match that described above for the simpler version of the Wrap method.
  • The contents of the buffer match the contents of the array object displayed in Figure 3.
  • The value of the position property changes from 0 to 8 when the relative get method is used in an iterative loop to display each element in the buffer.
Modifications to the buffer ...

I stated earlier, "Modifications to the buffer cause the array contents to be modified, and modifications to the array cause the buffer contents to be modified."

This is illustrated in the next several fragments.
 

    System.out.println(
         "Modify first array element");
    array[0] = 10;
    showArrayData(array);

Listing 8

The code in Listing 8 changes the value in the first array element from 0 to 10, and then displays the modified contents of the array object.  We will see that this causes the value of the first element in the buffer to change accordingly.



Page 4 of 7



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel