深层复制Java的ByteBuffer复制()

时间:2010-07-29 21:02:31

标签: java nio deep-copy bytebuffer

java.nio.ByteBuffer#duplicate()返回一个新的字节缓冲区,它共享旧缓冲区的内容。旧缓冲区内容的更改将在新缓冲区中可见,反之亦然。如果我想要字节缓冲区的深层副本怎么办?

6 个答案:

答案 0 :(得分:37)

我认为深层复制不需要涉及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;
}

答案 1 :(得分:17)

由于这个问题仍然是复制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;

答案 2 :(得分:2)

基于明法的解决方案:

这将为您提供几乎真实的深层复制品。唯一丢失的将是标记。如果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);
    }
}

答案 3 :(得分:2)

一个更简单的解决方案

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;
}

答案 4 :(得分:1)

您需要迭代整个缓冲区并按值复制到新缓冲区中。

答案 5 :(得分:0)

我认为这应该提供完整的深层副本,包括标记,“越界”数据等,以防万一您需要最完整的 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;
}
相关问题