Add ChannelHandlerContext.hasNext(In|Out)bound(Byte|Message)Buffer

This commit is contained in:
Trustin Lee 2012-06-03 04:35:38 -07:00
parent bbed0602c1
commit d48973b0ff
4 changed files with 90 additions and 26 deletions

View File

@ -52,21 +52,9 @@ public final class ChannelBufferHolder<E> {
case 0:
return msgBuf != null;
case 1:
// XXX: Should we introduce hasNext(Inbound|Outbound)(Message|Byte)Buffer()
// just because of this?
try {
ctx.nextInboundMessageBuffer();
return true;
} catch (NoSuchBufferException e) {
return false;
}
return ctx.hasNextInboundMessageBuffer();
case 2:
try {
ctx.nextOutboundMessageBuffer();
return true;
} catch (NoSuchBufferException e) {
return false;
}
return ctx.hasNextOutboundMessageBuffer();
default:
throw new Error();
}
@ -77,19 +65,9 @@ public final class ChannelBufferHolder<E> {
case 0:
return byteBuf != null;
case 1:
try {
ctx.nextInboundByteBuffer();
return true;
} catch (NoSuchBufferException e) {
return false;
}
return ctx.hasNextInboundByteBuffer();
case 2:
try {
ctx.nextOutboundByteBuffer();
return true;
} catch (NoSuchBufferException e) {
return false;
}
return ctx.hasNextOutboundByteBuffer();
default:
throw new Error();
}

View File

@ -137,9 +137,13 @@ public interface ChannelHandlerContext
boolean canHandleInbound();
boolean canHandleOutbound();
boolean hasNextInboundByteBuffer();
boolean hasNextInboundMessageBuffer();
ChannelBuffer nextInboundByteBuffer();
Queue<Object> nextInboundMessageBuffer();
boolean hasNextOutboundByteBuffer();
boolean hasNextOutboundMessageBuffer();
ChannelBuffer nextOutboundByteBuffer();
Queue<Object> nextOutboundMessageBuffer();
}

View File

@ -202,6 +202,26 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
return out;
}
@Override
public boolean hasNextInboundByteBuffer() {
return DefaultChannelPipeline.hasNextInboundByteBuffer(next);
}
@Override
public boolean hasNextInboundMessageBuffer() {
return DefaultChannelPipeline.hasNextInboundMessageBuffer(next);
}
@Override
public boolean hasNextOutboundByteBuffer() {
return pipeline.hasNextOutboundByteBuffer(prev);
}
@Override
public boolean hasNextOutboundMessageBuffer() {
return pipeline.hasNextOutboundMessageBuffer(prev);
}
@Override
public ChannelBuffer nextInboundByteBuffer() {
return DefaultChannelPipeline.nextInboundByteBuffer(next);

View File

@ -651,6 +651,32 @@ public class DefaultChannelPipeline implements ChannelPipeline {
return nextOutboundByteBuffer(tail);
}
static boolean hasNextInboundByteBuffer(DefaultChannelHandlerContext ctx) {
for (;;) {
if (ctx == null) {
return false;
}
ChannelBufferHolder<Object> in = ctx.in;
if (in != null && !in.isBypass() && in.hasByteBuffer()) {
return true;
}
ctx = ctx.next;
}
}
static boolean hasNextInboundMessageBuffer(DefaultChannelHandlerContext ctx) {
for (;;) {
if (ctx == null) {
return false;
}
ChannelBufferHolder<Object> in = ctx.inbound();
if (in != null && !in.isBypass() && in.hasMessageBuffer()) {
return true;
}
ctx = ctx.next;
}
}
static ChannelBuffer nextInboundByteBuffer(DefaultChannelHandlerContext ctx) {
for (;;) {
if (ctx == null) {
@ -677,6 +703,42 @@ public class DefaultChannelPipeline implements ChannelPipeline {
}
}
boolean hasNextOutboundByteBuffer(DefaultChannelHandlerContext ctx) {
for (;;) {
if (ctx == null) {
if (directOutbound.hasByteBuffer()) {
return true;
} else {
return false;
}
}
ChannelBufferHolder<Object> out = ctx.outbound();
if (out != null && !out.isBypass() && out.hasByteBuffer()) {
return true;
}
ctx = ctx.prev;
}
}
boolean hasNextOutboundMessageBuffer(DefaultChannelHandlerContext ctx) {
for (;;) {
if (ctx == null) {
if (directOutbound.hasMessageBuffer()) {
return true;
} else {
return false;
}
}
ChannelBufferHolder<Object> out = ctx.outbound();
if (out != null && !out.isBypass() && out.hasMessageBuffer()) {
return true;
}
ctx = ctx.prev;
}
}
ChannelBuffer nextOutboundByteBuffer(DefaultChannelHandlerContext ctx) {
for (;;) {
if (ctx == null) {