get

Java-Put and Get Multibyte Data

Put and Get Multibyte Data

The ByteBuffer class provides a lightweight mechanism to access groups of bytes as a multibyte data type. ByteBuffer contains setter and getter methods for each of the primitive data types.


Setter Methods

Method Description
ByteBuffer putChar (char value) Writes two bytes containing the given char value, in the current byte order, into this buffer at the current position, and then increments the position by two.
ByteBuffer putChar (int index, char value) Writes two bytes containing the given char value, in the current byte order, into this buffer at the given index.
ByteBuffer putShort (short value) Writes two bytes containing the given short value, in the current byte order, into this buffer at the current position, and then increments the position by two.
ByteBuffer putShort (int index, short value) Writes two bytes containing the given short value, in the current byte order, into this buffer at the given index.
ByteBuffer putInt (int value) Writes four bytes containing the given int value, in the current byte order, into this buffer at the current position, and then increments the position by four.
ByteBuffer putInt (int index, int value) Writes four bytes containing the given int value, in the current byte order, into this buffer at the given index.
ByteBuffer putLong (long value) Writes eight bytes containing the given long value, in the current byte order, into this buffer at the current position, and then increments the position by eight.
ByteBuffer putLong (int index, long value) Writes eight bytes containing the given long value, in the current byte order, into this buffer at the given index.
ByteBuffer putFloat (float value) Writes four bytes containing the given float value, in the current byte order, into this buffer at the current position, and then increments the position by four.
ByteBuffer putFloat (int index, float value) Writes four bytes containing the given float value, in the current byte order, into this buffer at the given index.
ByteBuffer putDouble (double value) Writes eight bytes containing the given double value, in the current byte order, into this buffer at the current position, and then increments the position by eight.
ByteBuffer putDouble (int index, double value) Writes eight bytes containing the given double value, in the current byte order, into this buffer at the given index.



Getter Methods

Method Description
char     getChar( ) Reads two bytes at the given index, composing them into a char value according to the current byte order.
char     getChar (int index) Reads two bytes at the given index, composing them into a char value according to the current byte order.
short    getShort( ) Reads the next two bytes at this buffer’s current position, composing them into a short value according to the current byte order, and then increments the position by two.
short    getShort (int index) Reads two bytes at the given index, composing them into a short value according to the current byte order.
int         getInt( ) Reads the next four bytes at this buffer’s current position, composing them into an int value according to the current byte order, and then increments the position by four.
int         getInt (int index) Reads four bytes at the given index, composing them into a int value according to the current byte order.
long      getLong( ) Reads the next eight bytes at this buffer’s current position, composing them into a long value according to the current byte order, and then increments the position by eight.
long      getLong (int index) Reads eight bytes at the given index, composing them into a long value according to the current byte order.
float      getFloat( ) Reads the next four bytes at this buffer’s current position, composing them into a float value according to the current byte order, and then increments the position by four.
float      getFloat (int index) Reads four bytes at the given index, composing them into a float value according to the current byte order.
double  getDouble( ) Reads the next four bytes at this buffer’s current position, composing them into a float value according to the current byte order, and then increments the position by four.
double  getDouble (int index) Reads four bytes at the given index, composing them into a float value according to the current byte order.


Setter/Getter methods access the bytes of the ByteBuffer, starting at the current position, as if a data element of that type were stored there. The bytes will be marshaled to or from the requested primitive data type according to the current byte-order setting in effect for the buffer. For example, if getInt( ) is called, the four bytes beginning at the current position would be packed into an int and returned as the method value. The actual value returned would depend on the current ByteOrder setting of the buffer. To be more specific: order (ByteOrder.BIG_ENDIAN).getInt( ); returns the value as BIG_ENDIAN, while order (ByteOrder.LITTLE_ENDIAN).getInt( ); returns the value as LITTLE_ENDIAN. If the primitive data type you’re trying to get requires more bytes than what remains in the buffer, a BufferUnderflowException will be thrown.

setter

The elements returned by these methods do not need to be aligned on any specific modulo boundary. Data values will be fetched and assembled from the byte buffer beginning at the current position of the buffer, regardless of word alignment. This can be inefficient, but it allows for arbitrary placement of data in a byte stream. This can be useful for extracting numeric values from binary file data or packing data into platform-specific formats for export to external systems. The setter methods perform the inverse operation of the getter methods. Primitive data values will be broken into bytes according to the byte order of the buffer and stored. If insufficient space is available to store all the bytes, a BufferOverflowException will be thrown.

getter
Program

Put and Get output


Program Source

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Javaapp {

    public static void main(String[] args) {
        
        ByteBuffer buf = ByteBuffer.allocate(13);
        buf.putInt(169090600);
        buf.putShort((short)12860);
        buf.order(ByteOrder.LITTLE_ENDIAN).putInt(169090600);
        buf.order(ByteOrder.BIG_ENDIAN).putShort((short)12860);
        buf.flip();
        System.out.println("Int   :   "+buf.getInt());
        System.out.println("Short :   "+buf.getShort());
        System.out.println("Int   :   "+buf.order(ByteOrder.LITTLE_ENDIAN).getInt());
        System.out.println("Int   :   "+buf.order(ByteOrder.BIG_ENDIAN).getShort());
    }
}

Leave a Comment