Add LengthFieldPrepender.lengthAdjustment parameter

This commit is contained in:
kxbmap 2013-05-16 07:52:02 +09:00 committed by Trustin Lee
parent f92cfba388
commit 3a5aa12000
2 changed files with 117 additions and 2 deletions

View File

@ -51,6 +51,7 @@ public class LengthFieldPrepender extends MessageToByteEncoder<ByteBuf> {
private final int lengthFieldLength; private final int lengthFieldLength;
private final boolean lengthIncludesLengthFieldLength; private final boolean lengthIncludesLengthFieldLength;
private final int lengthAdjustment;
/** /**
* Creates a new instance. * Creates a new instance.
@ -79,6 +80,40 @@ public class LengthFieldPrepender extends MessageToByteEncoder<ByteBuf> {
* if {@code lengthFieldLength} is not 1, 2, 3, 4, or 8 * if {@code lengthFieldLength} is not 1, 2, 3, 4, or 8
*/ */
public LengthFieldPrepender(int lengthFieldLength, boolean lengthIncludesLengthFieldLength) { public LengthFieldPrepender(int lengthFieldLength, boolean lengthIncludesLengthFieldLength) {
this(lengthFieldLength, 0, lengthIncludesLengthFieldLength);
}
/**
* Creates a new instance.
*
* @param lengthFieldLength the length of the prepended length field.
* Only 1, 2, 3, 4, and 8 are allowed.
* @param lengthAdjustment the compensation value to add to the value
* of the length field
*
* @throws IllegalArgumentException
* if {@code lengthFieldLength} is not 1, 2, 3, 4, or 8
*/
public LengthFieldPrepender(int lengthFieldLength, int lengthAdjustment) {
this(lengthFieldLength, lengthAdjustment, false);
}
/**
* Creates a new instance.
*
* @param lengthFieldLength the length of the prepended length field.
* Only 1, 2, 3, 4, and 8 are allowed.
* @param lengthAdjustment the compensation value to add to the value
* of the length field
* @param lengthIncludesLengthFieldLength
* if {@code true}, the length of the prepended
* length field is added to the value of the
* prepended length field.
*
* @throws IllegalArgumentException
* if {@code lengthFieldLength} is not 1, 2, 3, 4, or 8
*/
public LengthFieldPrepender(int lengthFieldLength, int lengthAdjustment, boolean lengthIncludesLengthFieldLength) {
if (lengthFieldLength != 1 && lengthFieldLength != 2 && if (lengthFieldLength != 1 && lengthFieldLength != 2 &&
lengthFieldLength != 3 && lengthFieldLength != 4 && lengthFieldLength != 3 && lengthFieldLength != 4 &&
lengthFieldLength != 8) { lengthFieldLength != 8) {
@ -89,6 +124,7 @@ public class LengthFieldPrepender extends MessageToByteEncoder<ByteBuf> {
this.lengthFieldLength = lengthFieldLength; this.lengthFieldLength = lengthFieldLength;
this.lengthIncludesLengthFieldLength = lengthIncludesLengthFieldLength; this.lengthIncludesLengthFieldLength = lengthIncludesLengthFieldLength;
this.lengthAdjustment = lengthAdjustment;
} }
@Override @Override
@ -96,8 +132,15 @@ public class LengthFieldPrepender extends MessageToByteEncoder<ByteBuf> {
ChannelHandlerContext ctx, ChannelHandlerContext ctx,
ByteBuf msg, ByteBuf out) throws Exception { ByteBuf msg, ByteBuf out) throws Exception {
int length = lengthIncludesLengthFieldLength? int length = (lengthIncludesLengthFieldLength?
msg.readableBytes() + lengthFieldLength : msg.readableBytes(); msg.readableBytes() + lengthFieldLength : msg.readableBytes()) +
lengthAdjustment;
if (length < 0) {
throw new IllegalArgumentException(
"Adjusted frame length (" + length + ") is less than zero");
}
switch (lengthFieldLength) { switch (lengthFieldLength) {
case 1: case 1:
if (length >= 256) { if (length >= 256) {

View File

@ -0,0 +1,72 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec.frame;
import io.netty.buffer.ByteBuf;
import io.netty.channel.IncompleteFlushException;
import io.netty.channel.embedded.EmbeddedByteChannel;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.util.CharsetUtil;
import org.junit.Before;
import org.junit.Test;
import static io.netty.buffer.Unpooled.copiedBuffer;
import static io.netty.buffer.Unpooled.wrappedBuffer;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
public class LengthFieldPrependerTest {
private ByteBuf msg;
@Before
public void setUp() throws Exception {
msg = copiedBuffer("A", CharsetUtil.ISO_8859_1);
}
@Test
public void testPrependLength() throws Exception {
final EmbeddedByteChannel ch = new EmbeddedByteChannel(new LengthFieldPrepender(4));
ch.writeOutbound(msg);
assertThat(ch.readOutbound(), is(wrappedBuffer(new byte[]{0, 0, 0, 1, 'A'})));
}
@Test
public void testPrependLengthIncludesLengthFieldLength() throws Exception {
final EmbeddedByteChannel ch = new EmbeddedByteChannel(new LengthFieldPrepender(4, true));
ch.writeOutbound(msg);
assertThat(ch.readOutbound(), is(wrappedBuffer(new byte[]{0, 0, 0, 5, 'A'})));
}
@Test
public void testPrependAdjustedLength() throws Exception {
final EmbeddedByteChannel ch = new EmbeddedByteChannel(new LengthFieldPrepender(4, -1));
ch.writeOutbound(msg);
assertThat(ch.readOutbound(), is(wrappedBuffer(new byte[]{0, 0, 0, 0, 'A'})));
}
@Test
public void testAdjustedLengthLessThanZero() throws Exception {
final EmbeddedByteChannel ch = new EmbeddedByteChannel(new LengthFieldPrepender(4, -2));
try {
ch.writeOutbound(msg);
fail(IncompleteFlushException.class.getSimpleName() + " must be raised.");
} catch (IncompleteFlushException e) {
// Expected
}
}
}