gpt4 book ai didi

java - 如何从较大的 ByteBuffer 中读取较小的 ByteBuffer?

转载 作者:行者123 更新时间:2023-12-02 06:11:17 26 4
gpt4 key购买 nike

虽然存在 ByteBuffer.put(ByteBuffer) 方法,但 ByteBuffer.get(ByteBuffer) 似乎丢失了?我应该如何从较大的 ByteBuffer 读取较小的 ByteBuffer

最佳答案

存在ByteBuffer#put方法:

public ByteBuffer put(ByteBuffer src) : This method transfers the bytes remaining in the given source buffer into this buffer

您正在寻找类似的东西

public ByteBuffer get(ByteBuffer dst) : This method transfers the bytes remaining in this buffer into the given target buffer

但请考虑操作 getput 在某种程度上是对称的。

ByteBuffer src = ...
ByteBuffer dst = ...
//src.get(dst); // Does not exist
dst.put(src); // Use this instead

您明确谈论了较小较大缓冲区。因此,我假设 dst 缓冲区小于 src 缓冲区。在这种情况下,您只需相应地设置源缓冲区的限制即可:

ByteBuffer src = ...
ByteBuffer dst = ...
int oldLimit = src.limit();
src.limit(src.position()+dst.remaining());
dst.put(src);
src.limit(oldLimit);

可以使用替代公式(例如使用原始缓冲区的ByteBuffer#slice())。但无论如何,您不必必须将缓冲区内容复制到新的字节数组中才能将其传输到另一个缓冲区!

关于java - 如何从较大的 ByteBuffer 中读取较小的 ByteBuffer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21857854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com