0
点赞
收藏
分享

微信扫一扫

Netty使用记录-ByteBuf.duplicate和copy的区别

践行数据分析 2022-03-11 阅读 47



ByteBuf.copy是拷贝一个新的对象,修改新对象或者就对象不会互相影响,此方法与buf.copy(buf.readerIndex(),buf.readableBytes())相同。拷贝就对象所有可读的二进制到新对象当中。此方法不会修改旧对象的此readerIndex或writerIndex属性

package com.ht.web.google;

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import org.springframework.util.Base64Utils;

public class Rsa {

public static void main(String[] args) throws Exception {
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(8);

//写入值
buf.writeInt(128);

//拷贝一个新对象,在新对象上修改不会影响前对象
ByteBuf cp = buf.copy();

//返回0-4 readerIndex和writerIndex不受copy方法影响
System.out.println("readerIndex=" + buf.readerIndex() + "| writerIndex=" + buf.writerIndex());

//修改前对象的值
cp.setInt(0, 126);

//修改值互不相信
System.out.println(buf.readInt());
System.out.println(cp.readInt());

//全部需要释放
buf.release();
cp.release();
}

}


返回共享缓冲区,新旧bytebuf共享一个缓冲区,修改内容会互相影响,调用duplicate不会修改readerIndex或writerIndex的值,新旧缓冲区都维护独立的索引Index,新返回的缓冲区不需要进行释放(increased)。

package com.ht.web.google;

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import org.springframework.util.Base64Utils;

public class Rsa {

public static void main(String[] args) throws Exception {
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(8);

//写入值
buf.writeInt(128);

//拷贝一个新对象,在新对象上修改不会影响前对象
ByteBuf cp = buf.duplicate();

//返回0-4 readerIndex和writerIndex不受duplicate方法影响
System.out.println("readerIndex=" + buf.readerIndex() + "| writerIndex=" + buf.writerIndex());

//修改前对象的值
cp.setInt(0, 126);

//共享一块缓冲区,修改互相影响
System.out.println(buf.readInt());
System.out.println(cp.readInt());

//不需要全部需要释放,只需要释放buf
buf.release();

//它不需要释放
//cp.release();
}

}




public static void main(String[] args) throws Exception {
ByteBufAllocator allocator = PooledByteBufAllocator.DEFAULT;
ByteBuf original = allocator.directBuffer(32);
original.writeByte(1);
original.writeByte(2);
original.writeByte(3);
original.writeByte(4);

original.readByte();
original.readByte();

//分配子缓冲区,复制是从原始缓冲区readerIndex开始
ByteBuf sub = original.slice();
sub.readerIndex(0);
//所以slice配的缓冲区自有2个可读字节
System.out.println(sub.readableBytes());

//分配子缓冲区,整个复制
ByteBuf duplicate = original.duplicate();
duplicate.readerIndex(0);
//所以duplicate配的缓冲区自有4个可读字节
System.out.println(duplicate.readableBytes());
}


duplicate负责原始缓冲区整个空间,默认r,w位置与原始缓冲区一致。
public DuplicatedByteBuf(ByteBuf buffer) {
//原始缓冲区, 原始缓冲区当前rw位置
this(buffer, buffer.readerIndex(), buffer.writerIndex());
}

DuplicatedByteBuf(ByteBuf buffer, int readerIndex, int writerIndex) {
super(buffer.maxCapacity());

if (buffer instanceof DuplicatedByteBuf) {
this.buffer = ((DuplicatedByteBuf) buffer).buffer;
} else if (buffer instanceof AbstractPooledDerivedByteBuf) {
this.buffer = buffer.unwrap();
} else {
this.buffer = buffer;
}

setIndex(readerIndex, writerIndex);
markReaderIndex();
markWriterIndex();
}



举报

相关推荐

0 条评论