我有一个空的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对谁感兴趣。
答案 0 :(得分:4)
您只是想将一个ByteBuffer
的一部分复制到另一个中吗?如果是,您应该duplicate()
要复制的ByteBuffer
,将复制缓冲区的position
和limit
设置为您要复制的范围,然后使用put()
。像:
ByteBuffer src = data.duplicate();
src.position(offset);
src.limit(offset + levelSize);
dest.put(src);
答案 1 :(得分:0)
不幸的是,它似乎没有。
致@Stefen C的信用
答案 2 :(得分:0)
我正在这样做:
ByteBuffer src = //...
byte[] remaining = new byte[src.capacity() - src.position() /*or offset */];
src.get(remaining);
ByteBuffer dest = ByteBuffer.wrap(remaining);