在 Java 中连接两个 ByteBuffer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23219644/
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-08-13 21:24:29  来源:igfitidea点击:

Concat two ByteBuffers in Java

javabytebuffer

提问by mcfly soft

How can I concat two ByteBuffers to one ByteBuffer?

如何将两个 ByteBuffer 连接到一个 ByteBuffer?

The following doesn't work:

以下不起作用:

    ByteBuffer bb = ByteBuffer.allocate(100);
    ByteBuffer bb2 = ByteBuffer.allocate(200);
    bb.allocate(200).put(bb2);
    System.out.println(bb.array().length);

The length of bbis still 100.

的长度bb仍然是100

采纳答案by Marco13

Something like

就像是

bb = ByteBuffer.allocate(300).put(bb).put(bb2);

should do the job: Create a buffer that is large enough to hold the contents of both buffers, and then use the relative put-methods to fill it with the first and the second buffer. (The putmethod returns the instance that the method was called on, by the way)

应该做的工作:创建一个足够大的缓冲区来保存两个缓冲区的内容,然后使用相关的 put-methods 用第一个和第二个缓冲区填充它。(put顺便说一下,该方法返回调用该方法的实例)

回答by Roelof van den Berg

Try the following code:

试试下面的代码:

//store both ByteBuffer object as an array
byte[] array1 = ByteBuffer.allocate(100).array();
byte[] array2 = ByteBuffer.allocate(200).array();

//create a ByteBuffer which is big enough
ByteBuffer bigenough = ByteBuffer.allocate(array1.length + array2.length);

//put the two arrays in one ByteBuffer
ByteBuffer after1 = bigenough.put(array1, 0, array1.length);
ByteBuffer result = after1.put(array2, array1.length, array2.length);

//print the length of the combined array.
System.out.println(result.array().length);

回答by TJR

We'll be copying all data. Remember that this is why string concatenation is expensive!

我们将复制所有数据。请记住,这就是字符串连接成本高昂的原因!

public static ByteBuffer concat(final ByteBuffer... buffers) {
    final ByteBuffer combined = ByteBuffer.allocate(Arrays.stream(buffers).mapToInt(Buffer::remaining).sum());
    Arrays.stream(buffers).forEach(b -> combined.put(b.duplicate()));
    return combined;
}

回答by Z-Y00

you can use the method here

你可以使用这里的方法

https://github.com/ata4/ioutils/blob/047e401d73c866317af2e12f7803b3ee43eec80a/src/main/java/info/ata4/io/buffer/ByteBufferUtils.java#L289

https://github.com/ata4/ioutils/blob/047e401d73c866317af2e12f7803b3ee43eec80a/src/main/java/info/ata4/io/buffer/ByteBufferUtils.java#L289

and for example:

例如:

  ByteBuffer concat() {
int length = 0;
for (ByteBuffer bb : buffers) {
  bb.rewind();
  length += bb.remaining();
}
ByteBuffer bbNew = ByteBuffer.allocateDirect((int) length);

// put all buffers from list
for (ByteBuffer bb : buffers) {
  bb.rewind();
  bbNew.put(bb);

}
bbNew.rewind();
return bbNew;
}

回答by Sid_idk

Probably because on line3 i.e. bb.allocate(200).put(bb2);,

可能是因为在3号线,即bb.allocate(200).put(bb2);

bb.allocate(200)is returning a new Byte buffer (See https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#allocate(int)). That is not actually changing bbitself. So its still the bytebuffer of capacity 100 from line1.

bb.allocate(200)正在返回一个新的字节缓冲区(参见https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#allocate(int))。这实际上并没有改变bb自己。所以它仍然是第 1 行容量为 100 的字节缓冲区。