Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have an unsigned ByteBuffer in java?

Tags:

java

Subject says it all. I'm working with OpenGL and OpenCL and would make life easier if I could just use an unsigned ByteBuffer to store data.

like image 222
user697111 Avatar asked Mar 27 '12 04:03

user697111


People also ask

Is there unsigned byte in Java?

Java doesn't have unsigned bytes (0 to 255). To make an unsigned byte, we can cast the byte into an int and mask (bitwise and) the new int with a 0xff to get the last 8 bits or prevent sign extension.

How does ByteBuffer work in Java?

ByteBuffer holds a sequence of integer values to be used in an I/O operation. The ByteBuffer class provides the following four categories of operations upon long buffers: Absolute and relative get method that read single bytes. Absolute and relative put methods that write single bytes.

Why is byte unsigned?

An UnsignedByte is like a Byte , but its values range from 0 to 255 instead of -128 to 127. Most languages have a native unsigned-byte type (e.g., C, C++, C#), but Java doesn't. When manipulating bytes as bit sequences, as we do in the CPU implementation, it is helpful to treat them as unsigned.

How do you make a ByteBuffer?

A ByteBuffer is created via the the two static factory methods: allocate(int) this will allocate a HeapByteBuffer with the capacity specified by the int argument. allocateDirect(int) this will allocate a DirectByteBuffer with the capacity specified by the int argument.


3 Answers

unsigned ByteBuffer example:

import java.nio.ByteBuffer;

public class test {
    public static short getUnsignedByte(ByteBuffer bb) {
        return ((short) (bb.get() & 0xff));
    }

    public static void putUnsignedByte(ByteBuffer bb, int value) {
        bb.put((byte) (value & 0xff));
    }

    public static short getUnsignedByte(ByteBuffer bb, int position) {
        return ((short) (bb.get(position) & (short) 0xff));
    }

    public static void putUnsignedByte(ByteBuffer bb, int position, int value) {
        bb.put(position, (byte) (value & 0xff));
    }

    // ---------------------------------------------------------------

    public static int getUnsignedShort(ByteBuffer bb) {
        return (bb.getShort() & 0xffff);
    }

    public static void putUnsignedShort(ByteBuffer bb, int value) {
        bb.putShort((short) (value & 0xffff));
    }

    public static int getUnsignedShort(ByteBuffer bb, int position) {
        return (bb.getShort(position) & 0xffff);
    }

    public static void putUnsignedShort(ByteBuffer bb, int position, int value) {
        bb.putShort(position, (short) (value & 0xffff));
    }

    // ---------------------------------------------------------------

    public static long getUnsignedInt(ByteBuffer bb) {
        return ((long) bb.getInt() & 0xffffffffL);
    }

    public static void putUnsignedInt(ByteBuffer bb, long value) {
        bb.putInt((int) (value & 0xffffffffL));
    }

    public static long getUnsignedInt(ByteBuffer bb, int position) {
        return ((long) bb.getInt(position) & 0xffffffffL);
    }

    public static void putUnsignedInt(ByteBuffer bb, int position, long value) {
        bb.putInt(position, (int) (value & 0xffffffffL));
    }

    // ---------------------------------------------------

    public static void main(String[] argv) throws Exception {
        ByteBuffer buffer = ByteBuffer.allocate(20);

        buffer.clear();
        test.putUnsignedByte(buffer, 255);
        test.putUnsignedByte(buffer, 128);
        test.putUnsignedShort(buffer, 0xcafe);
        test.putUnsignedInt(buffer, 0xcafebabe);

        for (int i = 0; i < 8; i++) {
            System.out.println("" + i + ": "
                    + Integer.toHexString((int) getUnsignedByte(buffer, i)));
        }

        System.out.println("2: "
                + Integer.toHexString(getUnsignedShort(buffer, 2)));
        System.out.println("4: " + Long.toHexString(getUnsignedInt(buffer, 4)));
    }
}
like image 62
kandarp Avatar answered Oct 21 '22 01:10

kandarp


Java doesn't support unsigned types. The typical solution is to go to the next biggest type (in your case: short), and just mask it so you only use the lower 'n' (in your case 8) bits.

... but that kind of breaks when you try to apply to buffers :-(

like image 32
John3136 Avatar answered Oct 20 '22 23:10

John3136


how about this Short.toUnsignedInt(buffer.getShort());

like image 2
francojohnc Avatar answered Oct 21 '22 00:10

francojohnc