gpt4 book ai didi

java - 如何在没有 byteBuffer.array() 的情况下将一个 byteBuffer 部分复制到另一个 byteBuffer

转载 作者:行者123 更新时间:2023-11-29 03:04:11 26 4
gpt4 key购买 nike

我有一个空的 byteBuffer 分配为

data = ByteBuffer.allocateDirect(layerSize(0, faces - 1, 0, levels - 1) * layers);

正在关注 this answer ,我尝试使用 array() 方法如下

public void setData(ByteBuffer data, int layer, int face, int level) {
int offset = offset(layer, face, level);
int levelSize = levelSize(level);
this.data.put(data.array(), offset, levelSize);
}

但是我得到:

Caused by: java.lang.UnsupportedOperationException
at java.nio.ByteBuffer.array(ByteBuffer.java:994)

我尝试使用的源字节缓冲区是这样读取的:

    File file = new File(Load.class.getResource(fileName).getFile());
FileInputStream fileInputStream = new FileInputStream(file);
FileChannel fileChannel = fileInputStream.getChannel();

return loadKtx(fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) file.length()));

public static Texture loadKtx(ByteBuffer byteBuffer) throws IOException {
...
byteBuffer.position(offset);
byteBuffer.limit(offset + faceSize);

ByteBuffer data = byteBuffer.slice();
texture.setData(data, layer, face, level);

byteBuffer.position(0);
byteBuffer.limit(byteBuffer.capacity());

有没有比简单的更好的选择

    for (int b = 0; b < levelSize; b++) {
this.data.put(offset + b, data.get(b));
}

?

但是,整个项目here对于有兴趣的人。

最佳答案

您是否只是想将一个 ByteBuffer 的一部分复制到另一个?如果是这样,你应该duplicate()你想复制的ByteBuffer,设置positionlimit复制缓冲区到您要复制的范围,然后使用 put()。喜欢:

ByteBuffer src = data.duplicate();
src.position(offset);
src.limit(offset + levelSize);
dest.put(src);

关于java - 如何在没有 byteBuffer.array() 的情况下将一个 byteBuffer 部分复制到另一个 byteBuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33012534/

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