From cb8c9767f6acad1ffc94d4a8895b042624412f76 Mon Sep 17 00:00:00 2001 From: Cruz Julian Bishop Date: Thu, 28 Jun 2012 10:24:18 +1000 Subject: [PATCH] Adds a method to get the buffer for a specific index in CompositeByteBuf This fixes #414 --- .../java/io/netty/buffer/CompositeByteBuf.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java index 9329de9023..f9ecc34aa6 100644 --- a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java @@ -574,6 +574,24 @@ public class CompositeByteBuf extends AbstractByteBuf { 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 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 public ByteBuf slice(int index, int length) {