From ebc2459bc7c720679a1171e3955555d6fcd290d2 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Fri, 15 Jan 2010 05:39:30 +0000 Subject: [PATCH] Removed code duplication --- .../netty/buffer/AbstractChannelBuffer.java | 9 ++++++ .../buffer/ByteBufferBackedChannelBuffer.java | 11 ------- .../netty/buffer/CompositeChannelBuffer.java | 32 ------------------- .../netty/buffer/DuplicatedChannelBuffer.java | 5 --- .../netty/buffer/DynamicChannelBuffer.java | 5 --- .../jboss/netty/buffer/HeapChannelBuffer.java | 10 ------ .../netty/buffer/ReadOnlyChannelBuffer.java | 5 --- .../netty/buffer/SlicedChannelBuffer.java | 6 ---- .../netty/buffer/TruncatedChannelBuffer.java | 6 ---- 9 files changed, 9 insertions(+), 80 deletions(-) diff --git a/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java index a67c48c415..19b8bb0aab 100644 --- a/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/AbstractChannelBuffer.java @@ -535,6 +535,15 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer { return toString(readerIndex, readableBytes(), charset); } + public String toString(int index, int length, Charset charset) { + if (length == 0) { + return ""; + } + + return ChannelBuffers.decodeString( + toByteBuffer(index, length), charset); + } + @Deprecated public String toString(int index, int length, String charsetName, ChannelBufferIndexFinder terminatorFinder) { diff --git a/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java index ea0937733c..207db1149c 100644 --- a/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java @@ -23,7 +23,6 @@ import java.nio.ByteOrder; import java.nio.channels.ClosedChannelException; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; -import java.nio.charset.Charset; /** * A NIO {@link ByteBuffer} based buffer. It is recommended to use {@link ChannelBuffers#directBuffer(int)} @@ -299,16 +298,6 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer { } } - public String toString(int index, int length, Charset charset) { - if (length == 0) { - return ""; - } - - return ChannelBuffers.decodeString( - ((ByteBuffer) buffer.duplicate().position(index).limit(index + length)), - charset); - } - public ChannelBuffer slice(int index, int length) { if (index == 0 && length == capacity()) { return duplicate(); diff --git a/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java index bda57bdc78..1bacf8b9a3 100644 --- a/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java @@ -22,7 +22,6 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; -import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -586,37 +585,6 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer { return buffers.toArray(new ByteBuffer[buffers.size()]); } - public String toString(int index, int length, Charset charset) { - if (length == 0) { - return ""; - } - - int componentId = componentId(index); - if (index + length <= indices[componentId + 1]) { - return components[componentId].toString( - index - indices[componentId], length, charset); - } - - byte[] data = new byte[length]; - int dataIndex = 0; - int i = componentId; - - int remaining = length; - while (remaining > 0) { - ChannelBuffer s = components[i]; - int adjustment = indices[i]; - int localLength = Math.min(remaining, s.capacity() - (index - adjustment)); - s.getBytes(index - adjustment, data, dataIndex, localLength); - index += localLength; - dataIndex += localLength; - remaining -= localLength; - i ++; - } - - return ChannelBuffers.decodeString( - ByteBuffer.wrap(data), charset); - } - private int componentId(int index) { int lastComponentId = lastAccessedComponentId; if (index >= indices[lastComponentId]) { diff --git a/src/main/java/org/jboss/netty/buffer/DuplicatedChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/DuplicatedChannelBuffer.java index 183370be8e..6d054bae18 100644 --- a/src/main/java/org/jboss/netty/buffer/DuplicatedChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/DuplicatedChannelBuffer.java @@ -22,7 +22,6 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; -import java.nio.charset.Charset; /** @@ -180,8 +179,4 @@ public class DuplicatedChannelBuffer extends AbstractChannelBuffer implements Wr public ByteBuffer toByteBuffer(int index, int length) { return buffer.toByteBuffer(index, length); } - - public String toString(int index, int length, Charset charset) { - return buffer.toString(index, length, 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..9d78c186bb 100644 --- a/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/DynamicChannelBuffer.java @@ -22,7 +22,6 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; -import java.nio.charset.Charset; /** @@ -290,8 +289,4 @@ public class DynamicChannelBuffer extends AbstractChannelBuffer { public ByteBuffer toByteBuffer(int index, int length) { return buffer.toByteBuffer(index, length); } - - public String toString(int index, int length, Charset charset) { - return buffer.toString(index, length, charset); - } } diff --git a/src/main/java/org/jboss/netty/buffer/HeapChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/HeapChannelBuffer.java index e6e12d1171..70f0b73889 100644 --- a/src/main/java/org/jboss/netty/buffer/HeapChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/HeapChannelBuffer.java @@ -22,7 +22,6 @@ import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; -import java.nio.charset.Charset; /** * A skeletal implementation for Java heap buffers. @@ -118,15 +117,6 @@ public abstract class HeapChannelBuffer extends AbstractChannelBuffer { return out.write(ByteBuffer.wrap(array, index, length)); } - public String toString(int index, int length, Charset charset) { - if (length == 0) { - return ""; - } - - return ChannelBuffers.decodeString( - ByteBuffer.wrap(array, index, length), charset); - } - public void setByte(int index, byte value) { array[index] = value; } diff --git a/src/main/java/org/jboss/netty/buffer/ReadOnlyChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/ReadOnlyChannelBuffer.java index c948a9efdb..be98760ae0 100644 --- a/src/main/java/org/jboss/netty/buffer/ReadOnlyChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/ReadOnlyChannelBuffer.java @@ -23,7 +23,6 @@ import java.nio.ByteOrder; import java.nio.ReadOnlyBufferException; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; -import java.nio.charset.Charset; /** * A derived buffer which forbids any write requests to its parent. It is @@ -194,8 +193,4 @@ public class ReadOnlyChannelBuffer extends AbstractChannelBuffer implements Wrap public int capacity() { return buffer.capacity(); } - - public String toString(int index, int length, Charset charset) { - return buffer.toString(index, length, 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..648279bf87 100644 --- a/src/main/java/org/jboss/netty/buffer/SlicedChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/SlicedChannelBuffer.java @@ -22,7 +22,6 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; -import java.nio.charset.Charset; /** @@ -214,11 +213,6 @@ public class SlicedChannelBuffer extends AbstractChannelBuffer implements Wrappe return buffer.toByteBuffer(index + adjustment, length); } - public String toString(int index, int length, Charset charset) { - checkIndex(index, length); - return buffer.toString(index + adjustment, length, charset); - } - private void checkIndex(int index) { if (index < 0 || index >= capacity()) { throw new IndexOutOfBoundsException(); diff --git a/src/main/java/org/jboss/netty/buffer/TruncatedChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/TruncatedChannelBuffer.java index 59e1aa5542..eb9a2b0ee1 100644 --- a/src/main/java/org/jboss/netty/buffer/TruncatedChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/TruncatedChannelBuffer.java @@ -22,7 +22,6 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; -import java.nio.charset.Charset; /** @@ -208,11 +207,6 @@ public class TruncatedChannelBuffer extends AbstractChannelBuffer implements Wra return buffer.toByteBuffer(index, length); } - public String toString(int index, int length, Charset charset) { - checkIndex(index, length); - return buffer.toString(index, length, charset); - } - private void checkIndex(int index) { if (index < 0 || index >= capacity()) { throw new IndexOutOfBoundsException();