Fix NETTY-452, add an option for whether to count length field into

packet length or not
This commit is contained in:
Norman Maurer 2011-11-09 20:43:50 +01:00
parent 582b25eaf5
commit db5d74748b

View File

@ -195,6 +195,7 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
private final int lengthFieldEndOffset;
private final int lengthAdjustment;
private final int initialBytesToStrip;
private final boolean lengthFieldIncludedInFrameLength;
private boolean discardingTooLongFrame;
private long tooLongFrameLength;
private long bytesToDiscard;
@ -215,7 +216,7 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
public LengthFieldBasedFrameDecoder(
int maxFrameLength,
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 lengthFieldOffset, int lengthFieldLength,
int lengthAdjustment, int initialBytesToStrip) {
this(maxFrameLength, lengthFieldOffset, lengthFieldLength, 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) {
throw new IllegalArgumentException(
"maxFrameLength must be a positive integer: " +
@ -278,6 +305,7 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
this.lengthAdjustment = lengthAdjustment;
lengthFieldEndOffset = lengthFieldOffset + lengthFieldLength;
this.initialBytesToStrip = initialBytesToStrip;
this.lengthFieldIncludedInFrameLength = lengthFieldIncludedInFrameLength;
}
@Override
@ -326,7 +354,9 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
"negative pre-adjustment length field: " + frameLength);
}
frameLength += lengthAdjustment + lengthFieldEndOffset;
if (!lengthFieldIncludedInFrameLength) {
frameLength += lengthAdjustment + lengthFieldEndOffset;
}
if (frameLength < lengthFieldEndOffset) {
buffer.skipBytes(lengthFieldEndOffset);
throw new CorruptedFrameException(