JavaBeat

  • Home
  • 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)
  • Privacy
  • Contact Us

How to use ByteBuffer in Java?

August 7, 2012 by Mohamed Sanaulla Leave a Comment

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:
[code lang=”java”]
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));

}
}

[/code]

Filed Under: Java Tagged With: 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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved