package com.ld.io.netty; import com.ld.io.api.IoException; import com.ld.io.api.IoServerOption; import com.ld.io.api.SplitByteDecoderType; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.handler.codec.*; import io.netty.handler.codec.compression.SnappyFrameDecoder; public class SplitDecoderFactory { public ByteToMessageDecoder build(IoServerOption ioServerOption) { int splitDecoderType = ioServerOption.getSplitDecoderType(); ByteToMessageDecoder result; switch (splitDecoderType) { case SplitByteDecoderType.DELIMITER_SYMBOL: ByteBuf byteBuf = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer(ioServerOption.getDelimiter())); result = new DelimiterBasedFrameDecoder(ioServerOption.getMaxFrameSize(), byteBuf); break; case SplitByteDecoderType.ENTER_SYMBOL: result = new LineBasedFrameDecoder(ioServerOption.getMaxFrameSize()); break; case SplitByteDecoderType.FIXED_LENGTH: result = new FixedLengthFrameDecoder(ioServerOption.getFixedLength()); break; case SplitByteDecoderType.LENGTH_FIELD: result = new LengthFieldBasedFrameDecoder(ioServerOption.getMaxFrameSize(), 0, ioServerOption.getLengthFieldLength()); break; case SplitByteDecoderType.NO_LIMIT: result = null; break; default: throw new IoException("split decoder type not exist"); } return result; } }