假设我有一个list/arrayList或一个ByteBuffers数组(List<ByteBuffer>或ByteBuffer[])
是否可以直接从上面的数组中获得字节,而无需迭代所有项或计算它们的总大小?我在找这样的东西:
ByteBuffer[] bufferList = new ByteBuffer[7]; //this can be any kind of list of ByteBuffers
//add items to the array
ByteBuffer buffer = (ByteBuffer) bufferList; //what i want to achieve 当然,最后一行是不对的。
问题是,我已经将数组中的所有字节按顺序排列,但我希望它们不再在列表中,而是在单个ByteBuffer中。那么,有什么东西可以从ByteBuffer的列表/数组中创建ByteBuffers呢?
谢谢!
发布于 2013-08-27 15:18:00
public ByteBuffer convertToOne(ByteBuffer[] src)
{
int size=0;
for(int i = 0 ; i < src.length ; i++) {
size+=src[i].array().length;
}
ByteBuffer newBuffer = ByteBuffer.allocate(size);
int sizeAt = 0;
for(int i = 0 ; i < src.length ; i++) {
newBuffer.put(src[i].array(), sizeAt);
sizeAt += src[i].array().length;
}
return newBuffer;
}发布于 2013-08-27 15:14:58
我相信您将不得不创建一个新的ByteBuffer,并将bufferList中的每个缓冲区复制到新的缓冲区中。您应该将此功能放入方法中,以避免每次使用它时重复代码。
https://stackoverflow.com/questions/18469241
复制相似问题