From 8bba7ccb5b68157d5467df97ff4a7c6277ffe334 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Tue, 6 Jan 2009 05:59:42 +0000 Subject: [PATCH] Merged LengthPrefixedFrameDecoder into LengthFieldBasedFrameDecoder by introducing initialBytesToStrip option --- .../frame/LengthFieldBasedFrameDecoder.java | 28 ++++++-- .../frame/LengthPrefixedFrameDecoder.java | 70 ------------------- 2 files changed, 22 insertions(+), 76 deletions(-) delete mode 100644 src/main/java/org/jboss/netty/handler/codec/frame/LengthPrefixedFrameDecoder.java diff --git a/src/main/java/org/jboss/netty/handler/codec/frame/LengthFieldBasedFrameDecoder.java b/src/main/java/org/jboss/netty/handler/codec/frame/LengthFieldBasedFrameDecoder.java index 45222866ce..6c626d7f9b 100644 --- a/src/main/java/org/jboss/netty/handler/codec/frame/LengthFieldBasedFrameDecoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/frame/LengthFieldBasedFrameDecoder.java @@ -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); } } diff --git a/src/main/java/org/jboss/netty/handler/codec/frame/LengthPrefixedFrameDecoder.java b/src/main/java/org/jboss/netty/handler/codec/frame/LengthPrefixedFrameDecoder.java deleted file mode 100644 index 7dd4d21006..0000000000 --- a/src/main/java/org/jboss/netty/handler/codec/frame/LengthPrefixedFrameDecoder.java +++ /dev/null @@ -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; - } -}