java netty之ChannelInboundByteHandlerAdapter

这篇文章分析另外一个表重要比较基础的handler : ChannelInboundByteHandlerAdapter

当我们编写的服务器程序需要直接处理byte的时候,可以直接扩展它来实现inboundhandler,用于处理传送过来的byte数据。。。例如举一个典型的用法:

public class MyChannelHandler extends ChannelInboundByteHandlerAdapter {
	@Override
	protected void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in)
			throws Exception {
		// TODO Auto-generated method stub
		//while (in.isReadable()) {
			//System.out.print((char)in.readByte());
		//}
		final String name = ctx.name();
		ByteBuf b = ctx.alloc().buffer();   //创建一个buffer用于存放数据
		b.writeBytes("aaa".getBytes());
		ctx.pipeline().write(b);   //调用pipeline的write方法,将数据发送出去
		ctx.pipeline().flush().addListener(new ChannelFutureListener(){

			@Override
			public void operationComplete(ChannelFuture future)
					throws Exception {
				// TODO Auto-generated method stub
				System.out.println(name);
				future.channel().close().sync();
			}
		});	
	}
}

该类直接继承自ChannelInboundByteHandlerAdapter,然后实现了inboundBufferUpdated方法用于处理读取进来的数据。。。将这个类的对象加入到channel的pipeline上面之后,当这个channel已经有数据读进来之后会调用inboundBufferUpdated方法来处理。。。

我们首先还是来看一下ChannelInboundByteHandlerAdapter的继承体系:


ChannelInboundByteHandlerAdapter直接继承了ChannelStateHandlerAdapter,这个类型在上面的一篇文章中也有说明,它继承了stathandler接口,然后实现了里面的方法,不过这些方法实现的都很简单,无非是从当前pipeline上面向下寻找到另一个stathandler,调用相应的方法。。。

然后ChannelInboundByteHandlerAdapter还实现了ChannelInboundByteHandler接口,这个接口中多定义了一个方法,那就是discardInboundReadBytes。。。

好了,接下来我们来看看ChannelInboundByteHandlerAdapter的定义吧:

public abstract class ChannelInboundByteHandlerAdapter
        extends ChannelStateHandlerAdapter implements ChannelInboundByteHandler {

    /**
     * Create a new unpooled {@link ByteBuf} by default. Sub-classes may override this to offer a more
     * optimized implementation.
     */
    @Override
    //用于构创建新的buffer
    public ByteBuf newInboundBuffer(ChannelHandlerContext ctx) throws Exception {
        return ChannelHandlerUtil.allocate(ctx);
    }

    @Override
    //抛弃已经读取的数据
    public void discardInboundReadBytes(ChannelHandlerContext ctx) throws Exception {
        ctx.inboundByteBuffer().discardSomeReadBytes();
    }

    @Override
    //当有新的收读取到时候,会调用过这个方法来处理这些读取到的数据
    public final void inboundBufferUpdated(ChannelHandlerContext ctx) throws Exception {
        inboundBufferUpdated(ctx, ctx.inboundByteBuffer());
    }

    /**
     * Callback which will get notifed once the given {@link ByteBuf} received more data to read. What will be done
     * with the data at this point is up to the implementation.
     * Implementations may choose to read it or just let it in the buffer to read it later.
     */
    //用户自己实现这个方法,真正的用于处理读取到的数据
    protected abstract void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) throws Exception;
}

实现代码还是很简单,首先重载了newInboundBuffer方法,至于inboundBufferUpdated方法,其实是用来调用用户自定义的inboundBufferUpdated方法来来处理新进来的数据。。

因此用户编码的时候,只需要继承ChannelInboundByteHandlerAdapter类,然后实现inboundBufferUpdated方法就可以了。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值