java 将 LongBuffer/IntBuffer/ShortBuffer 转换为 ByteBuffer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11665147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:57:38  来源:igfitidea点击:

convert a LongBuffer/IntBuffer/ShortBuffer to ByteBuffer

javaarraysbytearraynio

提问by logoff

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/short/int/long 数组转换为 ByteBuffer,然后获取字节数组的快速方法。例如,要将字节数组转换为短数组,我可以执行以下操作:

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].

产生短阵列是这样的:[256, 0, 0, 0, 256, 0, 0, 0]

How can I do the inverse operation using java.nioclasses?

如何使用java.nio类进行逆运算?

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.

我获得了原始的[1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]字节数组。但我想使用像 ShortBuffer.asByteBuffer() 这样的方法,而不是手动循环来做到这一点。

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

我找到了2001 年向 Sun 提出请求,但他们没有接受;-(

回答by A.H.

What about this? :

那这个呢?:

    bb.asShortBuffer().put(shortArray);

Then bbcontains your data.

然后bb包含您的数据。

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!
    }
}

回答by Edwin Dalorzo

Well, in the declaration

嗯,在声明中

ShortBuffer sb = bb.asShortBuffer();

AFAIK, your ShortBufferis just a view of the original ByteBuffer. So, you could always access the original ByteBuffervariable bband see the data as modified through your CharBufferreference sb.

AFAIK,您ShortBuffer只是对原始ByteBuffer. 因此,您始终可以访问原始ByteBuffer变量bb并查看通过您的CharBuffer引用修改的数据sb

The documentation for asCharBuffersays:

asCharBuffer的文档说:

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

[...] 对该缓冲区内容的更改将在新缓冲区中可见,反之亦然 [...]

回答by Peter Lawrey

I would remember the ByteBuffer you used to create the ShortBuffer from as its a view onto the same buffer.

我会记得你用来创建 ShortBuffer 的 ByteBuffer 作为它对同一个缓冲区的视图。

Otherwise it appears the only way to get this back is to use reflections to get the bbfield.

否则,似乎唯一的方法是使用反射来获取bb字段。

BTW: Using a direct ByteBuffer can be significantly faster depending on what you are doing esp if you use native byte order.

顺便说一句:使用直接 ByteBuffer 可以明显更快,具体取决于您在做什么,尤其是如果您使用本机字节顺序。