java.nio.ByteBuffer#duplicate()
返回一个共享旧缓冲区内容的新字节缓冲区。对旧缓冲区内容的更改将在新缓冲区中可见,反之亦然。如果我想要字节缓冲区的深层副本怎么办?
问问题
35082 次
6 回答
45
我认为深拷贝不需要涉及byte[]
。尝试以下操作:
public static ByteBuffer clone(ByteBuffer original) {
ByteBuffer clone = ByteBuffer.allocate(original.capacity());
original.rewind();//copy from the beginning
clone.put(original);
original.rewind();
clone.flip();
return clone;
}
于 2010-11-02T00:02:34.653 回答
22
由于这个问题仍然是复制 a 的首要问题之一,因此ByteBuffer
我将提供我的解决方案。此解决方案不会触及原始缓冲区,包括任何标记集,并且将返回与原始缓冲区具有相同容量的深层副本。
public static ByteBuffer cloneByteBuffer(final ByteBuffer original) {
// Create clone with same capacity as original.
final ByteBuffer clone = (original.isDirect()) ?
ByteBuffer.allocateDirect(original.capacity()) :
ByteBuffer.allocate(original.capacity());
// Create a read-only copy of the original.
// This allows reading from the original without modifying it.
final ByteBuffer readOnlyCopy = original.asReadOnlyBuffer();
// Flip and read from the original.
readOnlyCopy.flip();
clone.put(readOnlyCopy);
return clone;
}
如果有人关心将位置、限制或订单设置为与原始相同,那么这是对上述内容的简单补充:
clone.position(original.position());
clone.limit(original.limit());
clone.order(original.order());
return clone;
于 2014-01-27T18:00:00.583 回答
3
基于mingfai的解决方案:
这将为您提供几乎真实的深层副本。唯一丢失的将是标记。如果 orig 是 HeapBuffer 并且偏移量不为零或容量小于后备数组,则不复制外围数据。
public static ByteBuffer deepCopy( ByteBuffer orig )
{
int pos = orig.position(), lim = orig.limit();
try
{
orig.position(0).limit(orig.capacity()); // set range to entire buffer
ByteBuffer toReturn = deepCopyVisible(orig); // deep copy range
toReturn.position(pos).limit(lim); // set range to original
return toReturn;
}
finally // do in finally in case something goes wrong we don't bork the orig
{
orig.position(pos).limit(lim); // restore original
}
}
public static ByteBuffer deepCopyVisible( ByteBuffer orig )
{
int pos = orig.position();
try
{
ByteBuffer toReturn;
// try to maintain implementation to keep performance
if( orig.isDirect() )
toReturn = ByteBuffer.allocateDirect(orig.remaining());
else
toReturn = ByteBuffer.allocate(orig.remaining());
toReturn.put(orig);
toReturn.order(orig.order());
return (ByteBuffer) toReturn.position(0);
}
finally
{
orig.position(pos);
}
}
于 2012-11-20T03:13:59.723 回答
3
一种更简单的解决方案
public ByteBuffer deepCopy(ByteBuffer source, ByteBuffer target) {
int sourceP = source.position();
int sourceL = source.limit();
if (null == target) {
target = ByteBuffer.allocate(source.remaining());
}
target.put(source);
target.flip();
source.position(sourceP);
source.limit(sourceL);
return target;
}
于 2015-04-09T18:24:21.920 回答
1
您需要迭代整个缓冲区并按值复制到新缓冲区中。
于 2010-07-29T21:05:01.387 回答
1
我相信这应该提供完整的深层副本,包括标记、“越界”数据等……以防万一您需要最完整的沙盒安全副本 ByteBuffer。
它唯一不复制的是只读特征,您可以通过调用此方法并标记“.asReadOnlyBuffer()”轻松获得
public static ByteBuffer cloneByteBuffer(ByteBuffer original)
{
//Get position, limit, and mark
int pos = original.position();
int limit = original.limit();
int mark = -1;
try
{
original.reset();
mark = original.position();
}
catch (InvalidMarkException e)
{
//This happens when the original's mark is -1, so leave mark at default value of -1
}
//Create clone with matching capacity and byte order
ByteBuffer clone = (original.isDirect()) ? ByteBuffer.allocateDirect(original.capacity()) : ByteBuffer.allocate(original.capacity());
clone.order(original.order());
//Copy FULL buffer contents, including the "out-of-bounds" part
original.limit(original.capacity());
original.position(0);
clone.put(original);
//Set mark of both buffers to what it was originally
if (mark != -1)
{
original.position(mark);
original.mark();
clone.position(mark);
clone.mark();
}
//Set position and limit of both buffers to what they were originally
original.position(pos);
original.limit(limit);
clone.position(pos);
clone.limit(limit);
return clone;
}
于 2020-01-12T02:25:49.367 回答