Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a LongBuffer/IntBuffer/ShortBuffer to ByteBuffer

I know a quick way to convert a byte/short/int/long array to ByteBuffer, and then obtain a byte array. For instance, to convert a byte array to short array I can do:

byte[] bArray = { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 };

ByteBuffer bb = ByteBuffer.wrap(byteArray);
ShortBuffer sb = bb.asShortBuffer();
short[] shortArray = new short[byteArray.length / 2];
sb.get(shortArray);

produces a short array like this: [256, 0, 0, 0, 256, 0, 0, 0].

How can I do the inverse operation using java.nio classes?

Now I am doing this:

shortArray[] = {256, 0, 0, 0, 256, 0, 0, 0};
ByteBuffer bb = ByteBuffer.allocate(shortArray.length * 2);
for (short s : shortArray) {
    bb.putShort(s);
}
return bb.array();

And I obtain the original [1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0] byte array. But I want to use a method like ShortBuffer.asByteBuffer(), not a manual loop to do it.

I have found a request to Sun of 2001, but they did not accept it ;-((

like image 203
logoff Avatar asked Jul 26 '12 08:07

logoff


People also ask

How do I get ByteBuffer length?

receive(request); int length = request. getLength() - request. getOffset() + 1; The result of this is length = 20 as size was 15. That is, request.

How do I make 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.

How do I allocate ByteBuffer?

A new ByteBuffer can be allocated using the method allocate() in the class java. nio. ByteBuffer. This method requires a single parameter i.e. the capacity of the buffer.

What is the byte order of ByteBuffer?

By default, the order of a ByteBuffer object is BIG_ENDIAN. If a byte order is passed as a parameter to the order method, it modifies the byte order of the buffer and returns the buffer itself. The new byte order may be either LITTLE_ENDIAN or BIG_ENDIAN.


2 Answers

What about this? :

    bb.asShortBuffer().put(shortArray);

Then bb contains your data.

Full code:

public class Test {
    public static void main(final String args[]) {
        short[] arr = { 256, 0, 0, 0, 256, 0, 0, 0 };
        for (byte b : F(arr)) {
            System.out.print(b);
        }
    }

    public static byte[] F(short[] arr) {
        java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(arr.length * 2);
        bb.asShortBuffer().put(arr);
        return bb.array(); // this returns the "raw" array, it's shared and not copied!
    }
}
like image 56
A.H. Avatar answered Oct 06 '22 10:10

A.H.


Well, in the declaration

ShortBuffer sb = bb.asShortBuffer();

AFAIK, your ShortBuffer is just a view of the original ByteBuffer. So, you could always access the original ByteBuffer variable bb and see the data as modified through your CharBuffer reference sb.

The documentation for asCharBuffer says:

[...] Changes to this buffer's content will be visible in the new buffer, and vice versa [...]

like image 40
Edwin Dalorzo Avatar answered Oct 06 '22 08:10

Edwin Dalorzo