Make failImmediatelyOnTooLongFrame configurable via a constructor

This commit is contained in:
norman 2011-11-10 09:15:16 +01:00
parent 193acdb36c
commit 3856f99c03
2 changed files with 22 additions and 13 deletions

View File

@ -69,7 +69,7 @@ public class DelimiterBasedFrameDecoder extends FrameDecoder {
private final boolean stripDelimiter;
private boolean discardingTooLongFrame;
private int tooLongFrameLength;
private boolean failImmediatelyOnTooLongFrame = false;
private final boolean failImmediatelyOnTooLongFrame;
/**
* Creates a new instance.
@ -103,6 +103,7 @@ public class DelimiterBasedFrameDecoder extends FrameDecoder {
};
this.maxFrameLength = maxFrameLength;
this.stripDelimiter = stripDelimiter;
this.failImmediatelyOnTooLongFrame = false;
}
/**
@ -117,6 +118,14 @@ public class DelimiterBasedFrameDecoder extends FrameDecoder {
this(maxFrameLength, true, delimiters);
}
/**
* Calls {@link #DelimiterBasedFrameDecoder(int, boolean, boolean, ChannelBuffer...)} with failImmediatelyOnTooLongFrame set to <code>false</code>
*/
public DelimiterBasedFrameDecoder(
int maxFrameLength, boolean stripDelimiter, ChannelBuffer... delimiters) {
this(maxFrameLength, stripDelimiter, false, delimiters);
}
/**
* Creates a new instance.
*
@ -125,10 +134,16 @@ public class DelimiterBasedFrameDecoder extends FrameDecoder {
* the length of the frame exceeds this value.
* @param stripDelimiter whether the decoded frame should strip out the
* delimiter or not
* @param failImmediatelyOnTooLongFrame If false (the default) a {@link TooLongFrameException}
* is thrown if the length of the frame exceeds maxFrameLength,
* after the delimiter has been read.
* If true a {@link TooLongFrameException} is thrown immediately
* when the length of the frame exceeds maxFrameLength,
* regardless of whether a delimiter has been found yet.
* @param delimiters the delimiters
*/
public DelimiterBasedFrameDecoder(
int maxFrameLength, boolean stripDelimiter, ChannelBuffer... delimiters) {
int maxFrameLength, boolean stripDelimiter, boolean failImmediatelyOnTooLongFrame, ChannelBuffer... delimiters) {
validateMaxFrameLength(maxFrameLength);
if (delimiters == null) {
throw new NullPointerException("delimiters");
@ -144,8 +159,9 @@ public class DelimiterBasedFrameDecoder extends FrameDecoder {
}
this.maxFrameLength = maxFrameLength;
this.stripDelimiter = stripDelimiter;
this.failImmediatelyOnTooLongFrame = failImmediatelyOnTooLongFrame;
}
@Override
protected Object decode(
ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
@ -216,17 +232,11 @@ public class DelimiterBasedFrameDecoder extends FrameDecoder {
/**
* Set the behavior when a frame longer than maxFrameLength is encountered.
*
* @param failImmediatelyOnTooLongFrame If false (the default) a {@link TooLongFrameException}
* is thrown if the length of the frame exceeds maxFrameLength,
* after the delimiter has been read.
* If true a {@link TooLongFrameException} is thrown immediately
* when the length of the frame exceeds maxFrameLength,
* regardless of whether a delimiter has been found yet.
*
*/
public DelimiterBasedFrameDecoder setFailImmediatelyOnTooLongFrame(
boolean failImmediatelyOnTooLongFrame)
)
{
this.failImmediatelyOnTooLongFrame = failImmediatelyOnTooLongFrame;
return this;
}

View File

@ -50,8 +50,7 @@ public class DelimiterBasedFrameDecoderTest {
@Test
public void testFailImmediatelyTooLongFrameRecovery() throws Exception {
DecoderEmbedder<ChannelBuffer> embedder = new DecoderEmbedder<ChannelBuffer>(
new DelimiterBasedFrameDecoder(1, Delimiters.nulDelimiter()).
setFailImmediatelyOnTooLongFrame(true));
new DelimiterBasedFrameDecoder(1, true, true, Delimiters.nulDelimiter()));
for (int i = 0; i < 2; i ++) {
try {