【初学与研发之NETTY】netty4之对象、字节传输

netty4中的对象、字节传输做了封装,writeAndFlush中提供的参数Object可以直接的写入对象、byte[]。不管是字节数组还是对象,关键部分的代码如下:

new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    
		    p.addLast("encoder", new ObjectEncoder());
                    p.addLast("decoder", new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
                   
                    p.addLast("handler", new UploadServerHandler());
                }
             }


其中,new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)),Integer.MAX_VALUE是设置传输的对象大小。默认为1M,设置如:

new ObjectDecoder(ClassResolvers.cacheDisabled(null))

 

xxxHandler类继承ChannelInboundHandlerAdapter。

 

channel.writeAndFlush(new Object());

 

channel.writeAndFlush(new byte[10]);

你可能感兴趣的:(netty4,对象传输,字节传输)