Related issue: NETTY-257 Add the getters and setters for a String and a char[] to ChannelBuffer

* Reverted the previous addition of get/readString(...)
This commit is contained in:
Trustin Lee 2010-01-15 05:28:17 +00:00
parent 7d40d47270
commit 57bdb3a4a5
13 changed files with 61 additions and 215 deletions

View File

@ -19,15 +19,11 @@ 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.
@ -175,44 +171,6 @@ 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);
}
@ -422,35 +380,6 @@ 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) {
@ -603,9 +532,10 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
}
public String toString(Charset charset) {
return getString(readerIndex, readableBytes(), charset);
return toString(readerIndex, readableBytes(), charset);
}
@Deprecated
public String toString(int index, int length, String charsetName,
ChannelBufferIndexFinder terminatorFinder) {
if (terminatorFinder == null) {
@ -620,15 +550,18 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
return toString(index, terminatorIndex - index, charsetName);
}
@Deprecated
public String toString(int index, int length, String charsetName) {
return getString(index, length, Charset.forName(charsetName));
return toString(index, length, Charset.forName(charsetName));
}
@Deprecated
public String toString(String charsetName,
ChannelBufferIndexFinder terminatorFinder) {
return toString(readerIndex, readableBytes(), charsetName, terminatorFinder);
}
@Deprecated
public String toString(String charsetName) {
return toString(Charset.forName(charsetName));
}

View File

@ -20,14 +20,10 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import org.jboss.netty.util.CharsetUtil;
/**
* A NIO {@link ByteBuffer} based buffer. It is recommended to use {@link ChannelBuffers#directBuffer(int)}
@ -303,18 +299,14 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer {
}
}
public int getString(int index, int length, CharBuffer dst, Charset charset) {
public String toString(int index, int length, Charset charset) {
if (length == 0) {
return 0;
return "";
}
final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
final int start = dst.position();
final ByteBuffer src =
((ByteBuffer) buffer.duplicate().position(
index).limit(index + length)).order(order());
ChannelBuffers.decodeString(src, dst, decoder);
return dst.position() - start;
return ChannelBuffers.decodeString(
((ByteBuffer) buffer.duplicate().position(index).limit(index + length)),
charset);
}
public ChannelBuffer slice(int index, int length) {

View File

@ -20,7 +20,6 @@ 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;
@ -708,22 +707,6 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
*/
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.
@ -1253,15 +1236,6 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
*/
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.
@ -1648,6 +1622,13 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
*/
String toString(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 toString(int index, int length, Charset charset);
/**
* @deprecated Use {@link #toString(Charset)} instead.
*/
@ -1655,20 +1636,20 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
String toString(String charsetName);
/**
* @deprecated Use {@link #getString(int, int, Charset)} instead.
* @deprecated Use {@link #toString(int, int, Charset)} instead.
*/
@Deprecated
String toString(
String charsetName, ChannelBufferIndexFinder terminatorFinder);
/**
* @deprecated Use {@link #getString(int, int, Charset)} instead.
* @deprecated Use {@link #toString(int, int, Charset)} instead.
*/
@Deprecated
String toString(int index, int length, String charsetName);
/**
* @deprecated Use {@link #getString(int, int, Charset)} instead.
* @deprecated Use {@link #toString(int, int, Charset)} instead.
*/
@Deprecated
String toString(

View File

@ -792,12 +792,9 @@ public class ChannelBuffers {
}
private static ChannelBuffer copiedBuffer(ByteOrder endianness, CharBuffer buffer, Charset charset) {
CharsetEncoder encoder = CharsetUtil.getEncoder(charset);
CharBuffer src = buffer;
ByteBuffer dst = ByteBuffer.allocate(
(int) ((double) buffer.remaining() * encoder.maxBytesPerChar()));
ChannelBuffers.encodeString(src, dst, encoder);
ChannelBuffer result = wrappedBuffer(endianness, dst.array());
ByteBuffer dst = ChannelBuffers.encodeString(src, charset);
ChannelBuffer result = wrappedBuffer(endianness, dst.array(), 0, dst.remaining());
result.writerIndex(dst.position());
return result;
}
@ -1114,7 +1111,10 @@ public class ChannelBuffers {
return -1;
}
static void encodeString(CharBuffer src, ByteBuffer dst, CharsetEncoder encoder) {
static ByteBuffer encodeString(CharBuffer src, Charset charset) {
final CharsetEncoder encoder = CharsetUtil.getEncoder(charset);
final ByteBuffer dst = ByteBuffer.allocate(
(int) ((double) src.remaining() * encoder.maxBytesPerChar()));
try {
CoderResult cr = encoder.encode(src, dst, true);
if (!cr.isUnderflow()) {
@ -1127,9 +1127,14 @@ public class ChannelBuffers {
} catch (CharacterCodingException x) {
throw new IllegalStateException(x);
}
dst.flip();
return dst;
}
static void decodeString(ByteBuffer src, CharBuffer dst, CharsetDecoder decoder) {
static String decodeString(ByteBuffer src, Charset charset) {
final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
final CharBuffer dst = CharBuffer.allocate(
(int) ((double) src.remaining() * decoder.maxCharsPerByte()));
try {
CoderResult cr = decoder.decode(src, dst, true);
if (!cr.isUnderflow()) {
@ -1142,6 +1147,7 @@ public class ChannelBuffers {
} catch (CharacterCodingException x) {
throw new IllegalStateException(x);
}
return dst.flip().toString();
}
private ChannelBuffers() {

View File

@ -20,17 +20,13 @@ 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;
import java.nio.charset.CharsetDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.jboss.netty.util.CharsetUtil;
/**
* A virtual buffer which shows multiple buffers as a single merged buffer. It
@ -590,15 +586,15 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
return buffers.toArray(new ByteBuffer[buffers.size()]);
}
public int getString(int index, int length, CharBuffer dst, Charset charset) {
int componentId = componentId(index);
if (index + length <= indices[componentId + 1]) {
return components[componentId].getString(
index - indices[componentId], length, dst, charset);
public String toString(int index, int length, Charset charset) {
if (length == 0) {
return "";
}
if (length == 0) {
return 0;
int componentId = componentId(index);
if (index + length <= indices[componentId + 1]) {
return components[componentId].toString(
index - indices[componentId], length, charset);
}
byte[] data = new byte[length];
@ -617,11 +613,8 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
i ++;
}
final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
final int start = dst.position();
final ByteBuffer src = ByteBuffer.wrap(data);
ChannelBuffers.decodeString(src, dst, decoder);
return dst.position() - start;
return ChannelBuffers.decodeString(
ByteBuffer.wrap(data), charset);
}
private int componentId(int index) {

View File

@ -20,7 +20,6 @@ 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;
@ -182,7 +181,7 @@ public class DuplicatedChannelBuffer extends AbstractChannelBuffer implements Wr
return buffer.toByteBuffer(index, length);
}
public int getString(int index, int length, CharBuffer dst, Charset charset) {
return buffer.getString(index, length, dst, charset);
public String toString(int index, int length, Charset charset) {
return buffer.toString(index, length, charset);
}
}

View File

@ -20,7 +20,6 @@ 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;
@ -292,7 +291,7 @@ public class DynamicChannelBuffer extends AbstractChannelBuffer {
return buffer.toByteBuffer(index, length);
}
public int getString(int index, int length, CharBuffer dst, Charset charset) {
return buffer.getString(index, length, dst, charset);
public String toString(int index, int length, Charset charset) {
return buffer.toString(index, length, charset);
}
}

View File

@ -19,14 +19,10 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import org.jboss.netty.util.CharsetUtil;
/**
* A skeletal implementation for Java heap buffers.
@ -122,16 +118,13 @@ 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) {
public String toString(int index, int length, Charset charset) {
if (length == 0) {
return 0;
return "";
}
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;
return ChannelBuffers.decodeString(
ByteBuffer.wrap(array, index, length), charset);
}
public void setByte(int index, byte value) {

View File

@ -20,7 +20,6 @@ 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;
@ -196,7 +195,7 @@ public class ReadOnlyChannelBuffer extends AbstractChannelBuffer implements Wrap
return buffer.capacity();
}
public int getString(int index, int length, CharBuffer dst, Charset charset) {
return buffer.getString(index, length, dst, charset);
public String toString(int index, int length, Charset charset) {
return buffer.toString(index, length, charset);
}
}

View File

@ -20,7 +20,6 @@ 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;
@ -215,9 +214,9 @@ public class SlicedChannelBuffer extends AbstractChannelBuffer implements Wrappe
return buffer.toByteBuffer(index + adjustment, length);
}
public int getString(int index, int length, CharBuffer dst, Charset charset) {
public String toString(int index, int length, Charset charset) {
checkIndex(index, length);
return buffer.getString(index + adjustment, length, dst, charset);
return buffer.toString(index + adjustment, length, charset);
}
private void checkIndex(int index) {

View File

@ -20,7 +20,6 @@ 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;
@ -209,9 +208,9 @@ public class TruncatedChannelBuffer extends AbstractChannelBuffer implements Wra
return buffer.toByteBuffer(index, length);
}
public int getString(int index, int length, CharBuffer dst, Charset charset) {
public String toString(int index, int length, Charset charset) {
checkIndex(index, length);
return buffer.getString(index, length, dst, charset);
return buffer.toString(index, length, charset);
}
private void checkIndex(int index) {

View File

@ -20,7 +20,6 @@ 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;
@ -325,28 +324,6 @@ 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();
}
@ -528,31 +505,9 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
return buffer.toByteBuffers(index, length);
}
public String getString(int index, int length, Charset charset) {
public String toString(int index, int length, Charset charset) {
checkIndex(index, length);
return buffer.getString(index, length, charset);
}
public int getString(int index, int length, Appendable out, Charset charset)
throws IOException {
checkIndex(index, length);
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);
return buffer.toString(index, length, charset);
}
public String toString(Charset charsetName) {

View File

@ -23,7 +23,6 @@ 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;
@ -96,11 +95,10 @@ public class ReadOnlyChannelBufferTest {
ByteBuffer bb = ByteBuffer.allocate(100);
ByteBuffer[] bbs = new ByteBuffer[] { ByteBuffer.allocate(101), ByteBuffer.allocate(102) };
CharBuffer cb = CharBuffer.allocate(100);
expect(buf.toByteBuffer(23, 24)).andReturn(bb);
expect(buf.toByteBuffers(25, 26)).andReturn(bbs);
expect(buf.getString(27, 28, cb, CharsetUtil.UTF_8)).andReturn(29);
expect(buf.toString(27, 28, CharsetUtil.UTF_8)).andReturn("29");
expect(buf.capacity()).andReturn(30);
replay(buf);
@ -128,7 +126,7 @@ public class ReadOnlyChannelBufferTest {
assertEquals(102, roBBs[1].capacity());
assertTrue(roBBs[1].isReadOnly());
assertEquals(29, roBuf.getString(27, 28, cb, CharsetUtil.UTF_8));
assertEquals("29", roBuf.toString(27, 28, CharsetUtil.UTF_8));
assertEquals(30, roBuf.capacity());
verify(buf);