深度复制Java的ByteBuffer复制() [英] Deep copy duplicate() of Java's ByteBuffer

查看:296
本文介绍了深度复制Java的ByteBuffer复制()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

我认为深层副本不需要涉及字节[] 。请尝试以下方法:

I think the deep copy need not involve byte[]. Try the following:

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

这篇关于深度复制Java的ByteBuffer复制()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆