Added LengthPrefixedFrameDecoder

This commit is contained in:
Trustin Lee 2009-01-06 05:47:41 +00:00
parent 5d6ef046cd
commit 73a6716c4b
2 changed files with 71 additions and 1 deletions

View File

@ -40,7 +40,7 @@ public class LengthFieldBasedFrameDecoder extends FrameDecoder {
private final int maxFrameLength; private final int maxFrameLength;
private final int lengthFieldOffset; private final int lengthFieldOffset;
private final int lengthFieldLength; final int lengthFieldLength;
private final int lengthFieldEndOffset; private final int lengthFieldEndOffset;
private final int lengthAdjustment; private final int lengthAdjustment;
private volatile boolean discardingTooLongFrame; private volatile boolean discardingTooLongFrame;

View File

@ -0,0 +1,70 @@
/*
* 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;
}
}