Make ReplayingDecoder.newCumulationBuffer() overridable / Make Replaying|FrameDecoder allocate at least 256 bytes by default

This commit is contained in:
Trustin Lee 2012-02-29 10:04:05 -08:00
parent c46593266d
commit 689c47980b
3 changed files with 15 additions and 35 deletions

View File

@ -361,6 +361,6 @@ public abstract class FrameDecoder extends SimpleChannelUpstreamHandler {
ChannelHandlerContext ctx, int minimumCapacity) {
ChannelBufferFactory factory = ctx.getChannel().getConfig().getBufferFactory();
return ChannelBuffers.dynamicBuffer(
factory.getDefaultOrder(), minimumCapacity, factory);
factory.getDefaultOrder(), Math.max(minimumCapacity, 256), factory);
}
}

View File

@ -18,6 +18,7 @@ package io.netty.handler.codec.replay;
import java.net.SocketAddress;
import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBufferFactory;
import io.netty.buffer.ChannelBuffers;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
@ -596,9 +597,19 @@ public abstract class ReplayingDecoder<T extends Enum<T>>
}
}
private ChannelBuffer newCumulationBuffer(
/**
* Create a new {@link ChannelBuffer} which is used for the cumulation.
* Be aware that this MUST be a dynamic buffer. Sub-classes may override
* this to provide a dynamic {@link ChannelBuffer} which has some
* pre-allocated size that better fit their need.
*
* @param ctx {@link ChannelHandlerContext} for this handler
* @return buffer the {@link ChannelBuffer} which is used for cumulation
*/
protected ChannelBuffer newCumulationBuffer(
ChannelHandlerContext ctx, int minimumCapacity) {
return new UnsafeDynamicChannelBuffer(
ctx.getChannel().getConfig().getBufferFactory(), minimumCapacity);
ChannelBufferFactory factory = ctx.getChannel().getConfig().getBufferFactory();
return ChannelBuffers.dynamicBuffer(
factory.getDefaultOrder(), Math.max(minimumCapacity, 256), factory);
}
}

View File

@ -1,31 +0,0 @@
/*
* Copyright 2011 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.replay;
import io.netty.buffer.ChannelBufferFactory;
import io.netty.buffer.DynamicChannelBuffer;
class UnsafeDynamicChannelBuffer extends DynamicChannelBuffer {
UnsafeDynamicChannelBuffer(ChannelBufferFactory factory, int minimumCapacity) {
super(factory.getDefaultOrder(), minimumCapacity, factory);
}
@Override
protected void checkReadableBytes(int minReaderRemaining) {
// Do not check here - ReplayingDecoderBuffer will check.
}
}