• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to use ByteBuffer in Java?

August 7, 2012 //  by Mohamed Sanaulla

ByteBuffer API has been in Java since 1.4. The name itself suggests that these contain bytes of data. The data to be stored in the buffer can be Integer (int, long), Characters (char), Floating value (double), all these are converted into bytes before being stored in the buffer array. ByteBuffer can be of two types-

  • Direct ByteBuffer
  • Non-Direct ByteBuffer

also read:

  • Java Tutorials
  • Java EE Tutorials
  • Design Patterns Tutorials
  • Java File IO Tutorials

Direct ByteBuffer: JVM tries to use Native IO operations on buffers which are created as direct i.e it doesnt load the buffer contents into another buffer and instead tries to use the buffer directly.
There are a few essential properties of ByteBuffer:

  • Limit: While writing to a buffer, limit indicates how many more bytes can be written, whereas while reading from a buffer limit indicates how many more bytes can be read.
  • Position: Position tracks how much data is written or can be read from a buffer.
  • Capacity: Capacity specifies the number of bytes a buffer can store.

ByteBuffer provides plethora of methods to fetch data from the buffer and to add data to the buffer. The below code shows the use of those APIs:

import java.nio.ByteBuffer;

public class ByteBufferTest {
  
  public static void main(String[] args) {

    //Create a directBuffer of size 200 bytes
    ByteBuffer directBuffer = ByteBuffer.allocateDirect(200);
    
    //Create a nonDirectBuffer of size 200 bytes
    ByteBuffer nonDirectBuffer = ByteBuffer.allocate(200);
    
    //Get the capacity of the buffer
    System.out.println("Capacity "+nonDirectBuffer.capacity());
    
    //Get the position of the buffer
    System.out.println("Position "+nonDirectBuffer.position() );
    
    //Add data of different types to the buffer
    nonDirectBuffer.putChar('A');
    nonDirectBuffer.putInt(10);
    nonDirectBuffer.putDouble(0.98);
    
    //Get the position of the buffer, would print 14 as its has 14 bytes of data
    System.out.println("Position "+nonDirectBuffer.position() );
    nonDirectBuffer.putFloat(8.9f);
    
    //Fetch the data from buffer
    System.out.println(nonDirectBuffer.getChar(0));
    
    //A char is of 2 bytes, so fetch the integer at index 0+2=2bytes
    System.out.println(nonDirectBuffer.getInt(2));
    
    //A int is of 4 bytes so fetch the double value at index 2+4=6bytes
    System.out.println(nonDirectBuffer.getDouble(6));
    
    //A double is of 8 bytes so fetch the float value at index 6+8=14bytes
    System.out.println(nonDirectBuffer.getFloat(14));
   
  }
}

Category: JavaTag: nio

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Previous Post: « Lazy Initialization, Singleton Pattern and Double Checked locking
Next Post: How to use Socket API for creating Client-Server application in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Implement getActiveCount() Method of ThreadPoolExeceutor in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact