September 23, 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 »

Run the Program

If you haven't already done so, I encourage you to copy the code from Listing 17 into your text editor, compile it, and execute it.  Experiment with it, making changes, and observing the results of your changes.

Remember, however, that you must be running version 1.4.0 or later to compile and execute this program.

Complete Program Listing

A complete listing of the program is shown in Listing 17 below.
 
/* File Buffer01.java
Copyright 2002, R.G.Baldwin

Illustrates most methods of Buffer 
class and some methods of ByteBuffer
class.

Tested using JDK 1.4.0 under Win2000

Output is:

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

Wrap the byte array in a buffer
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

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

Rewind the buffer
Buffer Properties:
  capacity=8 limit=8 position=0
Show buffer data
10 1 2 3 4 5 6 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

Mark at index 2 using chaining
Set position to 4
Show buffer data
4 5 6 7
Reset to previous mark
Show buffer data
2 20 4 5 6 7

Buffer is read only: false
**************************************/

import java.nio.*;

class Buffer01{
  static void showBufferProperties(
                           Buffer buf){
    System.out.println(
       "Buffer Properties: "
                    +"\n  capacity=" 
                    + buf.capacity()
                    + " limit=" 
                    + buf.limit()
                    + " position=" 
                    + buf.position());
  }//end showBufferProperties
  //---------------------------------//
  
  static void showBufferData(
                       ByteBuffer buf){
    //Displays buffer contents from 
    // current position to limit using
    // relative get method.
    System.out.println(
                   "Show buffer data");
    while(buf.hasRemaining())
      System.out.print(
                      buf.get() + " ");
    System.out.println();//blank line
  }//end showBufferData
  //---------------------------------//

  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
  //---------------------------------//
  
  public static void main(
                        String[] args){
    
    //Wrap a byte array into a buffer
    System.out.println(
      "Create, populate, and display "
       + "\nan 8-element byte array");
    byte[] array = {0,1,2,3,4,5,6,7};
    showArrayData(array);
    System.out.println();//blank line
    
    System.out.println(
                 "Wrap the byte array "
                      + "in a buffer");
    ByteBuffer buf = 
                ByteBuffer.wrap(array);
    showBufferProperties(buf);
    showBufferData(buf);
    showBufferProperties(buf);
    System.out.println();//blank line
        
    //Mods to the buffer will cause the
    // array to be modified and vice 
    // versa.
    System.out.println(
         "Modify first array element");
    array[0] = 10;
    showArrayData(array);

    System.out.println(
                    "Flip the buffer");
    buf.flip();
    showBufferProperties(buf);
    showBufferData(buf);
    System.out.println();//blank line
    
    System.out.println(
                  "Rewind the buffer");
    buf.rewind();
    showBufferProperties(buf);
    showBufferData(buf);
    System.out.println();//blank line

    System.out.println(
            "Modify the buffer using");
    System.out.println(
                "absolute put method");
    buf.put(3,(byte)20);
    buf.rewind();
    showBufferData(buf);
    showArrayData(array);
    System.out.println();//blank line
    
    //Illustrate chaining, marking, 
    // and reset
    System.out.println(
     "Mark at index 2 using chaining");
    buf.rewind().position(2).mark();
    System.out.println(
                  "Set position to 4");
    buf.position(4);
    showBufferData(buf);
    System.out.println(
             "Reset to previous mark");
    buf.reset();
    showBufferData(buf);
    System.out.println();//blank line
    
    System.out.println(
                "Buffer is read only: "
                   + buf.isReadOnly());
  }// end main

}//end class Buffer01 definition

Listing 17


Copyright 2002, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java, C#, and XML. In addition to the many platform and/or language independent benefits of Java and C# applications, he believes that a combination of Java, C#, and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects, and he frequently provides onsite training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Programming Tutorials, which has gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

# # #





Page 7 of 7



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel