leutu
2024-06-03 3ef35e6cd16bbfa206b26bb3271eac40ad020bcb
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.fastbee.base.codec;
 
import static io.netty.util.internal.ObjectUtil.checkPositive;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
 
/**
 * 固定长度报文处理器
 * 处理固定长度报文,tcp粘包处理
 * @author bill
 */
public class LengthField {
 
    public final byte[] prefix;
    /*最大帧长度*/
    public final int lengthFieldMaxFrameLength;
    /*偏移量*/
    public final int lengthFieldOffset;
    /*字段长度*/
    public final int lengthFieldLength;
    /*结尾偏移量*/
    public final int lengthFieldEndOffset;
    /*报文调整 默认0,不调整*/
    public final int lengthAdjustment;
    /*,默认0*/
    public final int initialBytesToStrip;
 
    /**构造固定长度处理器*/
    public LengthField(byte[] prefix, int maxFrameLength, int lengthFieldOffset, int lengthFieldLength) {
        this(prefix, maxFrameLength, lengthFieldOffset, lengthFieldLength, 0, 0);
    }
 
    public LengthField(byte[] prefix, int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip) {
        checkPositive(maxFrameLength, "maxFrameLength_LengthField");
        checkPositiveOrZero(lengthFieldOffset, "lengthFieldOffset");
        checkPositiveOrZero(initialBytesToStrip, "initialBytesToStrip");
        if (lengthFieldOffset > maxFrameLength - lengthFieldLength) {
            throw new IllegalArgumentException("maxFrameLength_LengthField (" + maxFrameLength + ") must be equal to or greater than lengthFieldOffset (" + lengthFieldOffset + ") + lengthFieldLength (" + lengthFieldLength + ").");
        } else {
            this.prefix = prefix;
            this.lengthFieldMaxFrameLength = maxFrameLength;
            this.lengthFieldOffset = lengthFieldOffset;
            this.lengthFieldLength = lengthFieldLength;
            this.lengthAdjustment = lengthAdjustment;
            this.lengthFieldEndOffset = lengthFieldOffset + lengthFieldLength;
            this.initialBytesToStrip = initialBytesToStrip;
        }
    }
 
    public byte[] getPrefix() {
        return prefix;
    }
 
    public int getLengthFieldMaxFrameLength() {
        return lengthFieldMaxFrameLength;
    }
 
    public int getLengthFieldOffset() {
        return lengthFieldOffset;
    }
 
    public int getLengthFieldLength() {
        return lengthFieldLength;
    }
 
    public int getLengthFieldEndOffset() {
        return lengthFieldEndOffset;
    }
 
    public int getLengthAdjustment() {
        return lengthAdjustment;
    }
 
    public int getInitialBytesToStrip() {
        return initialBytesToStrip;
    }
}