Merged LengthPrefixedFrameDecoder into LengthFieldBasedFrameDecoder by introducing initialBytesToStrip option

This commit is contained in:
Trustin Lee 2009-01-06 05:59:42 +00:00
parent 73a6716c4b
commit 8bba7ccb5b
2 changed files with 22 additions and 76 deletions

View File

@ -40,9 +40,10 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
private final int maxFrameLength;
private final int lengthFieldOffset;
final int lengthFieldLength;
private final int lengthFieldLength;
private final int lengthFieldEndOffset;
private final int lengthAdjustment;
private final int initialBytesToStrip;
private volatile boolean discardingTooLongFrame;
private volatile long tooLongFrameLength;
private volatile long bytesToDiscard;
@ -53,7 +54,7 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
public LengthFieldBasedFrameDecoder(
int maxFrameLength,
int lengthFieldOffset, int lengthFieldLength) {
this(maxFrameLength, lengthFieldOffset, lengthFieldLength, 0);
this(maxFrameLength, lengthFieldOffset, lengthFieldLength, 0, 0);
}
/**
@ -61,7 +62,8 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
*/
public LengthFieldBasedFrameDecoder(
int maxFrameLength,
int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment) {
int lengthFieldOffset, int lengthFieldLength,
int lengthAdjustment, int initialBytesToStrip) {
if (maxFrameLength <= 0) {
throw new IllegalArgumentException(
"maxFrameLength must be a positive integer: " +
@ -74,6 +76,12 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
lengthFieldOffset);
}
if (initialBytesToStrip < 0) {
throw new IllegalArgumentException(
"initialBytesToStrip must be a non-negative integer: " +
initialBytesToStrip);
}
if (lengthFieldLength != 1 && lengthFieldLength != 2 &&
lengthFieldLength != 3 && lengthFieldLength != 4 &&
lengthFieldLength != 8) {
@ -95,6 +103,7 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
this.lengthFieldLength = lengthFieldLength;
this.lengthAdjustment = lengthAdjustment;
lengthFieldEndOffset = lengthFieldOffset + lengthFieldLength;
this.initialBytesToStrip = initialBytesToStrip;
}
@Override
@ -158,8 +167,8 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
if (frameLength < lengthFieldEndOffset) {
buffer.skipBytes(lengthFieldEndOffset);
throw new CorruptedFrameException(
"Adjusted length (" + frameLength + ") is less than " +
lengthFieldEndOffset);
"Adjusted frame length (" + frameLength + ") is less " +
"than lengthFieldEndOffset: " + lengthFieldEndOffset);
}
if (frameLength > maxFrameLength) {
@ -177,6 +186,13 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
return null;
}
return buffer.readBytes(frameLengthInt);
if (initialBytesToStrip > frameLengthInt) {
buffer.skipBytes(frameLengthInt);
throw new CorruptedFrameException(
"Adjusted frame length (" + frameLength + ") is less " +
"than initialBytesToStrip: " + initialBytesToStrip);
}
buffer.skipBytes(initialBytesToStrip);
return buffer.readBytes(frameLengthInt - initialBytesToStrip);
}
}

View File

@ -1,70 +0,0 @@
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.netty.handler.codec.frame;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
/**
* A specialized version of {@link LengthFieldBasedFrameDecoder} which assumes
* the length field is always located at offset {@code 0} and removes the length
* field from the decoded frame.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
*/
public class LengthPrefixedFrameDecoder extends LengthFieldBasedFrameDecoder {
private final boolean stripLengthField;
public LengthPrefixedFrameDecoder(int maxFrameLength, int lengthFieldLength) {
this(maxFrameLength, lengthFieldLength, 0, true);
}
public LengthPrefixedFrameDecoder(int maxFrameLength, int lengthFieldLength, boolean stripLengthField) {
this(maxFrameLength, lengthFieldLength, 0, stripLengthField);
}
public LengthPrefixedFrameDecoder(int maxFrameLength, int lengthFieldLength, int lengthAdjustment) {
this(maxFrameLength, lengthFieldLength, lengthAdjustment, true);
}
public LengthPrefixedFrameDecoder(int maxFrameLength, int lengthFieldLength, int lengthAdjustment, boolean stripLengthField) {
super(maxFrameLength, 0, lengthFieldLength, lengthAdjustment);
this.stripLengthField = stripLengthField;
}
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel,
ChannelBuffer buffer) throws Exception {
ChannelBuffer frame = (ChannelBuffer) super.decode(ctx, channel, buffer);
if (stripLengthField && frame != null) {
frame.skipBytes(lengthFieldLength);
return frame.slice();
}
return frame;
}
}