From 58599fe343d637c3722ff6a5bf8c8bd11d563d59 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Sun, 10 Aug 2008 01:20:30 +0000 Subject: [PATCH] Added more toString() variants to ChannelBuffer - makes ChannelBuffer more convenient to decode a NUL-terminated string. --- .../netty/buffer/AbstractChannelBuffer.java | 19 +++++++++++++++++++ .../org/jboss/netty/buffer/ChannelBuffer.java | 2 ++ .../codec/replay/ReplayingDecoderBuffer.java | 13 +++++++++++++ 3 files changed, 34 insertions(+) diff --git a/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java index a3f58b4d07..b377616512 100644 --- a/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java @@ -459,6 +459,25 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer { return toString(readerIndex, readableBytes(), charsetName); } + public String toString(String charsetName, ChannelBufferIndexFinder terminatorFinder) { + return toString(readerIndex, readableBytes(), charsetName, terminatorFinder); + } + + public String toString( + int index, int length, String charsetName, + ChannelBufferIndexFinder terminatorFinder) { + if (terminatorFinder == null) { + return toString(index, length, charsetName); + } + + int terminatorIndex = indexOf(index, index + length, terminatorFinder); + if (terminatorIndex < 0) { + return toString(index, length, charsetName); + } + + return toString(index, terminatorIndex - index, charsetName); + } + public int indexOf(int fromIndex, int toIndex, byte value) { return ChannelBuffers.indexOf(this, fromIndex, toIndex, value); } diff --git a/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java index ddcc1be43a..8e288f1797 100644 --- a/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java @@ -694,7 +694,9 @@ public interface ChannelBuffer extends Comparable { ByteBuffer[] toByteBuffers(int index, int length); String toString(String charsetName); + String toString(String charsetName, ChannelBufferIndexFinder terminatorFinder); String toString(int index, int length, String charsetName); + String toString(int index, int length, String charsetName, ChannelBufferIndexFinder terminatorFinder); /** * Returns a hash code which was calculated from the content of this diff --git a/src/main/java/org/jboss/netty/handler/codec/replay/ReplayingDecoderBuffer.java b/src/main/java/org/jboss/netty/handler/codec/replay/ReplayingDecoderBuffer.java index f776bacf09..82de28abed 100644 --- a/src/main/java/org/jboss/netty/handler/codec/replay/ReplayingDecoderBuffer.java +++ b/src/main/java/org/jboss/netty/handler/codec/replay/ReplayingDecoderBuffer.java @@ -448,11 +448,24 @@ class ReplayingDecoderBuffer implements ChannelBuffer { return buffer.toString(index, length, charsetName); } + public String toString( + int index, int length, String charsetName, + ChannelBufferIndexFinder terminatorFinder) { + checkIndex(index, length); + return buffer.toString(index, length, charsetName, terminatorFinder); + } + public String toString(String charsetName) { reject(); return null; } + public String toString( + String charsetName, ChannelBufferIndexFinder terminatorFinder) { + reject(); + return null; + } + @Override public String toString() { return buffer.toString();