从 java 中的 ByteBuffer 获取字节数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/679298/
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
Gets byte array from a ByteBuffer in java
提问by kal
Is this the recommended way to get the bytes from the ByteBuffer
这是从 ByteBuffer 获取字节的推荐方法吗
ByteBuffer bb =..
byte[] b = new byte[bb.remaining()]
bb.get(b, 0, b.length);
回答by Jason S
Depends what you want to do.
取决于你想做什么。
If what you want is to retrieve the bytes that are remaining (between position and limit), then what you have will work. You could also just do:
如果您想要检索剩余的字节(在位置和限制之间),那么您所拥有的将起作用。你也可以这样做:
ByteBuffer bb =..
byte[] b = new byte[bb.remaining()]
bb.get(b);
which is equivalent as per the ByteBufferjavadocs.
这相当于ByteBufferjavadocs。
回答by Peter Lawrey
This is a simple way to get a byte[], but part of the point of using a ByteBuffer is avoiding having to create a byte[]. Perhaps you can get whatever you wanted to get from the byte[] directly from the ByteBuffer.
这是获取 byte[] 的一种简单方法,但使用 ByteBuffer 的部分目的是避免必须创建 byte[]。也许您可以直接从 ByteBuffer 中获取您想从 byte[] 中获取的任何内容。
回答by R4zorax
Note that the bb.array() doesn't honor the byte-buffers position, and might be even worse if the bytebuffer you are working on is a slice of some other buffer.
请注意, bb.array() 不尊重字节缓冲区的位置,如果您正在处理的字节缓冲区是其他缓冲区的一部分,则情况可能会更糟。
I.e.
IE
byte[] test = "Hello World".getBytes("Latin1");
ByteBuffer b1 = ByteBuffer.wrap(test);
byte[] hello = new byte[6];
b1.get(hello); // "Hello "
ByteBuffer b2 = b1.slice(); // position = 0, string = "World"
byte[] tooLong = b2.array(); // Will NOT be "World", but will be "Hello World".
byte[] world = new byte[5];
b2.get(world); // world = "World"
Which might not be what you intend to do.
这可能不是您打算做的。
If you really do not want to copy the byte-array, a work-around could be to use the byte-buffer's arrayOffset() + remaining(), but this only works if the application supports index+length of the byte-buffers it needs.
如果您真的不想复制字节数组,则解决方法可能是使用字节缓冲区的 arrayOffset() + 剩余 (),但这仅在应用程序支持字节缓冲区的索引+长度时才有效需要。
回答by Jin Kwon
final ByteBuffer buffer;
if (buffer.hasArray()) {
final byte[] array = buffer.array();
final int arrayOffset = buffer.arrayOffset();
return Arrays.copyOfRange(array, arrayOffset + buffer.position(),
arrayOffset + buffer.limit());
}
// do something else
回答by Tomá? My?ík
If one does not know anything about the internal state of the given (Direct)ByteBuffer and wants to retrieve the whole content of the buffer, this can be used:
如果对给定的 (Direct)ByteBuffer 的内部状态一无所知,并且想要检索缓冲区的全部内容,则可以使用以下方法:
ByteBuffer byteBuffer = ...;
byte[] data = new byte[byteBuffer.capacity()];
((ByteBuffer) byteBuffer.duplicate().clear()).get(data);
回答by Salman Nazir
As simple as that
就如此容易
private static byte[] getByteArrayFromByteBuffer(ByteBuffer byteBuffer) {
byte[] bytesArray = new byte[byteBuffer.remaining()];
byteBuffer.get(bytesArray, 0, bytesArray.length);
return bytesArray;
}