Added ChannelBuffer.readSlice() methods in case a user doesn't want to copy at the risk of modified content

This commit is contained in:
Trustin Lee 2008-08-09 23:47:40 +00:00
parent b33bbf7da7
commit 289e84d69c
3 changed files with 30 additions and 0 deletions

View File

@ -224,6 +224,20 @@ public abstract class AbstractChannelBuffer implements ChannelBuffer {
return readBytes(endIndex);
}
public ChannelBuffer readSlice(int length) {
ChannelBuffer slice = slice(readerIndex, length);
readerIndex += length;
return slice;
}
public ChannelBuffer readSlice(ChannelBufferIndexFinder endIndexFinder) {
int endIndex = indexOf(readerIndex, writerIndex, endIndexFinder);
if (endIndex < 0) {
throw new NoSuchElementException();
}
return readSlice(endIndex);
}
public void readBytes(byte[] dst, int dstIndex, int length) {
checkReadableBytes(length);
getBytes(readerIndex, dst, dstIndex, length);

View File

@ -604,6 +604,8 @@ public interface ChannelBuffer extends Comparable<ChannelBuffer> {
ChannelBuffer readBytes(int length);
ChannelBuffer readBytes(ChannelBufferIndexFinder endIndexFinder);
ChannelBuffer readSlice(int length);
ChannelBuffer readSlice(ChannelBufferIndexFinder endIndexFinder);
void readBytes(ChannelBuffer dst);
void readBytes(ChannelBuffer dst, int length);
void readBytes(ChannelBuffer dst, int dstIndex, int length);

View File

@ -246,6 +246,20 @@ class ReplayingDecoderBuffer implements ChannelBuffer {
return buffer.readBytes(length);
}
public ChannelBuffer readSlice(
ChannelBufferIndexFinder endIndexFinder) {
int endIndex = buffer.indexOf(buffer.readerIndex(), buffer.writerIndex(), endIndexFinder);
if (endIndex < 0) {
throw REPLAY;
}
return readSlice(endIndex);
}
public ChannelBuffer readSlice(int length) {
checkReadableBytes(length);
return buffer.readSlice(length);
}
public void readBytes(OutputStream out, int length) throws IOException {
reject();
}