Buffers in Java

Discussion in 'Java' started by pradeep, Dec 28, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in

    Introduction



    Buffers can be very useful in Java since they speed up I/O operations considerably. Basically a buffer is a space allocated in memory for Bytes, Chars, and other data types to be stored. Buffers are really useful while writing Client/ Server applications.

    Creating a Buffer



    A Buffer is created in the following way:
    Code:
    /*
     * Buffer size
     */
    int BUFFER_SIZE = 100;
    
    /*
     * Allocates a ByteBuffer with a size of a 100
     */
    ByteBuffer byteBuffer = ByteBuffer.allocate (BUFFER_SIZE);
    
    /*
     * Makes an IntBuffer
     */
    IntBuffer intBuffer = IntBuffer.allocate (BUFFER_SIZE);
    
    /*
     * Makes a Direct CharBuffer
     */
    CharBuffer charBuffer = CharBuffer.allocateDirect (BUFFER_SIZE);
    

    Using the Buffer



    Buffer has three different markers, namely - position, limit and capacity.

    Capacity: Capacity is the number set with the allocate (BUFFER_SIZE). That is basically how big the buffer is.

    Limit: The Limit of the Buffer is the index of the first element that should not be read or written in the buffer. The limit of the buffer cannot be negative or greater that it’s capacity.

    Position: The position of the Buffer is the place that the buffer is the next element to be read/ written.

    The Different methods that edit the buffer just move those markers around. There are four methods that you should familiarize yourself with:

    Buffer.clear();
    Buffer.compact();
    Buffer.flip();
    Buffer.rewind();

    clear(); The clear method when applied to a buffer sets the limit to the capacity and the position the 0. All that that means that when new data is added to the buffer it will overwrite the old data.

    compact(); The compact method moves the elements between the current position and the limit to the beginning of the buffer.

    flip(); The flip method need to be called before reading the date from the buffer. When a flip is called the limit is set to the current position, and the position to 0.

    rewind(); The rewind method sets the position to zero again in case you want to make the buffer ready for another draining. You would need to flip the buffer first though.

    Direct vs. nonDirect Buffers



    Buffers can be either direct or nonDirect.

    Direct: creating a direct buffer simply means that the buffer is allocated inside the native data structure. That means that data can be transferred to native resources without having to go through the java data structure. That can heave a really good impact on performance.

    NonDirect: if you create a buffer that will not interact with native resource (ex. Just to store a String) you should use a NonDirect Buffer.

    Adding to a Buffer



    When adding data to a buffer you can use the wrap() method;
    Code:
    String string = "Text to be added";
    
    CharBuffer charBuffer = CharBuffer.allocate(string.length());
    
    charBuffer.wrap(string);
    
    Note that when a buffer is created by wrapping it is never direct.
    Code:
    /*
     *  wraps a string inside a buffer.
     */
    String string = "Text to be added";
    
    CharBuffer charBuffer = CharBuffer.allocate(string.length());
    
    charBuffer.wrap(string);
    
    
    or you could wrap entire blocks of data in a form of an array:
    Code:
    /*
     * takes a byte array and wraps it into a buffer.
     */
    byte[] data = “Text to be added”.getBytes(“UTF-8”);
    
    ByteBuffer buffer1 = ByteBuffer.wrap(data);
    

    Draining a Buffer:



    Buffers can be drained into any data type:
    Code:
    
    /*
     * uses the get() method to fill a string.
     */
    String fromBuffer = “”;
    
    while (buffer.hasRemaining()) {
    
       fromBuffer += buffer.get();
    
    }
    

    Data Conversion:



    Data Conversion is an important aspect of buffers. You can use the factory methods to change a buffer from one type of another:
    Code:
    
    ByteBuffer byteBuffer = ByteBuffer.allocate(5);
    
    IntBuffer intBuffer = byteBuffer.asIntBuffer();
    
    
    Here is a list of conversions:
    asShortBuffer();
    asCharBuffer();
    asIntBuffer();
    asLongBuffer();
    asFloatBuffer();
    asDoubleBuffer();
     
  2. Fasga

    Fasga New Member

    Joined:
    Dec 29, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Wow, that's interesting. I've been using manual buffers (manually reading/writing into a byte array, parsing, etc). This definitely makes it a lot easier.
     
  3. MarkLikvor

    MarkLikvor New Member

    Joined:
    Dec 29, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Oh..!! At Last, I find your site again..!!

    Oh..!! At Last, I find your site again..!! A few days ago I search "Programming and Web Development Forum" in the NET, and find this magnificent topic. But not SAVE your address www.go4expert.com. I've been reading it for a while, and decided to try my luck asking a few questions…., and I'd like want more about the "Programming and Web Development Forum", and about active members.! Where I can find more INFO about the "Programming and Web Development Forum". Thank you very much Best Regards..!!
     
  4. Krolik

    Krolik New Member

    Joined:
    Feb 16, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://home.elka.pw.edu.pl/~pkolaczk/
    Can you give some benchmarks? How does it affect performance? I mean - unless you are writing a RDBMS engine such micro-optimizations are of little value.
     
  5. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    I guess buffers also help improving performance while working on very large files!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice