add an option in LengthFieldBasedFrameDecoder for whether to count the length field into frame length.
This commit is contained in:
parent
cc758aa3b6
commit
942b746d73
@ -195,6 +195,7 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
|
|||||||
private final int lengthFieldEndOffset;
|
private final int lengthFieldEndOffset;
|
||||||
private final int lengthAdjustment;
|
private final int lengthAdjustment;
|
||||||
private final int initialBytesToStrip;
|
private final int initialBytesToStrip;
|
||||||
|
private final boolean lengthFieldIncludedInFrameLength;
|
||||||
private boolean discardingTooLongFrame;
|
private boolean discardingTooLongFrame;
|
||||||
private long tooLongFrameLength;
|
private long tooLongFrameLength;
|
||||||
private long bytesToDiscard;
|
private long bytesToDiscard;
|
||||||
@ -215,7 +216,7 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
|
|||||||
public LengthFieldBasedFrameDecoder(
|
public LengthFieldBasedFrameDecoder(
|
||||||
int maxFrameLength,
|
int maxFrameLength,
|
||||||
int lengthFieldOffset, int lengthFieldLength) {
|
int lengthFieldOffset, int lengthFieldLength) {
|
||||||
this(maxFrameLength, lengthFieldOffset, lengthFieldLength, 0, 0);
|
this(maxFrameLength, lengthFieldOffset, lengthFieldLength, 0, 0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -238,6 +239,32 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
|
|||||||
int maxFrameLength,
|
int maxFrameLength,
|
||||||
int lengthFieldOffset, int lengthFieldLength,
|
int lengthFieldOffset, int lengthFieldLength,
|
||||||
int lengthAdjustment, int initialBytesToStrip) {
|
int lengthAdjustment, int initialBytesToStrip) {
|
||||||
|
this(maxFrameLength, lengthFieldOffset, lengthFieldOffset, lengthAdjustment,
|
||||||
|
initialBytesToStrip, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new instance.
|
||||||
|
*
|
||||||
|
* @param maxFrameLength
|
||||||
|
* the maximum length of the frame. If the length of the frame is
|
||||||
|
* greater than this value, {@link TooLongFrameException} will be
|
||||||
|
* thrown.
|
||||||
|
* @param lengthFieldOffset
|
||||||
|
* the offset of the length field
|
||||||
|
* @param lengthFieldLength
|
||||||
|
* the length of the length field
|
||||||
|
* @param lengthAdjustment
|
||||||
|
* the compensation value to add to the value of the length field
|
||||||
|
* @param initialBytesToStrip
|
||||||
|
* the number of first bytes to strip out from the decoded frame
|
||||||
|
* @param lengthFieldIncludedInFrameLength
|
||||||
|
* whether to count length field into frame length
|
||||||
|
*/
|
||||||
|
public LengthFieldBasedFrameDecoder(
|
||||||
|
int maxFrameLength,
|
||||||
|
int lengthFieldOffset, int lengthFieldLength,
|
||||||
|
int lengthAdjustment, int initialBytesToStrip, boolean lengthFieldIncludedInFrameLength) {
|
||||||
if (maxFrameLength <= 0) {
|
if (maxFrameLength <= 0) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"maxFrameLength must be a positive integer: " +
|
"maxFrameLength must be a positive integer: " +
|
||||||
@ -278,6 +305,7 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
|
|||||||
this.lengthAdjustment = lengthAdjustment;
|
this.lengthAdjustment = lengthAdjustment;
|
||||||
lengthFieldEndOffset = lengthFieldOffset + lengthFieldLength;
|
lengthFieldEndOffset = lengthFieldOffset + lengthFieldLength;
|
||||||
this.initialBytesToStrip = initialBytesToStrip;
|
this.initialBytesToStrip = initialBytesToStrip;
|
||||||
|
this.lengthFieldIncludedInFrameLength = lengthFieldIncludedInFrameLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -326,7 +354,9 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
|
|||||||
"negative pre-adjustment length field: " + frameLength);
|
"negative pre-adjustment length field: " + frameLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
frameLength += lengthAdjustment + lengthFieldEndOffset;
|
if (!lengthFieldIncludedInFrameLength) {
|
||||||
|
frameLength += lengthAdjustment + lengthFieldEndOffset;
|
||||||
|
}
|
||||||
if (frameLength < lengthFieldEndOffset) {
|
if (frameLength < lengthFieldEndOffset) {
|
||||||
buffer.skipBytes(lengthFieldEndOffset);
|
buffer.skipBytes(lengthFieldEndOffset);
|
||||||
throw new CorruptedFrameException(
|
throw new CorruptedFrameException(
|
||||||
|
Loading…
Reference in New Issue
Block a user