Added more toString() variants to ChannelBuffer - makes ChannelBuffer more convenient to decode a NUL-terminated string.

This commit is contained in:
Trustin Lee 2008-08-10 01:20:30 +00:00
parent 19fcbac575
commit 58599fe343
3 changed files with 34 additions and 0 deletions

View File

@ -459,6 +459,25 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
return toString(readerIndex, readableBytes(), charsetName);
}
public String toString(String charsetName, ChannelBufferIndexFinder terminatorFinder) {
return toString(readerIndex, readableBytes(), charsetName, terminatorFinder);
}
public String toString(
int index, int length, String charsetName,
ChannelBufferIndexFinder 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 int indexOf(int fromIndex, int toIndex, byte value) {
return ChannelBuffers.indexOf(this, fromIndex, toIndex, value);
}

View File

@ -694,7 +694,9 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
ByteBuffer[] toByteBuffers(int index, int length);
String toString(String charsetName);
String toString(String charsetName, ChannelBufferIndexFinder terminatorFinder);
String toString(int index, int length, String charsetName);
String toString(int index, int length, String charsetName, ChannelBufferIndexFinder terminatorFinder);
/**
* Returns a hash code which was calculated from the content of this

View File

@ -448,11 +448,24 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
return buffer.toString(index, length, charsetName);
}
public String toString(
int index, int length, String charsetName,
ChannelBufferIndexFinder terminatorFinder) {
checkIndex(index, length);
return buffer.toString(index, length, charsetName, terminatorFinder);
}
public String toString(String charsetName) {
reject();
return null;
}
public String toString(
String charsetName, ChannelBufferIndexFinder terminatorFinder) {
reject();
return null;
}
@Override
public String toString() {
return buffer.toString();