Adds a method to get the buffer for a specific index in CompositeByteBuf

This fixes #414
This commit is contained in:
Cruz Julian Bishop 2012-06-28 10:24:18 +10:00 committed by Trustin Lee
parent 8b5ab52a5b
commit 343d674839

View File

@ -575,6 +575,24 @@ public class CompositeByteBuf extends AbstractByteBuf {
dst.writerIndex(dst.capacity()); dst.writerIndex(dst.capacity());
} }
public ByteBuf getBufferFor(int index) throws IOException {
if (index < 0 || index > capacity()) {
throw new IndexOutOfBoundsException("Invalid index: " + index
+ " - Bytes needed: " + (index) + ", maximum is "
+ capacity());
}
List<ByteBuf> components = decompose(index, 1);
switch (components.size()) {
case 0:
return Unpooled.EMPTY_BUFFER;
case 1:
return components.get(0);
default:
throw new IOException("Index " + index + " is part of " + components.size() + " buffers!");
}
}
@Override @Override
public ByteBuf slice(int index, int length) { public ByteBuf slice(int index, int length) {
if (index == 0) { if (index == 0) {