Replace CompositeBuffer.extendComposite with extendWith

This commit is contained in:
Chris Vest 2021-04-27 12:19:53 +02:00
parent 2f12455fa9
commit f2033263dd
5 changed files with 41 additions and 53 deletions

View File

@ -48,7 +48,7 @@ import java.util.stream.Stream;
* </li> * </li>
* </ul> * </ul>
* Composite buffers can later be extended with internally allocated components, with {@link #ensureWritable(int)}, * Composite buffers can later be extended with internally allocated components, with {@link #ensureWritable(int)},
* or with externally allocated buffers, using {@link #extendComposite(Buffer, Buffer)}. * or with externally allocated buffers, using {@link #extendWith(Buffer)}.
* *
* <h3>Constituent buffer requirements</h3> * <h3>Constituent buffer requirements</h3>
* *
@ -206,7 +206,7 @@ public final class CompositeBuffer extends RcSupport<Buffer, CompositeBuffer> im
/** /**
* Create an empty composite buffer, that has no components. The buffer can be extended with components using either * Create an empty composite buffer, that has no components. The buffer can be extended with components using either
* {@link #ensureWritable(int)} or {@link #extendComposite(Buffer, Buffer)}. * {@link #ensureWritable(int)} or {@link #extendWith(Buffer)}.
* *
* @param allocator The allocator for the composite buffer. This allocator will be used e.g. to service * @param allocator The allocator for the composite buffer. This allocator will be used e.g. to service
* {@link #ensureWritable(int)} calls. * {@link #ensureWritable(int)} calls.
@ -216,28 +216,6 @@ public final class CompositeBuffer extends RcSupport<Buffer, CompositeBuffer> im
return new CompositeBuffer(allocator, EMPTY_BUFFER_ARRAY, COMPOSITE_DROP, false); return new CompositeBuffer(allocator, EMPTY_BUFFER_ARRAY, COMPOSITE_DROP, false);
} }
/**
* Extend the given composite buffer with the given extension buffer.
* This works as if the extension had originally been included at the end of the list of constituent buffers when
* the composite buffer was created.
* The composite buffer is modified in-place.
*
* @see #compose(BufferAllocator, Buffer...)
* @see #compose(BufferAllocator, Send...)
* @param composite The composite buffer (from a prior {@link #compose(BufferAllocator, Buffer...)} call) to extend
* with the given extension buffer.
* @param extension The buffer to extend the composite buffer with.
*/
public static void extendComposite(Buffer composite, Buffer extension) {
if (!isComposite(composite)) {
throw new IllegalArgumentException(
"Expected the first buffer to be a composite buffer, " +
"but it is a " + composite.getClass() + " buffer: " + composite + '.');
}
CompositeBuffer compositeBuffer = (CompositeBuffer) composite;
compositeBuffer.extendWith(extension);
}
/** /**
* Check if the given buffer is a {@linkplain #compose(BufferAllocator, Buffer...) composite} buffer or not. * Check if the given buffer is a {@linkplain #compose(BufferAllocator, Buffer...) composite} buffer or not.
* @param composite The buffer to check. * @param composite The buffer to check.
@ -854,7 +832,17 @@ public final class CompositeBuffer extends RcSupport<Buffer, CompositeBuffer> im
unsafeExtendWith(extension); unsafeExtendWith(extension);
} }
void extendWith(Buffer extension) { /**
* Extend this composite buffer with the given extension buffer.
* This works as if the extension had originally been included at the end of the list of constituent buffers when
* the composite buffer was created.
* The extension buffer is added to the end of this composite buffer, which is modified in-place.
*
* @see #compose(BufferAllocator, Buffer...)
* @see #compose(BufferAllocator, Send...)
* @param extension The buffer to extend the composite buffer with.
*/
public void extendWith(Buffer extension) {
Objects.requireNonNull(extension, "Extension buffer cannot be null."); Objects.requireNonNull(extension, "Extension buffer cannot be null.");
if (!isOwned()) { if (!isOwned()) {
throw new IllegalStateException("This buffer cannot be extended because it is not in an owned state."); throw new IllegalStateException("This buffer cannot be extended because it is not in an owned state.");

View File

@ -65,7 +65,7 @@ public class BufferCompositionTest extends BufferTestSupport {
a.close(); a.close();
try { try {
e = assertThrows(IllegalArgumentException.class, e = assertThrows(IllegalArgumentException.class,
() -> CompositeBuffer.extendComposite(composite, a)); () -> ((CompositeBuffer) composite).extendWith(a));
assertThat(e).hasMessageContaining("duplicate"); assertThat(e).hasMessageContaining("duplicate");
} finally { } finally {
a.acquire(); a.acquire();
@ -94,15 +94,15 @@ public class BufferCompositionTest extends BufferTestSupport {
try (buf; a) { try (buf; a) {
a.close(); a.close();
try { try {
assertThrows(IllegalArgumentException.class, () -> CompositeBuffer.extendComposite(buf, buf)); assertThrows(IllegalArgumentException.class, () -> ((CompositeBuffer) buf).extendWith(buf));
assertTrue(buf.isOwned()); assertTrue(buf.isOwned());
try (Buffer composite = CompositeBuffer.compose(allocator, buf)) { try (Buffer composite = CompositeBuffer.compose(allocator, buf)) {
// the composing increments the reference count of constituent buffers... // the composing increments the reference count of constituent buffers...
// counter-act this so it can be extended: // counter-act this so it can be extended:
a.close(); // buf is now owned so it can be extended. a.close(); // buf is now owned so it can be extended.
try { try {
assertThrows(IllegalArgumentException.class, assertThrows(ClassCastException.class,
() -> CompositeBuffer.extendComposite(buf, composite)); () -> ((CompositeBuffer) buf).extendWith(composite));
} finally { } finally {
a.acquire(); // restore the reference count to align with our try-with-resources structure. a.acquire(); // restore the reference count to align with our try-with-resources structure.
} }
@ -162,7 +162,7 @@ public class BufferCompositionTest extends BufferTestSupport {
try (BufferAllocator allocator = BufferAllocator.heap(); try (BufferAllocator allocator = BufferAllocator.heap();
Buffer a = allocator.allocate(8); Buffer a = allocator.allocate(8);
Buffer b = allocator.allocate(8)) { Buffer b = allocator.allocate(8)) {
var exc = assertThrows(IllegalArgumentException.class, () -> CompositeBuffer.extendComposite(a, b)); var exc = assertThrows(IllegalArgumentException.class, () -> ((CompositeBuffer) a).extendWith(b));
assertThat(exc).hasMessageContaining("Expected").hasMessageContaining("composite"); assertThat(exc).hasMessageContaining("Expected").hasMessageContaining("composite");
} }
} }
@ -174,7 +174,7 @@ public class BufferCompositionTest extends BufferTestSupport {
Buffer b = allocator.allocate(8); Buffer b = allocator.allocate(8);
Buffer composed = CompositeBuffer.compose(allocator, a)) { Buffer composed = CompositeBuffer.compose(allocator, a)) {
try (Buffer ignore = composed.acquire()) { try (Buffer ignore = composed.acquire()) {
var exc = assertThrows(IllegalStateException.class, () -> CompositeBuffer.extendComposite(composed, b)); var exc = assertThrows(IllegalStateException.class, () -> ((CompositeBuffer) composed).extendWith(b));
assertThat(exc).hasMessageContaining("owned"); assertThat(exc).hasMessageContaining("owned");
} }
} }
@ -189,7 +189,7 @@ public class BufferCompositionTest extends BufferTestSupport {
} }
try (composite) { try (composite) {
var exc = assertThrows(IllegalArgumentException.class, var exc = assertThrows(IllegalArgumentException.class,
() -> CompositeBuffer.extendComposite(composite, composite)); () -> ((CompositeBuffer) composite).extendWith(composite));
assertThat(exc).hasMessageContaining("cannot be extended"); assertThat(exc).hasMessageContaining("cannot be extended");
} }
} }
@ -199,7 +199,7 @@ public class BufferCompositionTest extends BufferTestSupport {
public void extendingWithZeroCapacityBufferHasNoEffect() { public void extendingWithZeroCapacityBufferHasNoEffect() {
try (BufferAllocator allocator = BufferAllocator.heap(); try (BufferAllocator allocator = BufferAllocator.heap();
Buffer composite = CompositeBuffer.compose(allocator)) { Buffer composite = CompositeBuffer.compose(allocator)) {
CompositeBuffer.extendComposite(composite, composite); ((CompositeBuffer) composite).extendWith(composite);
assertThat(composite.capacity()).isZero(); assertThat(composite.capacity()).isZero();
assertThat(composite.countComponents()).isZero(); assertThat(composite.countComponents()).isZero();
} }
@ -211,7 +211,7 @@ public class BufferCompositionTest extends BufferTestSupport {
assertThat(composite.capacity()).isOne(); assertThat(composite.capacity()).isOne();
assertThat(composite.countComponents()).isOne(); assertThat(composite.countComponents()).isOne();
try (Buffer b = CompositeBuffer.compose(allocator)) { try (Buffer b = CompositeBuffer.compose(allocator)) {
CompositeBuffer.extendComposite(composite, b); ((CompositeBuffer) composite).extendWith(b);
} }
assertTrue(composite.isOwned()); assertTrue(composite.isOwned());
assertThat(composite.capacity()).isOne(); assertThat(composite.capacity()).isOne();
@ -223,7 +223,7 @@ public class BufferCompositionTest extends BufferTestSupport {
public void extendingCompositeBufferWithNullMustThrow() { public void extendingCompositeBufferWithNullMustThrow() {
try (BufferAllocator allocator = BufferAllocator.heap(); try (BufferAllocator allocator = BufferAllocator.heap();
Buffer composite = CompositeBuffer.compose(allocator)) { Buffer composite = CompositeBuffer.compose(allocator)) {
assertThrows(NullPointerException.class, () -> CompositeBuffer.extendComposite(composite, null)); assertThrows(NullPointerException.class, () -> ((CompositeBuffer) composite).extendWith(null));
} }
} }
@ -233,7 +233,7 @@ public class BufferCompositionTest extends BufferTestSupport {
Buffer composite = CompositeBuffer.compose(allocator)) { Buffer composite = CompositeBuffer.compose(allocator)) {
assertThat(composite.capacity()).isZero(); assertThat(composite.capacity()).isZero();
try (Buffer buf = allocator.allocate(8, BIG_ENDIAN)) { try (Buffer buf = allocator.allocate(8, BIG_ENDIAN)) {
CompositeBuffer.extendComposite(composite, buf); ((CompositeBuffer) composite).extendWith(buf);
} }
assertThat(composite.capacity()).isEqualTo(8); assertThat(composite.capacity()).isEqualTo(8);
composite.writeLong(0x0102030405060708L); composite.writeLong(0x0102030405060708L);
@ -247,7 +247,7 @@ public class BufferCompositionTest extends BufferTestSupport {
Buffer composite = CompositeBuffer.compose(allocator)) { Buffer composite = CompositeBuffer.compose(allocator)) {
assertThat(composite.capacity()).isZero(); assertThat(composite.capacity()).isZero();
try (Buffer buf = allocator.allocate(8, LITTLE_ENDIAN)) { try (Buffer buf = allocator.allocate(8, LITTLE_ENDIAN)) {
CompositeBuffer.extendComposite(composite, buf); ((CompositeBuffer) composite).extendWith(buf);
} }
assertThat(composite.capacity()).isEqualTo(8); assertThat(composite.capacity()).isEqualTo(8);
composite.writeLong(0x0102030405060708L); composite.writeLong(0x0102030405060708L);
@ -265,7 +265,7 @@ public class BufferCompositionTest extends BufferTestSupport {
try (composite) { try (composite) {
try (Buffer b = allocator.allocate(8, LITTLE_ENDIAN)) { try (Buffer b = allocator.allocate(8, LITTLE_ENDIAN)) {
var exc = assertThrows(IllegalArgumentException.class, var exc = assertThrows(IllegalArgumentException.class,
() -> CompositeBuffer.extendComposite(composite, b)); () -> ((CompositeBuffer) composite).extendWith(b));
assertThat(exc).hasMessageContaining("byte order"); assertThat(exc).hasMessageContaining("byte order");
} }
} }
@ -282,7 +282,7 @@ public class BufferCompositionTest extends BufferTestSupport {
try (composite) { try (composite) {
try (Buffer b = allocator.allocate(8, BIG_ENDIAN)) { try (Buffer b = allocator.allocate(8, BIG_ENDIAN)) {
var exc = assertThrows(IllegalArgumentException.class, var exc = assertThrows(IllegalArgumentException.class,
() -> CompositeBuffer.extendComposite(composite, b)); () -> ((CompositeBuffer) composite).extendWith(b));
assertThat(exc).hasMessageContaining("byte order"); assertThat(exc).hasMessageContaining("byte order");
} }
} }
@ -294,7 +294,7 @@ public class BufferCompositionTest extends BufferTestSupport {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
try (Buffer composite = CompositeBuffer.compose(allocator)) { try (Buffer composite = CompositeBuffer.compose(allocator)) {
try (Buffer b = allocator.allocate(8, BIG_ENDIAN)) { try (Buffer b = allocator.allocate(8, BIG_ENDIAN)) {
CompositeBuffer.extendComposite(composite, b); ((CompositeBuffer) composite).extendWith(b);
assertThat(composite.order()).isEqualTo(BIG_ENDIAN); assertThat(composite.order()).isEqualTo(BIG_ENDIAN);
} }
} }
@ -306,7 +306,7 @@ public class BufferCompositionTest extends BufferTestSupport {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
try (Buffer composite = CompositeBuffer.compose(allocator)) { try (Buffer composite = CompositeBuffer.compose(allocator)) {
try (Buffer b = allocator.allocate(8, LITTLE_ENDIAN)) { try (Buffer b = allocator.allocate(8, LITTLE_ENDIAN)) {
CompositeBuffer.extendComposite(composite, b); ((CompositeBuffer) composite).extendWith(b);
assertThat(composite.order()).isEqualTo(LITTLE_ENDIAN); assertThat(composite.order()).isEqualTo(LITTLE_ENDIAN);
} }
} }
@ -318,7 +318,7 @@ public class BufferCompositionTest extends BufferTestSupport {
try (BufferAllocator allocator = BufferAllocator.heap()) { try (BufferAllocator allocator = BufferAllocator.heap()) {
try (Buffer composite = CompositeBuffer.compose(allocator)) { try (Buffer composite = CompositeBuffer.compose(allocator)) {
try (Buffer b = allocator.allocate(8).readOnly(true)) { try (Buffer b = allocator.allocate(8).readOnly(true)) {
CompositeBuffer.extendComposite(composite, b); ((CompositeBuffer) composite).extendWith(b);
assertTrue(composite.readOnly()); assertTrue(composite.readOnly());
} }
} }
@ -336,7 +336,7 @@ public class BufferCompositionTest extends BufferTestSupport {
composite.writeLong(0); composite.writeLong(0);
try (Buffer b = allocator.allocate(8)) { try (Buffer b = allocator.allocate(8)) {
b.writeInt(1); b.writeInt(1);
CompositeBuffer.extendComposite(composite, b); ((CompositeBuffer) composite).extendWith(b);
assertThat(composite.capacity()).isEqualTo(16); assertThat(composite.capacity()).isEqualTo(16);
assertThat(composite.writerOffset()).isEqualTo(12); assertThat(composite.writerOffset()).isEqualTo(12);
} }
@ -356,10 +356,10 @@ public class BufferCompositionTest extends BufferTestSupport {
try (Buffer b = allocator.allocate(8)) { try (Buffer b = allocator.allocate(8)) {
b.writeInt(1); b.writeInt(1);
var exc = assertThrows(IllegalArgumentException.class, var exc = assertThrows(IllegalArgumentException.class,
() -> CompositeBuffer.extendComposite(composite, b)); () -> ((CompositeBuffer) composite).extendWith(b));
assertThat(exc).hasMessageContaining("unwritten gap"); assertThat(exc).hasMessageContaining("unwritten gap");
b.writerOffset(0); b.writerOffset(0);
CompositeBuffer.extendComposite(composite, b); ((CompositeBuffer) composite).extendWith(b);
assertThat(composite.capacity()).isEqualTo(16); assertThat(composite.capacity()).isEqualTo(16);
assertThat(composite.writerOffset()).isEqualTo(4); assertThat(composite.writerOffset()).isEqualTo(4);
} }
@ -380,7 +380,7 @@ public class BufferCompositionTest extends BufferTestSupport {
try (Buffer b = allocator.allocate(8)) { try (Buffer b = allocator.allocate(8)) {
b.writeInt(1); b.writeInt(1);
b.readInt(); b.readInt();
CompositeBuffer.extendComposite(composite, b); ((CompositeBuffer) composite).extendWith(b);
assertThat(composite.capacity()).isEqualTo(16); assertThat(composite.capacity()).isEqualTo(16);
assertThat(composite.writerOffset()).isEqualTo(12); assertThat(composite.writerOffset()).isEqualTo(12);
} }
@ -402,10 +402,10 @@ public class BufferCompositionTest extends BufferTestSupport {
b.writeInt(1); b.writeInt(1);
b.readInt(); b.readInt();
var exc = assertThrows(IllegalArgumentException.class, var exc = assertThrows(IllegalArgumentException.class,
() -> CompositeBuffer.extendComposite(composite, b)); () -> ((CompositeBuffer) composite).extendWith(b));
assertThat(exc).hasMessageContaining("unread gap"); assertThat(exc).hasMessageContaining("unread gap");
b.readerOffset(0); b.readerOffset(0);
CompositeBuffer.extendComposite(composite, b); ((CompositeBuffer) composite).extendWith(b);
assertThat(composite.capacity()).isEqualTo(16); assertThat(composite.capacity()).isEqualTo(16);
assertThat(composite.writerOffset()).isEqualTo(12); assertThat(composite.writerOffset()).isEqualTo(12);
assertThat(composite.readerOffset()).isEqualTo(4); assertThat(composite.readerOffset()).isEqualTo(4);
@ -454,7 +454,7 @@ public class BufferCompositionTest extends BufferTestSupport {
composite = CompositeBuffer.compose(allocator, a); composite = CompositeBuffer.compose(allocator, a);
} }
try (composite; Buffer b = allocator.allocate(8).readOnly(true)) { try (composite; Buffer b = allocator.allocate(8).readOnly(true)) {
assertThrows(IllegalArgumentException.class, () -> CompositeBuffer.extendComposite(composite, b)); assertThrows(IllegalArgumentException.class, () -> ((CompositeBuffer) composite).extendWith(b));
} }
} }
} }
@ -467,7 +467,7 @@ public class BufferCompositionTest extends BufferTestSupport {
composite = CompositeBuffer.compose(allocator, a); composite = CompositeBuffer.compose(allocator, a);
} }
try (composite; Buffer b = allocator.allocate(8)) { try (composite; Buffer b = allocator.allocate(8)) {
assertThrows(IllegalArgumentException.class, () -> CompositeBuffer.extendComposite(composite, b)); assertThrows(IllegalArgumentException.class, () -> ((CompositeBuffer) composite).extendWith(b));
} }
} }
} }

View File

@ -365,7 +365,7 @@ public abstract class BufferTestSupport {
assertThrows(IllegalStateException.class, () -> buf.copyInto(0, new byte[1], 0, 1)); assertThrows(IllegalStateException.class, () -> buf.copyInto(0, new byte[1], 0, 1));
assertThrows(IllegalStateException.class, () -> buf.copyInto(0, ByteBuffer.allocate(1), 0, 1)); assertThrows(IllegalStateException.class, () -> buf.copyInto(0, ByteBuffer.allocate(1), 0, 1));
if (CompositeBuffer.isComposite(buf)) { if (CompositeBuffer.isComposite(buf)) {
assertThrows(IllegalStateException.class, () -> CompositeBuffer.extendComposite(buf, target)); assertThrows(IllegalStateException.class, () -> ((CompositeBuffer) buf).extendWith(target));
} }
} }

View File

@ -87,7 +87,7 @@ public abstract class AlternativeMessageDecoder extends ChannelHandlerAdapter {
&& (collector.writableBytes() == 0 || input.writerOffset() == 0) && (collector.writableBytes() == 0 || input.writerOffset() == 0)
&& (collector.readableBytes() == 0 || input.readerOffset() == 0) && (collector.readableBytes() == 0 || input.readerOffset() == 0)
&& collector.order() == input.order()) { && collector.order() == input.order()) {
CompositeBuffer.extendComposite(collector, input); ((CompositeBuffer) collector).extendWith(input);
drainCollector(ctx); drainCollector(ctx);
return; return;
} }

View File

@ -140,7 +140,7 @@ public abstract class ByteToMessageDecoder extends ChannelHandlerAdapter {
} else { } else {
composite = CompositeBuffer.compose(alloc, cumulation); composite = CompositeBuffer.compose(alloc, cumulation);
} }
CompositeBuffer.extendComposite(composite, in); ((CompositeBuffer) composite).extendWith(in);
return composite; return composite;
} }
}; };