jiazx0107
2025-09-01 ce4f9b9f72a4269a1f25812dadd59bfb92c7b3cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
    }
}