Java-ByteBuffer to Primitive Buffer

ByteBuffer to Primitive Buffer

When doing high-volume Input/Output, odds are you’ll be using ByteBuffers to read in files, receive data from network connections, etc. Once the data has arrived in your ByteBuffer, you’ll need to look at it to decide what to do or manipulate it before sending it along. The ByteBuffer class provides a rich API for creating view buffers.

View buffers are created by a factory method duplicate() on an existing buffer object instance. The view object maintains its own attributes, capacity, position, limit, and mark, but shares data elements with the original buffer. But ByteBuffer allows the creation of views to map the raw bytes of the byte buffer to other primitive data types. For example, the asShortBuffer( ) method creates a view buffer that will access groups of 2 bytes from the ByteBuffer as Short, the asIntBuffer( ) method creates a view buffer that will access groups of 4 bytes from the ByteBuffer as Int.

Each of the following factory methods create a new buffer that is a view into the original ByteBuffer object.

Methods Description
CharBuffer asCharBuffer( ) Creates a view of this byte buffer as a char buffer.
ShortBuffer asShortBuffer( ) Creates a view of this byte buffer as a short buffer.
IntBuffer asIntBuffer( ) Creates a view of this byte buffer as an int buffer.
LongBuffer asLongBuffer( ) Creates a view of this byte buffer as a long buffer.
FloatBuffer asFloatBuffer( ) Creates a view of this byte buffer as a float buffer.
DoubleBuffer asDoubleBuffer( ) Creates a view of this byte buffer as a double buffer.

Invoking one of this methods will create a buffer of the corresponding type, which is a view buffer of the underlying byte buffer corresponding to the byte buffer’s current position and limit. The new buffer will have a capacity equal to the number of elements remaining in the byte buffer (as returned by remaining( )) divided by the number of bytes comprising the view’s primitive type. Any remaining bytes at the end of the original buffer will not be visible in the view. The first element of the view will begin at the position (as returned by position( )) of the ByteBuffer object at the time the view was created. Once you’ve obtained the primitive view buffer, you can create further subviews with duplicate(), slice(), and asReadOnlyBuffer().

Primitive Data Types and Sizes

Data type Size
byte 1 byte
char 2 bytes
short 2 bytes
int 4 bytes
long 8 bytes
float 4 bytes
double 8 bytes

Program

Program Source

import java.nio.ByteBuffer;
import java.nio.ShortBuffer;
import java.nio.IntBuffer;

public class Javaapp {

    public static void main(String[] args) {

        ByteBuffer buf = ByteBuffer.allocate(16);
        buf.put(new byte[]{10,20,30,40,50,60,70,80,90,100,105,110,115,120,125});
        buf.flip();
        ShortBuffer shortbuf = buf.asShortBuffer();
        IntBuffer intbuf = buf.asIntBuffer();
        
        for(int i=0;i<shortbuf.limit();i++)
        {
            System.out.print("["+shortbuf.position()+"-"+shortbuf.get()+"] ");
        }
        System.out.println();
        for(int i=0;i<intbuf.limit();i++)
        {
            System.out.print("["+intbuf.position()+"-"+intbuf.get()+"] ");
        }   
    }
}

Leave a Comment