[#1241] Fix NPE in AbstractTrafficShapingHandler and the logic

This commit is contained in:
Norman Maurer 2013-04-07 11:02:32 +02:00
parent 8632d9011e
commit b36e815026

View File

@ -18,8 +18,6 @@ package io.netty.handler.traffic;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelDuplexHandler; import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundByteHandler;
import io.netty.channel.ChannelOutboundByteHandler;
import io.netty.channel.ChannelPromise; import io.netty.channel.ChannelPromise;
import io.netty.util.Attribute; import io.netty.util.Attribute;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
@ -43,9 +41,7 @@ import java.util.concurrent.TimeUnit;
* or start the monitoring, to change the checkInterval directly, or to have access to its values.</li> * or start the monitoring, to change the checkInterval directly, or to have access to its values.</li>
* </ul> * </ul>
*/ */
public abstract class AbstractTrafficShapingHandler extends ChannelDuplexHandler public abstract class AbstractTrafficShapingHandler extends ChannelDuplexHandler {
implements ChannelInboundByteHandler, ChannelOutboundByteHandler {
/** /**
* Default delay between two checks: 1s * Default delay between two checks: 1s
*/ */
@ -213,39 +209,10 @@ public abstract class AbstractTrafficShapingHandler extends ChannelDuplexHandler
return (bytes * 1000 / limit - interval / 10) * 10; return (bytes * 1000 / limit - interval / 10) * 10;
} }
@Override
public ByteBuf newInboundBuffer(ChannelHandlerContext ctx) throws Exception {
return ctx.nextInboundByteBuffer();
}
@Override
public void discardInboundReadBytes(ChannelHandlerContext ctx) throws Exception {
// NOOP
}
@Override
public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
// do nothing
}
@Override
public ByteBuf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
return ctx.nextOutboundByteBuffer();
}
@Override
public void discardOutboundReadBytes(ChannelHandlerContext ctx) throws Exception {
// NOOP
}
@Override
public void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
// do nothing
}
@Override @Override
public void inboundBufferUpdated(final ChannelHandlerContext ctx) throws Exception { public void inboundBufferUpdated(final ChannelHandlerContext ctx) throws Exception {
ByteBuf buf = ctx.inboundByteBuffer(); ByteBuf buf = ctx.nextInboundByteBuffer();
long curtime = System.currentTimeMillis(); long curtime = System.currentTimeMillis();
long size = buf.readableBytes(); long size = buf.readableBytes();
@ -310,7 +277,7 @@ public abstract class AbstractTrafficShapingHandler extends ChannelDuplexHandler
@Override @Override
public void flush(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception { public void flush(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
long curtime = System.currentTimeMillis(); long curtime = System.currentTimeMillis();
long size = ctx.outboundByteBuffer().readableBytes(); long size = ctx.nextOutboundByteBuffer().readableBytes();
if (trafficCounter != null) { if (trafficCounter != null) {
trafficCounter.bytesWriteFlowControl(size); trafficCounter.bytesWriteFlowControl(size);