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
This commit is contained in:
Trustin Lee 2010-01-14 11:44:11 +00:00
parent 8a574a7eb6
commit 55b0bc2c2c
14 changed files with 232 additions and 188 deletions

View File

@ -19,11 +19,15 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.CharBuffer;
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.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import org.jboss.netty.util.CharsetUtil;
/** /**
* A skeletal implementation of a buffer. * A skeletal implementation of a buffer.
@ -171,6 +175,44 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
dst.writerIndex(dst.writerIndex() + length); 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) { public void setChar(int index, char value) {
setShort(index, (short) value); setShort(index, (short) value);
} }
@ -380,6 +422,35 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
readerIndex += length; 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) { public void skipBytes(int length) {
int newReaderIndex = readerIndex + length; int newReaderIndex = readerIndex + length;
if (newReaderIndex > writerIndex) { if (newReaderIndex > writerIndex) {
@ -532,40 +603,30 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
} }
public String toString(Charset charset) { public String toString(Charset charset) {
return toString(readerIndex, readableBytes(), charset); return getString(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);
} }
public String toString(int index, int length, String charsetName, public String toString(int index, int length, String charsetName,
ChannelBufferIndexFinder terminatorFinder) { 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) { 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, public String toString(String charsetName,
ChannelBufferIndexFinder terminatorFinder) { ChannelBufferIndexFinder terminatorFinder) {
return toString(Charset.forName(charsetName), terminatorFinder); return toString(readerIndex, readableBytes(), charsetName, terminatorFinder);
} }
public String toString(String charsetName) { public String toString(String charsetName) {

View File

@ -24,10 +24,8 @@ import java.nio.CharBuffer;
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.CharacterCodingException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import org.jboss.netty.util.CharsetUtil; 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) { if (length == 0) {
return ""; return 0;
} }
final CharsetDecoder decoder = CharsetUtil.getDecoder(charset); final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
final int start = dst.position();
final ByteBuffer src = final ByteBuffer src =
((ByteBuffer) buffer.duplicate().position( ((ByteBuffer) buffer.duplicate().position(
index).limit(index + length)).order(order()); index).limit(index + length)).order(order());
final CharBuffer dst = CharBuffer.allocate( ChannelBuffers.decodeString(src, dst, decoder);
(int) ((double) length * decoder.maxCharsPerByte())); return dst.position() - start;
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();
} }
public ChannelBuffer slice(int index, int length) { public ChannelBuffer slice(int index, int length) {

View File

@ -20,6 +20,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.CharBuffer;
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.nio.charset.Charset;
@ -707,6 +708,22 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
*/ */
int getBytes(int index, GatheringByteChannel out, int length) throws IOException; 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 * Sets the specified byte at the specified absolute {@code index} in this
* buffer. * buffer.
@ -1236,6 +1253,15 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
*/ */
int readBytes(GatheringByteChannel out, int length) throws IOException; 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 * Increases the current {@code readerIndex} by the specified
* {@code length} in this buffer. * {@code length} in this buffer.
@ -1629,62 +1655,20 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
String toString(String charsetName); String toString(String charsetName);
/** /**
* Decodes this buffer's readable bytes into a string until the specified * @deprecated Use {@link #getString(int, int, Charset)} instead.
* {@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 @Deprecated
String toString( String toString(
String charsetName, ChannelBufferIndexFinder terminatorFinder); String charsetName, ChannelBufferIndexFinder terminatorFinder);
/** /**
* Decodes this buffer's sub-region into a string with the specified * @deprecated Use {@link #getString(int, int, Charset)} instead.
* 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 @Deprecated
String toString(int index, int length, String charsetName); String toString(int index, int length, String charsetName);
/** /**
* Decodes this buffer's readable bytes into a string until the specified * @deprecated Use {@link #getString(int, int, Charset)} instead.
* {@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 @Deprecated
String toString( String toString(

View File

@ -20,6 +20,7 @@ import java.nio.ByteOrder;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException; import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder; import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult; import java.nio.charset.CoderResult;
import java.util.ArrayList; import java.util.ArrayList;
@ -795,20 +796,7 @@ public class ChannelBuffers {
CharBuffer src = buffer; CharBuffer src = buffer;
ByteBuffer dst = ByteBuffer.allocate( ByteBuffer dst = ByteBuffer.allocate(
(int) ((double) buffer.remaining() * encoder.maxBytesPerChar())); (int) ((double) buffer.remaining() * encoder.maxBytesPerChar()));
ChannelBuffers.encodeString(src, dst, 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);
}
ChannelBuffer result = wrappedBuffer(endianness, dst.array()); ChannelBuffer result = wrappedBuffer(endianness, dst.array());
result.writerIndex(dst.position()); result.writerIndex(dst.position());
return result; return result;
@ -1126,6 +1114,36 @@ public class ChannelBuffers {
return -1; 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() { private ChannelBuffers() {
// Unused // Unused
} }

View File

@ -23,10 +23,8 @@ import java.nio.ByteOrder;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.channels.GatheringByteChannel; import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel; import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -592,15 +590,15 @@ 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) { public int getString(int index, int length, CharBuffer dst, Charset charset) {
int componentId = componentId(index); int componentId = componentId(index);
if (index + length <= indices[componentId + 1]) { if (index + length <= indices[componentId + 1]) {
return components[componentId].toString( return components[componentId].getString(
index - indices[componentId], length, charset); index - indices[componentId], length, dst, charset);
} }
if (length == 0) { if (length == 0) {
return ""; return 0;
} }
byte[] data = new byte[length]; byte[] data = new byte[length];
@ -620,24 +618,10 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer {
} }
final CharsetDecoder decoder = CharsetUtil.getDecoder(charset); final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
final int start = dst.position();
final ByteBuffer src = ByteBuffer.wrap(data); final ByteBuffer src = ByteBuffer.wrap(data);
final CharBuffer dst = CharBuffer.allocate( ChannelBuffers.decodeString(src, dst, decoder);
(int) ((double) length * decoder.maxCharsPerByte())); return dst.position() - start;
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();
} }
private int componentId(int index) { private int componentId(int index) {

View File

@ -20,6 +20,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.CharBuffer;
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.nio.charset.Charset;
@ -181,7 +182,7 @@ public class DuplicatedChannelBuffer extends AbstractChannelBuffer implements Wr
return buffer.toByteBuffer(index, length); 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) {
return buffer.toString(index, length, charset); return buffer.getString(index, length, dst, charset);
} }
} }

View File

@ -20,6 +20,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.CharBuffer;
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.nio.charset.Charset;
@ -291,7 +292,7 @@ public class DynamicChannelBuffer extends AbstractChannelBuffer {
return buffer.toByteBuffer(index, length); 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) {
return buffer.toString(index, length, charset); return buffer.getString(index, length, dst, charset);
} }
} }

View File

@ -23,10 +23,8 @@ import java.nio.CharBuffer;
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.CharacterCodingException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import org.jboss.netty.util.CharsetUtil; import org.jboss.netty.util.CharsetUtil;
@ -124,6 +122,18 @@ public abstract class HeapChannelBuffer extends AbstractChannelBuffer {
return out.write(ByteBuffer.wrap(array, index, length)); 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) { public void setByte(int index, byte value) {
array[index] = value; array[index] = value;
} }
@ -210,30 +220,4 @@ public abstract class HeapChannelBuffer extends AbstractChannelBuffer {
public ByteBuffer toByteBuffer(int index, int length) { public ByteBuffer toByteBuffer(int index, int length) {
return ByteBuffer.wrap(array, index, length).order(order()); 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();
}
} }

View File

@ -20,6 +20,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.CharBuffer;
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;
@ -191,11 +192,11 @@ public class ReadOnlyChannelBuffer extends AbstractChannelBuffer implements Wrap
return bufs; return bufs;
} }
public String toString(int index, int length, Charset charset) {
return buffer.toString(index, length, charset);
}
public int capacity() { public int capacity() {
return buffer.capacity(); return buffer.capacity();
} }
public int getString(int index, int length, CharBuffer dst, Charset charset) {
return buffer.getString(index, length, dst, charset);
}
} }

View File

@ -20,6 +20,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.CharBuffer;
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.nio.charset.Charset;
@ -214,9 +215,9 @@ 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) { public int getString(int index, int length, CharBuffer dst, Charset charset) {
checkIndex(index, length); checkIndex(index, length);
return buffer.toString(index + adjustment, length, charset); return buffer.getString(index + adjustment, length, dst, charset);
} }
private void checkIndex(int index) { private void checkIndex(int index) {

View File

@ -20,6 +20,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.CharBuffer;
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.nio.charset.Charset;
@ -208,9 +209,9 @@ 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) { public int getString(int index, int length, CharBuffer dst, Charset charset) {
checkIndex(index, length); checkIndex(index, length);
return buffer.toString(index, length, charset); return buffer.getString(index, length, dst, charset);
} }
private void checkIndex(int index) { private void checkIndex(int index) {

View File

@ -20,6 +20,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.CharBuffer;
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.nio.charset.Charset;
@ -324,6 +325,28 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
throw new UnreplayableOperationException(); 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() { public int readerIndex() {
return buffer.readerIndex(); return buffer.readerIndex();
} }
@ -505,27 +528,37 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
return buffer.toByteBuffers(index, length); 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); checkIndex(index, length);
return buffer.toString(index, length, charset); return buffer.getString(index, length, charset);
} }
public String toString( public int getString(int index, int length, Appendable out, Charset charset)
int index, int length, Charset charset, throws IOException {
ChannelBufferIndexFinder terminatorFinder) {
checkIndex(index, length); 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) { public String toString(Charset charsetName) {
throw new UnreplayableOperationException(); throw new UnreplayableOperationException();
} }
public String toString(
Charset charset, ChannelBufferIndexFinder terminatorFinder) {
throw new UnreplayableOperationException();
}
@Deprecated @Deprecated
public String toString(int index, int length, String charsetName) { public String toString(int index, int length, String charsetName) {
checkIndex(index, length); checkIndex(index, length);

View File

@ -1537,15 +1537,6 @@ public abstract class AbstractChannelBufferTest {
buffer.clear(); buffer.clear();
buffer.writeBytes(copiedBuffer("Hello, World!", CharsetUtil.ISO_8859_1)); buffer.writeBytes(copiedBuffer("Hello, World!", CharsetUtil.ISO_8859_1));
assertEquals("Hello, World!", buffer.toString(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 @Test

View File

@ -98,7 +98,7 @@ public class ReadOnlyChannelBufferTest {
expect(buf.toByteBuffer(23, 24)).andReturn(bb); expect(buf.toByteBuffer(23, 24)).andReturn(bb);
expect(buf.toByteBuffers(25, 26)).andReturn(bbs); 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); expect(buf.capacity()).andReturn(30);
replay(buf); replay(buf);
@ -126,7 +126,7 @@ public class ReadOnlyChannelBufferTest {
assertEquals(102, roBBs[1].capacity()); assertEquals(102, roBBs[1].capacity());
assertTrue(roBBs[1].isReadOnly()); 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()); assertEquals(30, roBuf.capacity());
verify(buf); verify(buf);