Java 的 ByteBuffer 的深拷贝副本()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3366925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deep copy duplicate() of Java's ByteBuffer
提问by Mr. Red
java.nio.ByteBuffer#duplicate()
returns a new byte buffer that shares the old buffer's content. Changes to the old buffer's content will be visible in the new buffer, and vice versa. What if I want a deep copy of the byte buffer?
java.nio.ByteBuffer#duplicate()
返回一个共享旧缓冲区内容的新字节缓冲区。对旧缓冲区内容的更改将在新缓冲区中可见,反之亦然。如果我想要字节缓冲区的深层副本怎么办?
回答by Kylar
You'll need to iterate the entire buffer and copy by value into the new buffer.
您需要迭代整个缓冲区并按值复制到新缓冲区中。
回答by mingfai
I think the deep copy need not involve byte[]
. Try the following:
我认为深拷贝不需要涉及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;
}
回答by LINEMAN78
Based off of mingfai's solution:
基于 mingfai 的解决方案:
This will give you an almost true deep copy. The only thing lost will be the mark. If orig is a HeapBuffer and the offset is not zero or the capacity is less than the backing array than the outlying data is not copied.
这会给你一个几乎真实的深拷贝。唯一丢失的将是标记。如果 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);
}
}
回答by jdmichal
As this question still comes up as one of the first hits to copying a ByteBuffer
, I will offer my solution. This solution does not touch the original buffer, including any mark set, and will return a deep copy with the same capacity as the original.
由于这个问题仍然是复制 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;
}
If one cares for the position, limit, or order to be set the same as the original, then that's an easy addition to the above:
如果您希望将头寸、限制或订单设置为与原始相同,那么这是对上述内容的一个简单补充:
clone.position(original.position());
clone.limit(original.limit());
clone.order(original.order());
return clone;
回答by sunil kalva
One more simple solution
一种更简单的解决方案
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;
}
回答by Laike Endaril
I believe this should supply a fulldeep copy, including the mark, "out-of-bounds" data, etc...just in case you need the most completesandbox-safe carbon copy of a ByteBuffer.
我相信这应该提供一个完整的深层副本,包括标记、“越界”数据等……以防万一您需要最完整的 ByteBuffer 沙箱安全副本。
The only thing it doesn't copy is the read-only trait, which you can easily get by just calling this method and tagging on a ".asReadOnlyBuffer()"
它唯一不复制的是只读特性,您可以通过调用此方法并在“.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;
}