netty 常用编解码器说明


netty 常用编解码器说明

        

                

                             

常用编码器

         

StringEncoder:字符串编码器

@Sharable
public class StringEncoder extends MessageToMessageEncoder<CharSequence> {
    private final Charset charset;

    public StringEncoder() {
        this(Charset.defaultCharset());
    }

    public StringEncoder(Charset charset) {
        this.charset = (Charset)ObjectUtil.checkNotNull(charset, "charset");
    }

    protected void encode(ChannelHandlerContext ctx, CharSequence msg, List<Object> out) throws Exception {
        if (msg.length() != 0) {
            out.add(ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(msg), this.charset));
        }
    }
}

            

ObjectEncoder:对象序列化

@Sharable
public class ObjectEncoder extends MessageToByteEncoder<Serializable> {
    private static final byte[] LENGTH_PLACEHOLDER = new byte[4];

    public ObjectEncoder() {
    }

    protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
        int startIdx = out.writerIndex();
        ByteBufOutputStream bout = new ByteBufOutputStream(out);
        CompactObjectOutputStream oout = null;

        try {
            bout.write(LENGTH_PLACEHOLDER);
            oout = new CompactObjectOutputStream(bout);
            oout.writeObject(msg);
            oout.flush();
        } finally {
            if (oout != null) {
                oout.close();
            } else {
                bout.close();
            }

        }

        int endIdx = out.writerIndex();
        out.setInt(startIdx, endIdx - startIdx - 4);
    }
}

      

                           

                             

常用解码器

     

StringDecoder:字符串解码器

@Sharable
public class StringDecoder extends MessageToMessageDecoder<ByteBuf> {
    private final Charset charset;

    public StringDecoder() {
        this(Charset.defaultCharset());
    }

    public StringDecoder(Charset charset) {
        this.charset = (Charset)ObjectUtil.checkNotNull(charset, "charset");
    }

    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
        out.add(msg.toString(this.charset));
    }
}

            

ObjectDecoder:对象反序列化

public class ObjectDecoder extends LengthFieldBasedFrameDecoder {
    private final ClassResolver classResolver;

    public ObjectDecoder(ClassResolver classResolver) {
        this(1048576, classResolver);
    }

    public ObjectDecoder(int maxObjectSize, ClassResolver classResolver) {
        super(maxObjectSize, 0, 4, 0, 4);
        this.classResolver = classResolver;
    }

    protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        ByteBuf frame = (ByteBuf)super.decode(ctx, in);
        if (frame == null) {
            return null;
        } else {
            CompactObjectInputStream ois = new CompactObjectInputStream(new ByteBufInputStream(frame, true), this.classResolver);

            Object var5;
            try {
                var5 = ois.readObject();
            } finally {
                ois.close();
            }

            return var5;
        }
    }
}

          

LengthFieldBasedFrameDecoder:读取指定位置偏移、数据长度的解码器

public class LengthFieldBasedFrameDecoder extends ByteToMessageDecoder {
    private final ByteOrder byteOrder;
    private final int maxFrameLength;
    private final int lengthFieldOffset;    //长度字段start偏移量
    private final int lengthFieldLength;    //length字段长度
    private final int lengthFieldEndOffset; //长度字段end偏移量
    private final int lengthAdjustment;     //length起始偏移量调整
    private final int initialBytesToStrip;  //跳过的字节数
    private final boolean failFast;
    private boolean discardingTooLongFrame;
    private long tooLongFrameLength;
    private long bytesToDiscard;
    private int frameLengthInt;

    public LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength) {
    public LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip) {
    public LengthFieldBasedFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast) {
    public LengthFieldBasedFrameDecoder(ByteOrder byteOrder, int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast) {


    protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order) {
    protected ByteBuf extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length) {

    private void fail(long frameLength) {
    private void discardingTooLongFrame(ByteBuf in) {
    private void exceededFrameLength(ByteBuf in, long frameLength) {
    private void failIfNecessary(boolean firstDetectionOfTooLongFrame) {
    private static void failOnFrameLengthLessThanInitialBytesToStrip(ByteBuf in, long frameLength, int initialBytesToStrip) {
    private static void failOnFrameLengthLessThanLengthFieldEndOffset(ByteBuf in, long frameLength, int lengthFieldEndOffset) {
    private static void failOnNegativeLengthField(ByteBuf in, long frameLength, int lengthFieldEndOffset) {

           

LineBasedFrameDecoder:解析以\n、\r\n结尾的二进制流

public class LineBasedFrameDecoder extends ByteToMessageDecoder {
    private final int maxLength;          //解析的最大长度
    private final boolean failFast;       //超出最大长度是否抛出异常,默认false
    private final boolean stripDelimiter; //是否不包含分隔符,默认true
    private boolean discarding;           //是否为丢弃模式:true(丢弃分隔符之前的数据)
                                                         false(当前到分隔符之间的数据不丢弃)
    private int discardedBytes;           //需要丢弃的字节数
    private int offset;                   //读数据的起始偏移量

    public LineBasedFrameDecoder(int maxLength) {
    public LineBasedFrameDecoder(int maxLength, boolean stripDelimiter, boolean failFast) {

    protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {

    private void fail(ChannelHandlerContext ctx, int length) {
    private void fail(ChannelHandlerContext ctx, String length) {
    private int findEndOfLine(ByteBuf buffer) {

          

DelimiterBasedFrameDecoder:解析指定分隔符的二进制流

public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder {
    private final ByteBuf[] delimiters;      //指定分隔符
    private final int maxFrameLength;        //最大长度
    private final boolean stripDelimiter;    //解析后的数据是否不包含分隔符,默认true
    private final boolean failFast;          //超出最大长度是否抛出异常,默认true
    private boolean discardingTooLongFrame;
    private int tooLongFrameLength;
    private final LineBasedFrameDecoder lineBasedDecoder;

    public DelimiterBasedFrameDecoder(int maxFrameLength, ByteBuf delimiter) {
    public DelimiterBasedFrameDecoder(int maxFrameLength, boolean stripDelimiter, ByteBuf delimiter) {
    public DelimiterBasedFrameDecoder(int maxFrameLength, boolean stripDelimiter, boolean failFast, ByteBuf delimiter) {
    public DelimiterBasedFrameDecoder(int maxFrameLength, ByteBuf... delimiters) {
    public DelimiterBasedFrameDecoder(int maxFrameLength, boolean stripDelimiter, ByteBuf... delimiters) {
    public DelimiterBasedFrameDecoder(int maxFrameLength, boolean stripDelimiter, boolean failFast, ByteBuf... delimiters) {


    protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {

    private boolean isSubclass() {
    private void fail(long frameLength) {
    private static boolean isLineBased(ByteBuf[] delimiters) {
    private static int indexOf(ByteBuf haystack, ByteBuf needle) {
    private static void validateDelimiter(ByteBuf delimiter) {
    private static void validateMaxFrameLength(int maxFrameLength) {

             

FixedLengthFrameDecoder:解析固定长度的二进制流

public class FixedLengthFrameDecoder extends ByteToMessageDecoder {
    private final int frameLength;

    public FixedLengthFrameDecoder(int frameLength) {
        ObjectUtil.checkPositive(frameLength, "frameLength");
        this.frameLength = frameLength;
    }

    protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        Object decoded = this.decode(ctx, in);
        if (decoded != null) {
            out.add(decoded);
        }

    }

    protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        return in.readableBytes() < this.frameLength ? null : in.readRetainedSlice(this.frameLength);
    }   //可读字节长度小于设置的长度,返回null,下次读取的时候从累加器中读取frameLength长度的数据
}

        

               

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值