October 22, 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 »

The put and get methods

The abstract Buffer class does not provide methods for storing or retrieving data from a buffer.  Rather, that capability is provided by ByteBuffer and other subclasses of Buffer.

The ByteBuffer class provides get and put methods for reading and writing single bytes in both an absolute and a relative sense.  These features, along with others, are illustrated in the program named Buffer01, which I will discuss in fragments.  A complete listing of the program is provided in Listing 17 near the end of the lesson.

The sample program named Buffer01

The sample program named Buffer01 illustrates most of the methods of the Buffer class and some of the methods of the ByteBuffer class.

Listing 1 shows the beginning of the controlling class named Buffer01.  Listing 1 also shows the import directive used by this program, to remind you that this is a new API for input/output.
 

import java.nio.*;

class Buffer01{

Listing 1

The java.nio package did not exist prior to the release of version 1.4.0.  Therefore, in order to compile and execute this sample program, you will need to have version 1.4.0 or later installed on your system.

Displaying buffer properties

Listing 2 shows the first of three convenience methods designed to make the code in the main body of the program simpler and easier to understand.
 

  static void showBufferProperties(
                           Buffer buf){
    System.out.println(
       "Buffer Properties: "
                    +"\n  capacity=" 
                    + buf.capacity()
                    + " limit=" 
                    + buf.limit()
                    + " position=" 
                    + buf.position());
  }//end showBufferProperties

Listing 2

The purpose of the method named showBufferProperties is to get and display the capacity, limit, and position properties of a Buffer object whose reference is received as an incoming parameter.

(The actual type of the objects passed to this method in this program is ByteBuffer.  However, because ByteBuffer extends Buffer, and all the methods invoked by the code in this method are defined in Buffer, the type of the incoming object can be Buffer or any subclass of Buffer.)
The boldface code in Listing 2 identifies the getter methods used to get the values of the three properties listed above.  The remaining code in Listing 2 is a large print statement that causes the values returned by the getter methods to be displayed on the standard output device.  A sample of the screen output produced by this method is shown in Figure 2.
 
Buffer Properties:
  capacity=8 limit=8 position=0

Figure 2

Display buffer data

The method shown in Listing 3 gets and displays the data stored in the buffer beginning with the element at the current value of the position property and extending to the value of the limit property.
 

  static void showBufferData(
                       ByteBuffer buf){
    System.out.println(
                   "Show buffer data");
    while(buf.hasRemaining())
      System.out.print(
                      buf.get() + " ");
    System.out.println();//blank line
  }//end showBufferData

Listing 3

This is accomplished using the relative get method defined in the ByteBuffer class.

Parameter is not type Buffer

Note that the incoming parameter to this method is type ByteBuffer and is not type Buffer, as was the case in Listing 2.  This is because the get method invoked by the code in this method is not defined in the Buffer class.  Rather, it is defined in the ByteBuffer class, and therefore can only be invoked on a reference of type ByteBuffer, or a subclass of the ByteBuffer class.

The hasRemaining method

The code in Listing 3 invokes two interesting methods.  The first is the hasRemaining method.  This method is much like the methods of the Iterator and Enumeration interfaces, used to iterate on objects instantiated from the concrete classes of the Java Collections Framework.

The hasRemaining method is defined in the Buffer class, and tells whether there are any elements remaining between the current position and the limit.  This method returns a boolean, which is true only if there is at least one element remaining in the buffer.  Thus, it works very nicely in the conditional clause of a while loop for the purpose of iterating on a buffer.

The relative get method

The second method of interest in Listing 3 is the relative get method of the ByteBuffer class.  This method reads and returns the byte at the buffer's current position, and then increments the position.  Thus, it also works quite well in an iterator loop for a buffer (provided you have exercised proper control over the values of the position and limit properties beforehand).

Display array data

The third convenience method, shown in Listing 4, is a method designed simply to display the data in an array object of type byte.
 

  static void showArrayData(
                         byte[] array){
    System.out.println(
                    "Show array data");
    for(int cnt = 0; 
            cnt < array.length; cnt++){
      System.out.print(
                     array[cnt] + " ");
    }//end for loop
    System.out.println();//blank line
  }//end showArrayData

Listing 4

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.  If that is not the case, you can learn about array objects at Baldwin's Java Programming Tutorials.



Page 3 of 7



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel