Skip to content

Commit

Permalink
FixedLengthFrameDecoder should used a optimizated initialSize when
Browse files Browse the repository at this point in the history
creating the cumulative ChannelBuffer. See netty#170
  • Loading branch information
normanmaurer committed Jan 31, 2012
1 parent 02f49af commit 9c0aa0c
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.netty.handler.codec.frame;

import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

Expand All @@ -38,20 +39,30 @@
public class FixedLengthFrameDecoder extends FrameDecoder {

private final int frameLength;
private final boolean allocateFullBuffer;

/**
* Calls {@link #FixedLengthFrameDecoder(int, boolean)} with <code>false</code>
*/
public FixedLengthFrameDecoder(int frameLength) {
this(frameLength, false);
}

/**
* Creates a new instance.
*
* @param frameLength the length of the frame
* @param allocateFullBuffer <code>true</code> if the cumulative {@link ChannelBuffer} should use the {@link #frameLength} as its initial size
*/
public FixedLengthFrameDecoder(int frameLength) {
public FixedLengthFrameDecoder(int frameLength, boolean allocateFullBuffer) {
if (frameLength <= 0) {
throw new IllegalArgumentException(
"frameLength must be a positive integer: " + frameLength);
}
this.frameLength = frameLength;
this.allocateFullBuffer = allocateFullBuffer;
}

@Override
protected Object decode(
ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
Expand All @@ -61,4 +72,13 @@ protected Object decode(
return buffer.readBytes(frameLength);
}
}

@Override
protected ChannelBuffer createCumulationDynamicBuffer(ChannelHandlerContext ctx) {
if (allocateFullBuffer) {
return ChannelBuffers.dynamicBuffer(frameLength, ctx.getChannel().getConfig().getBufferFactory());
}
return super.createCumulationDynamicBuffer(ctx);
}

}

0 comments on commit 9c0aa0c

Please sign in to comment.