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

 

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

 

2023-07-14 15:19:01.215 WARN 7308 --- [sson-netty-2-15] io.netty.util.concurrent.DefaultPromise : An exception was thrown by org.redisson.misc.RedissonPromise$$Lambda$888/0x00000008008f7440.operationComplete() java.lang.NullPointerException: null 2023-07-14 15:19:01.216 ERROR 7308 --- [sson-netty-2-15] o.r.c.SentinelConnectionManager : Can't execute SENTINEL commands on /172.24.107.11:26379 org.redisson.client.RedisException: ERR No such master with that name. channel: [id: 0x2d66827d, L:/172.23.9.103:46812 - R:/172.24.107.11:26379] command: (SENTINEL SLAVES), params: [mymaster] at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:365) ~[redisson-3.13.3.jar:3.13.3] at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:196) ~[redisson-3.13.3.jar:3.13.3] at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:134) ~[redisson-3.13.3.jar:3.13.3] at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:104) ~[redisson-3.13.3.jar:3.13.3] at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:501) ~[netty-codec-4.1.51.Final.jar:4.1.51.Final] at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366) ~[netty-codec-4.1.51.Final.jar:4.1.51.Final] at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276) ~[netty-codec-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) ~[netty-transport-4.1.51.Final.jar:4.1.51.Final] at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.51.Final.jar:4.1.51.Final] at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.51.Final.jar:4.1.51.Final] at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.51.Final.jar:4.1.51.Final] at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na] 解决方法
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值