Removed code duplication

This commit is contained in:
Trustin Lee 2010-01-15 05:39:30 +00:00
parent 8a8d1d1da6
commit ebc2459bc7
9 changed files with 9 additions and 80 deletions

View File

@ -535,6 +535,15 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
return toString(readerIndex, readableBytes(), charset); 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 @Deprecated
public String toString(int index, int length, String charsetName, public String toString(int index, int length, String charsetName,
ChannelBufferIndexFinder terminatorFinder) { ChannelBufferIndexFinder terminatorFinder) {

View File

@ -23,7 +23,6 @@ import java.nio.ByteOrder;
import java.nio.channels.ClosedChannelException; import java.nio.channels.ClosedChannelException;
import java.nio.channels.GatheringByteChannel; import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel; 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)} * 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) { public ChannelBuffer slice(int index, int length) {
if (index == 0 && length == capacity()) { if (index == 0 && length == capacity()) {
return duplicate(); return duplicate();

View File

@ -22,7 +22,6 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.channels.GatheringByteChannel; import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel; import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -586,37 +585,6 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
return buffers.toArray(new ByteBuffer[buffers.size()]); 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) { private int componentId(int index) {
int lastComponentId = lastAccessedComponentId; int lastComponentId = lastAccessedComponentId;
if (index >= indices[lastComponentId]) { if (index >= indices[lastComponentId]) {

View File

@ -22,7 +22,6 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.channels.GatheringByteChannel; import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel; 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) { public ByteBuffer toByteBuffer(int index, int length) {
return buffer.toByteBuffer(index, length); return buffer.toByteBuffer(index, length);
} }
public String toString(int index, int length, Charset charset) {
return buffer.toString(index, length, charset);
}
} }

View File

@ -22,7 +22,6 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.channels.GatheringByteChannel; import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel; 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) { public ByteBuffer toByteBuffer(int index, int length) {
return buffer.toByteBuffer(index, length); return buffer.toByteBuffer(index, length);
} }
public String toString(int index, int length, Charset charset) {
return buffer.toString(index, length, charset);
}
} }

View File

@ -22,7 +22,6 @@ import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException; import java.nio.channels.ClosedChannelException;
import java.nio.channels.GatheringByteChannel; import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel; import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
/** /**
* A skeletal implementation for Java heap buffers. * 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)); 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) { public void setByte(int index, byte value) {
array[index] = value; array[index] = value;
} }

View File

@ -23,7 +23,6 @@ import java.nio.ByteOrder;
import java.nio.ReadOnlyBufferException; import java.nio.ReadOnlyBufferException;
import java.nio.channels.GatheringByteChannel; import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel; import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
/** /**
* A derived buffer which forbids any write requests to its parent. It is * 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() { public int capacity() {
return buffer.capacity(); return buffer.capacity();
} }
public String toString(int index, int length, Charset charset) {
return buffer.toString(index, length, charset);
}
} }

View File

@ -22,7 +22,6 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.channels.GatheringByteChannel; import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel; 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); 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) { private void checkIndex(int index) {
if (index < 0 || index >= capacity()) { if (index < 0 || index >= capacity()) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();

View File

@ -22,7 +22,6 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.channels.GatheringByteChannel; import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel; 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); 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) { private void checkIndex(int index) {
if (index < 0 || index >= capacity()) { if (index < 0 || index >= capacity()) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();