Add CompositeByteBuf.consolidate()
- also added test cases that test automatic / full / ranged consolidation
This commit is contained in:
parent
a442789f7a
commit
728306b64f
@ -37,6 +37,7 @@ public interface CompositeByteBuf extends ByteBuf, Iterable<ByteBuf> {
|
||||
ByteBuf componentAtOffset(int offset);
|
||||
|
||||
void discardReadComponents();
|
||||
void consolidate();
|
||||
void consolidate(int cIndex, int numComponents);
|
||||
|
||||
int toComponentIndex(int offset);
|
||||
|
@ -321,9 +321,7 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit
|
||||
@Override
|
||||
public void removeComponents(int cIndex, int numComponents) {
|
||||
checkComponentIndex(cIndex, numComponents);
|
||||
for (int i = 0; i < numComponents; i ++) {
|
||||
components.remove(cIndex);
|
||||
}
|
||||
components.subList(cIndex, cIndex + numComponents).clear();
|
||||
updateComponentOffsets(cIndex);
|
||||
}
|
||||
|
||||
@ -1045,6 +1043,28 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void consolidate() {
|
||||
final int numComponents = numComponents();
|
||||
if (numComponents <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Component last = components.get(numComponents - 1);
|
||||
final int capacity = last.endOffset;
|
||||
final ByteBuf consolidated = last.buf.unsafe().newBuffer(capacity);
|
||||
|
||||
for (int i = 0; i < numComponents; i ++) {
|
||||
ByteBuf b = components.get(i).buf;
|
||||
consolidated.writeBytes(b);
|
||||
b.unsafe().release();
|
||||
}
|
||||
|
||||
components.clear();
|
||||
components.add(new Component(consolidated));
|
||||
updateComponentOffsets(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void consolidate(int cIndex, int numComponents) {
|
||||
checkComponentIndex(cIndex, numComponents);
|
||||
@ -1063,10 +1083,7 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit
|
||||
b.unsafe().release();
|
||||
}
|
||||
|
||||
for (int i = numComponents - 1; i > 0; i --) {
|
||||
components.remove(cIndex);
|
||||
}
|
||||
|
||||
components.subList(cIndex + 1, endCIndex).clear();
|
||||
components.set(cIndex, new Component(consolidated));
|
||||
updateComponentOffsets(cIndex);
|
||||
}
|
||||
@ -1093,8 +1110,9 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit
|
||||
// Remove read components.
|
||||
int firstComponentId = toComponentIndex(readerIndex);
|
||||
for (int i = 0; i < firstComponentId; i ++) {
|
||||
components.remove(0).buf.unsafe().release();
|
||||
components.get(i).buf.unsafe().release();
|
||||
}
|
||||
components.subList(0, firstComponentId).clear();
|
||||
|
||||
// Update indexes and markers.
|
||||
Component first = components.get(0);
|
||||
@ -1125,8 +1143,9 @@ public class DefaultCompositeByteBuf extends AbstractByteBuf implements Composit
|
||||
// Remove read components.
|
||||
int firstComponentId = toComponentIndex(readerIndex);
|
||||
for (int i = 0; i < firstComponentId; i ++) {
|
||||
components.remove(0).buf.unsafe().release();
|
||||
components.get(i).buf.unsafe().release();
|
||||
}
|
||||
components.subList(0, firstComponentId).clear();
|
||||
|
||||
// Replace the first readable component with a new slice.
|
||||
Component c = components.get(0);
|
||||
|
@ -155,6 +155,53 @@ public abstract class AbstractCompositeChannelBufferTest extends
|
||||
assertTrue(ByteBufUtil.equals(a, b));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAutoConsolidation() {
|
||||
CompositeByteBuf buf = compositeBuffer(2);
|
||||
|
||||
buf.addComponent(wrappedBuffer(new byte[] { 1 }));
|
||||
assertEquals(1, buf.numComponents());
|
||||
|
||||
buf.addComponent(wrappedBuffer(new byte[] { 2, 3 }));
|
||||
assertEquals(2, buf.numComponents());
|
||||
|
||||
buf.addComponent(wrappedBuffer(new byte[] { 4, 5, 6 }));
|
||||
|
||||
assertEquals(1, buf.numComponents());
|
||||
assertTrue(buf.hasArray());
|
||||
assertNotNull(buf.array());
|
||||
assertEquals(0, buf.arrayOffset());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFullConsolidation() {
|
||||
CompositeByteBuf buf = compositeBuffer(Integer.MAX_VALUE);
|
||||
buf.addComponent(wrappedBuffer(new byte[] { 1 }));
|
||||
buf.addComponent(wrappedBuffer(new byte[] { 2, 3 }));
|
||||
buf.addComponent(wrappedBuffer(new byte[] { 4, 5, 6 }));
|
||||
buf.consolidate();
|
||||
|
||||
assertEquals(1, buf.numComponents());
|
||||
assertTrue(buf.hasArray());
|
||||
assertNotNull(buf.array());
|
||||
assertEquals(0, buf.arrayOffset());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRangedConsolidation() {
|
||||
CompositeByteBuf buf = compositeBuffer(Integer.MAX_VALUE);
|
||||
buf.addComponent(wrappedBuffer(new byte[] { 1 }));
|
||||
buf.addComponent(wrappedBuffer(new byte[] { 2, 3 }));
|
||||
buf.addComponent(wrappedBuffer(new byte[] { 4, 5, 6 }));
|
||||
buf.addComponent(wrappedBuffer(new byte[] { 7, 8, 9, 10 }));
|
||||
buf.consolidate(1, 2);
|
||||
|
||||
assertEquals(3, buf.numComponents());
|
||||
assertEquals(wrappedBuffer(new byte[] { 1 }), buf.component(0));
|
||||
assertEquals(wrappedBuffer(new byte[] { 2, 3, 4, 5, 6 }), buf.component(1));
|
||||
assertEquals(wrappedBuffer(new byte[] { 7, 8, 9, 10 }), buf.component(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompositeWrappedBuffer() {
|
||||
ByteBuf header = buffer(12).order(order);
|
||||
@ -232,6 +279,7 @@ public abstract class AbstractCompositeChannelBufferTest extends
|
||||
wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 6, 5).order(order));
|
||||
assertFalse(ByteBufUtil.equals(a, b));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrappedBuffer() {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user