From 55b0bc2c2c4a7e406db87d34921e6376f83bd433 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 14 Jan 2010 11:44:11 +0000 Subject: [PATCH] Related issue: NETTY-257 Add the getters and setters for a String and a char[] to ChannelBuffer * Added getString() and readString() * Renamed toString(int, int, Charset) to getString(...) * Removed some toString() methods which were added in the previous alpha releases - I will add an alternative method later --- .../netty/buffer/AbstractChannelBuffer.java | 107 ++++++++++++++---- .../buffer/ByteBufferBackedChannelBuffer.java | 26 +---- .../org/jboss/netty/buffer/ChannelBuffer.java | 74 +++++------- .../jboss/netty/buffer/ChannelBuffers.java | 46 +++++--- .../netty/buffer/CompositeChannelBuffer.java | 30 ++--- .../netty/buffer/DuplicatedChannelBuffer.java | 5 +- .../netty/buffer/DynamicChannelBuffer.java | 5 +- .../jboss/netty/buffer/HeapChannelBuffer.java | 40 ++----- .../netty/buffer/ReadOnlyChannelBuffer.java | 9 +- .../netty/buffer/SlicedChannelBuffer.java | 5 +- .../netty/buffer/TruncatedChannelBuffer.java | 5 +- .../codec/replay/ReplayingDecoderBuffer.java | 55 +++++++-- .../buffer/AbstractChannelBufferTest.java | 9 -- .../buffer/ReadOnlyChannelBufferTest.java | 4 +- 14 files changed, 232 insertions(+), 188 deletions(-) diff --git a/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java index a90cfca3e2..bc2d82e648 100644 --- a/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java @@ -19,11 +19,15 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; +import java.nio.CharBuffer; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; import java.util.NoSuchElementException; +import org.jboss.netty.util.CharsetUtil; + /** * A skeletal implementation of a buffer. @@ -171,6 +175,44 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer { dst.writerIndex(dst.writerIndex() + length); } + public String getString(int index, int length, Charset charset) { + if (length == 0) { + return ""; + } + + final CharsetDecoder decoder = CharsetUtil.getDecoder(charset); + final CharBuffer dst = CharBuffer.allocate( + (int) ((double) length * decoder.maxCharsPerByte())); + + int decodedChars = getString(index, length, dst, charset); + if (decodedChars == 0) { + return ""; + } + return dst.flip().toString(); + } + + public int getString(int index, int length, char[] dst, Charset charset) { + return getString(index, length, CharBuffer.wrap(dst), charset); + } + + public int getString( + int index, int length, + char[] dst, int dstOffset, int dstLength, Charset charset) { + return getString(index, length, CharBuffer.wrap( + dst, dstOffset, dstLength), charset); + } + + public int getString(int index, int length, Appendable out, Charset charset) + throws IOException { + if (out instanceof CharBuffer) { + return getString(index, length, out, charset); + } + + String str = getString(index, length, charset); + out.append(str); + return str.length(); + } + public void setChar(int index, char value) { setShort(index, (short) value); } @@ -380,6 +422,35 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer { readerIndex += length; } + public int readString(int length, char[] dst, Charset charset) { + checkReadableBytes(length); + int result = getString(readerIndex, length, dst, charset); + readerIndex += length; + return result; + } + + public int readString(int length, char[] dst, int dstOffset, int dstLength, Charset charset) { + checkReadableBytes(length); + int result = getString(readerIndex, length, dst, dstOffset, dstLength, charset); + readerIndex += length; + return result; + } + + public int readString(int length, Appendable out, Charset charset) + throws IOException { + checkReadableBytes(length); + int result = getString(readerIndex, length, out, charset); + readerIndex += length; + return result; + } + + public String readString(int length, Charset charset) { + checkReadableBytes(length); + String result = getString(readerIndex, length, charset); + readerIndex += length; + return result; + } + public void skipBytes(int length) { int newReaderIndex = readerIndex + length; if (newReaderIndex > writerIndex) { @@ -532,40 +603,30 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer { } public String toString(Charset charset) { - return toString(readerIndex, readableBytes(), charset); - } - - public String toString(Charset charset, ChannelBufferIndexFinder terminatorFinder) { - return toString(readerIndex, readableBytes(), charset, terminatorFinder); - } - - public String toString( - int index, int length, Charset charset, - ChannelBufferIndexFinder terminatorFinder) { - if (terminatorFinder == null) { - return toString(index, length, charset); - } - - int terminatorIndex = indexOf(index, index + length, terminatorFinder); - if (terminatorIndex < 0) { - return toString(index, length, charset); - } - - return toString(index, terminatorIndex - index, charset); + return getString(readerIndex, readableBytes(), charset); } public String toString(int index, int length, String charsetName, ChannelBufferIndexFinder terminatorFinder) { - return toString(index, length, Charset.forName(charsetName), 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 String toString(int index, int length, String charsetName) { - return toString(index, length, Charset.forName(charsetName)); + return getString(index, length, Charset.forName(charsetName)); } public String toString(String charsetName, ChannelBufferIndexFinder terminatorFinder) { - return toString(Charset.forName(charsetName), terminatorFinder); + return toString(readerIndex, readableBytes(), charsetName, terminatorFinder); } public String toString(String charsetName) { diff --git a/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java index 677b65caae..710160dff0 100644 --- a/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java @@ -24,10 +24,8 @@ import java.nio.CharBuffer; import java.nio.channels.ClosedChannelException; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; -import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; -import java.nio.charset.CoderResult; import org.jboss.netty.util.CharsetUtil; @@ -305,32 +303,18 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer { } } - public String toString(int index, int length, Charset charset) { + public int getString(int index, int length, CharBuffer dst, Charset charset) { if (length == 0) { - return ""; + return 0; } final CharsetDecoder decoder = CharsetUtil.getDecoder(charset); + final int start = dst.position(); final ByteBuffer src = ((ByteBuffer) buffer.duplicate().position( index).limit(index + length)).order(order()); - final CharBuffer dst = CharBuffer.allocate( - (int) ((double) length * decoder.maxCharsPerByte())); - try { - CoderResult cr = decoder.decode(src, dst, true); - if (!cr.isUnderflow()) { - cr.throwException(); - } - cr = decoder.flush(dst); - if (!cr.isUnderflow()) { - cr.throwException(); - } - } catch (CharacterCodingException x) { - throw new IllegalStateException(x); - } - - dst.flip(); - return dst.toString(); + ChannelBuffers.decodeString(src, dst, decoder); + return dst.position() - start; } public ChannelBuffer slice(int index, int length) { diff --git a/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java index 799eb4bea5..8baa5d99e3 100644 --- a/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/ChannelBuffer.java @@ -20,6 +20,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.nio.CharBuffer; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; import java.nio.charset.Charset; @@ -707,6 +708,22 @@ public interface ChannelBuffer extends Comparable { */ int getBytes(int index, GatheringByteChannel out, int length) throws IOException; + // FIXME Document me + int getString(int index, int length, char[] dst, Charset charset); + // FIXME Document me + int getString(int index, int length, char[] dst, int dstOffset, int dstLength, Charset charset); + // FIXME Document me + int getString(int index, int length, Appendable out, Charset charset) throws IOException; + // FIXME Document me + int getString(int index, int length, CharBuffer dst, Charset charset); + + /** + * Decodes this buffer's sub-region into a string with the specified + * character set. This method does not modify {@code readerIndex} or + * {@code writerIndex} of this buffer. + */ + String getString(int index, int length, Charset charset); + /** * Sets the specified byte at the specified absolute {@code index} in this * buffer. @@ -1236,6 +1253,15 @@ public interface ChannelBuffer extends Comparable { */ int readBytes(GatheringByteChannel out, int length) throws IOException; + // FIXME Document me + int readString(int length, char[] dst, Charset charset); + // FIXME Document me + int readString(int length, char[] dst, int dstOffset, int dstLength, Charset charset); + // FIXME Document me + int readString(int length, Appendable out, Charset charset) throws IOException ; + // FIXME Document me + String readString(int length, Charset charset); + /** * Increases the current {@code readerIndex} by the specified * {@code length} in this buffer. @@ -1629,62 +1655,20 @@ public interface ChannelBuffer extends Comparable { String toString(String charsetName); /** - * Decodes this buffer's readable bytes into a string until the specified - * {@code terminatorFinder} returns {@code true} with the specified - * character set name. This method is identical to - * {@code buf.toString(buf.readerIndex(), buf.readableBytes(), charsetName, terminatorFinder)}. - * This method does not modify {@code readerIndex} or {@code writerIndex} of - * this buffer. - * - * @throws UnsupportedCharsetException - * if the specified character set name is not supported by the - * current VM - */ - String toString( - Charset charset, ChannelBufferIndexFinder terminatorFinder); - - /** - * @deprecated Use {@link #toString(Charset, ChannelBufferIndexFinder)} instead. + * @deprecated Use {@link #getString(int, int, Charset)} instead. */ @Deprecated String toString( String charsetName, ChannelBufferIndexFinder terminatorFinder); /** - * Decodes this buffer's sub-region into a string with the specified - * character set name. - * This method does not modify {@code readerIndex} or {@code writerIndex} of - * this buffer. - * - * @throws UnsupportedCharsetException - * if the specified character set name is not supported by the - * current VM - */ - String toString(int index, int length, Charset charset); - - /** - * @deprecated Use {@link #toString(int, int, Charset)} instead. + * @deprecated Use {@link #getString(int, int, Charset)} instead. */ @Deprecated String toString(int index, int length, String charsetName); /** - * Decodes this buffer's readable bytes into a string until the specified - * {@code terminatorFinder} returns {@code true} with the specified - * character set name. - * This method does not modify {@code readerIndex} or {@code writerIndex} of - * this buffer. - * - * @throws UnsupportedCharsetException - * if the specified character set name is not supported by the - * current VM - */ - String toString( - int index, int length, Charset charset, - ChannelBufferIndexFinder terminatorFinder); - - /** - * @deprecated Use {@link #toString(int, int, Charset, ChannelBufferIndexFinder)} instead. + * @deprecated Use {@link #getString(int, int, Charset)} instead. */ @Deprecated String toString( diff --git a/src/main/java/org/jboss/netty/buffer/ChannelBuffers.java b/src/main/java/org/jboss/netty/buffer/ChannelBuffers.java index f0141937f9..9218ebdfa4 100644 --- a/src/main/java/org/jboss/netty/buffer/ChannelBuffers.java +++ b/src/main/java/org/jboss/netty/buffer/ChannelBuffers.java @@ -20,6 +20,7 @@ import java.nio.ByteOrder; import java.nio.CharBuffer; import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetEncoder; import java.nio.charset.CoderResult; import java.util.ArrayList; @@ -795,20 +796,7 @@ public class ChannelBuffers { CharBuffer src = buffer; ByteBuffer dst = ByteBuffer.allocate( (int) ((double) buffer.remaining() * encoder.maxBytesPerChar())); - - try { - CoderResult cr = encoder.encode(src, dst, true); - if (!cr.isUnderflow()) { - cr.throwException(); - } - cr = encoder.flush(dst); - if (!cr.isUnderflow()) { - cr.throwException(); - } - } catch (CharacterCodingException x) { - throw new IllegalStateException(x); - } - + ChannelBuffers.encodeString(src, dst, encoder); ChannelBuffer result = wrappedBuffer(endianness, dst.array()); result.writerIndex(dst.position()); return result; @@ -1126,6 +1114,36 @@ public class ChannelBuffers { return -1; } + static void encodeString(CharBuffer src, ByteBuffer dst, CharsetEncoder encoder) { + try { + CoderResult cr = encoder.encode(src, dst, true); + if (!cr.isUnderflow()) { + cr.throwException(); + } + cr = encoder.flush(dst); + if (!cr.isUnderflow()) { + cr.throwException(); + } + } catch (CharacterCodingException x) { + throw new IllegalStateException(x); + } + } + + static void decodeString(ByteBuffer src, CharBuffer dst, CharsetDecoder decoder) { + try { + CoderResult cr = decoder.decode(src, dst, true); + if (!cr.isUnderflow()) { + cr.throwException(); + } + cr = decoder.flush(dst); + if (!cr.isUnderflow()) { + cr.throwException(); + } + } catch (CharacterCodingException x) { + throw new IllegalStateException(x); + } + } + private ChannelBuffers() { // Unused } diff --git a/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java index 758b88326c..5ab671e1e9 100644 --- a/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java @@ -23,10 +23,8 @@ import java.nio.ByteOrder; import java.nio.CharBuffer; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; -import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; -import java.nio.charset.CoderResult; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -592,15 +590,15 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer { return buffers.toArray(new ByteBuffer[buffers.size()]); } - public String toString(int index, int length, Charset charset) { + public int getString(int index, int length, CharBuffer dst, Charset charset) { int componentId = componentId(index); if (index + length <= indices[componentId + 1]) { - return components[componentId].toString( - index - indices[componentId], length, charset); + return components[componentId].getString( + index - indices[componentId], length, dst, charset); } if (length == 0) { - return ""; + return 0; } byte[] data = new byte[length]; @@ -620,24 +618,10 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer { } final CharsetDecoder decoder = CharsetUtil.getDecoder(charset); + final int start = dst.position(); final ByteBuffer src = ByteBuffer.wrap(data); - final CharBuffer dst = CharBuffer.allocate( - (int) ((double) length * decoder.maxCharsPerByte())); - try { - CoderResult cr = decoder.decode(src, dst, true); - if (!cr.isUnderflow()) { - cr.throwException(); - } - cr = decoder.flush(dst); - if (!cr.isUnderflow()) { - cr.throwException(); - } - } catch (CharacterCodingException x) { - throw new IllegalStateException(x); - } - - dst.flip(); - return dst.toString(); + ChannelBuffers.decodeString(src, dst, decoder); + return dst.position() - start; } private int componentId(int index) { diff --git a/src/main/java/org/jboss/netty/buffer/DuplicatedChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/DuplicatedChannelBuffer.java index 183370be8e..074806eade 100644 --- a/src/main/java/org/jboss/netty/buffer/DuplicatedChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/DuplicatedChannelBuffer.java @@ -20,6 +20,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.nio.CharBuffer; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; import java.nio.charset.Charset; @@ -181,7 +182,7 @@ public class DuplicatedChannelBuffer extends AbstractChannelBuffer implements Wr return buffer.toByteBuffer(index, length); } - public String toString(int index, int length, Charset charset) { - return buffer.toString(index, length, charset); + public int getString(int index, int length, CharBuffer dst, Charset charset) { + return buffer.getString(index, length, dst, charset); } } diff --git a/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java index 6a1dfee3c5..9e3dcaab99 100644 --- a/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java @@ -20,6 +20,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.nio.CharBuffer; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; import java.nio.charset.Charset; @@ -291,7 +292,7 @@ public class DynamicChannelBuffer extends AbstractChannelBuffer { return buffer.toByteBuffer(index, length); } - public String toString(int index, int length, Charset charset) { - return buffer.toString(index, length, charset); + public int getString(int index, int length, CharBuffer dst, Charset charset) { + return buffer.getString(index, length, dst, charset); } } diff --git a/src/main/java/org/jboss/netty/buffer/HeapChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/HeapChannelBuffer.java index e53105f265..9935f09048 100644 --- a/src/main/java/org/jboss/netty/buffer/HeapChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/HeapChannelBuffer.java @@ -23,10 +23,8 @@ import java.nio.CharBuffer; import java.nio.channels.ClosedChannelException; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; -import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; -import java.nio.charset.CoderResult; import org.jboss.netty.util.CharsetUtil; @@ -124,6 +122,18 @@ public abstract class HeapChannelBuffer extends AbstractChannelBuffer { return out.write(ByteBuffer.wrap(array, index, length)); } + public int getString(int index, int length, CharBuffer dst, Charset charset) { + if (length == 0) { + return 0; + } + + final CharsetDecoder decoder = CharsetUtil.getDecoder(charset); + final int start = dst.position(); + final ByteBuffer src = ByteBuffer.wrap(array, index, length); + ChannelBuffers.decodeString(src, dst, decoder); + return dst.position() - start; + } + public void setByte(int index, byte value) { array[index] = value; } @@ -210,30 +220,4 @@ public abstract class HeapChannelBuffer extends AbstractChannelBuffer { public ByteBuffer toByteBuffer(int index, int length) { return ByteBuffer.wrap(array, index, length).order(order()); } - - public String toString(int index, int length, Charset charset) { - if (length == 0) { - return ""; - } - - final CharsetDecoder decoder = CharsetUtil.getDecoder(charset); - final ByteBuffer src = ByteBuffer.wrap(array, index, length); - final CharBuffer dst = CharBuffer.allocate( - (int) ((double) length * decoder.maxCharsPerByte())); - try { - CoderResult cr = decoder.decode(src, dst, true); - if (!cr.isUnderflow()) { - cr.throwException(); - } - cr = decoder.flush(dst); - if (!cr.isUnderflow()) { - cr.throwException(); - } - } catch (CharacterCodingException x) { - throw new IllegalStateException(x); - } - - dst.flip(); - return dst.toString(); - } } diff --git a/src/main/java/org/jboss/netty/buffer/ReadOnlyChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/ReadOnlyChannelBuffer.java index a6dc889b6f..34d6ade4de 100644 --- a/src/main/java/org/jboss/netty/buffer/ReadOnlyChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/ReadOnlyChannelBuffer.java @@ -20,6 +20,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.nio.CharBuffer; import java.nio.ReadOnlyBufferException; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; @@ -191,11 +192,11 @@ public class ReadOnlyChannelBuffer extends AbstractChannelBuffer implements Wrap return bufs; } - public String toString(int index, int length, Charset charset) { - return buffer.toString(index, length, charset); - } - public int capacity() { return buffer.capacity(); } + + public int getString(int index, int length, CharBuffer dst, Charset charset) { + return buffer.getString(index, length, dst, charset); + } } diff --git a/src/main/java/org/jboss/netty/buffer/SlicedChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/SlicedChannelBuffer.java index ea0a7ca3dd..df84d45f67 100644 --- a/src/main/java/org/jboss/netty/buffer/SlicedChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/SlicedChannelBuffer.java @@ -20,6 +20,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.nio.CharBuffer; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; import java.nio.charset.Charset; @@ -214,9 +215,9 @@ public class SlicedChannelBuffer extends AbstractChannelBuffer implements Wrappe return buffer.toByteBuffer(index + adjustment, length); } - public String toString(int index, int length, Charset charset) { + public int getString(int index, int length, CharBuffer dst, Charset charset) { checkIndex(index, length); - return buffer.toString(index + adjustment, length, charset); + return buffer.getString(index + adjustment, length, dst, charset); } private void checkIndex(int index) { diff --git a/src/main/java/org/jboss/netty/buffer/TruncatedChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/TruncatedChannelBuffer.java index 59e1aa5542..9cab9025d4 100644 --- a/src/main/java/org/jboss/netty/buffer/TruncatedChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/TruncatedChannelBuffer.java @@ -20,6 +20,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.nio.CharBuffer; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; import java.nio.charset.Charset; @@ -208,9 +209,9 @@ public class TruncatedChannelBuffer extends AbstractChannelBuffer implements Wra return buffer.toByteBuffer(index, length); } - public String toString(int index, int length, Charset charset) { + public int getString(int index, int length, CharBuffer dst, Charset charset) { checkIndex(index, length); - return buffer.toString(index, length, charset); + return buffer.getString(index, length, dst, charset); } private void checkIndex(int index) { 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 21c328c371..f1724a93bf 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 @@ -20,6 +20,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.nio.CharBuffer; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; import java.nio.charset.Charset; @@ -324,6 +325,28 @@ class ReplayingDecoderBuffer implements ChannelBuffer { throw new UnreplayableOperationException(); } + public int readString(int length, Appendable out, Charset charset) + throws IOException { + checkReadableBytes(length); + return buffer.readString(length, out, charset); + } + + public int readString(int length, char[] dst, Charset charset) { + checkReadableBytes(length); + return buffer.readString(length, dst, charset); + } + + public int readString(int length, char[] dst, int dstOffset, int dstLength, Charset charset) { + checkReadableBytes(length); + return buffer.readString(length, dst, dstOffset, dstLength, charset); + } + + public String readString(int length, Charset charset) { + checkReadableBytes(length); + // TODO Auto-generated method stub + return null; + } + public int readerIndex() { return buffer.readerIndex(); } @@ -505,27 +528,37 @@ class ReplayingDecoderBuffer implements ChannelBuffer { return buffer.toByteBuffers(index, length); } - public String toString(int index, int length, Charset charset) { + public String getString(int index, int length, Charset charset) { checkIndex(index, length); - return buffer.toString(index, length, charset); + return buffer.getString(index, length, charset); } - public String toString( - int index, int length, Charset charset, - ChannelBufferIndexFinder terminatorFinder) { + public int getString(int index, int length, Appendable out, Charset charset) + throws IOException { checkIndex(index, length); - return buffer.toString(index, length, charset, terminatorFinder); + return buffer.getString(index, length, out, charset); + } + + public int getString(int index, int length, char[] dst, Charset charset) { + checkIndex(index, length); + return buffer.getString(index, length, dst, charset); + } + + public int getString(int index, int length, char[] dst, int dstOffset, int dstLength, + Charset charset) { + checkIndex(index, length); + return buffer.getString(index, length, dst, dstOffset, dstLength, charset); + } + + public int getString(int index, int length, CharBuffer dst, Charset charset) { + checkIndex(index, length); + return buffer.getString(index, length, dst, charset); } public String toString(Charset charsetName) { throw new UnreplayableOperationException(); } - public String toString( - Charset charset, ChannelBufferIndexFinder terminatorFinder) { - throw new UnreplayableOperationException(); - } - @Deprecated public String toString(int index, int length, String charsetName) { checkIndex(index, length); diff --git a/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java b/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java index 5cc895feee..2d5de9fdb6 100644 --- a/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java +++ b/src/test/java/org/jboss/netty/buffer/AbstractChannelBufferTest.java @@ -1537,15 +1537,6 @@ public abstract class AbstractChannelBufferTest { buffer.clear(); buffer.writeBytes(copiedBuffer("Hello, World!", CharsetUtil.ISO_8859_1)); assertEquals("Hello, World!", buffer.toString(CharsetUtil.ISO_8859_1)); - - // Same with the previous one - assertEquals("Hello, World!", buffer.toString(CharsetUtil.ISO_8859_1, null)); - - // NUL not found. - assertEquals("Hello, World!", buffer.toString(CharsetUtil.ISO_8859_1, ChannelBufferIndexFinder.NUL)); - - // Linear space found. - assertEquals("Hello,", buffer.toString(CharsetUtil.ISO_8859_1, ChannelBufferIndexFinder.LINEAR_WHITESPACE)); } @Test diff --git a/src/test/java/org/jboss/netty/buffer/ReadOnlyChannelBufferTest.java b/src/test/java/org/jboss/netty/buffer/ReadOnlyChannelBufferTest.java index 4df231fd90..257d100610 100644 --- a/src/test/java/org/jboss/netty/buffer/ReadOnlyChannelBufferTest.java +++ b/src/test/java/org/jboss/netty/buffer/ReadOnlyChannelBufferTest.java @@ -98,7 +98,7 @@ public class ReadOnlyChannelBufferTest { expect(buf.toByteBuffer(23, 24)).andReturn(bb); expect(buf.toByteBuffers(25, 26)).andReturn(bbs); - expect(buf.toString(27, 28, CharsetUtil.UTF_8)).andReturn("29"); + expect(buf.getString(27, 28, CharsetUtil.UTF_8)).andReturn("29"); expect(buf.capacity()).andReturn(30); replay(buf); @@ -126,7 +126,7 @@ public class ReadOnlyChannelBufferTest { assertEquals(102, roBBs[1].capacity()); assertTrue(roBBs[1].isReadOnly()); - assertEquals("29", roBuf.toString(27, 28, CharsetUtil.UTF_8)); + assertEquals("29", roBuf.getString(27, 28, CharsetUtil.UTF_8)); assertEquals(30, roBuf.capacity()); verify(buf);