Add DataBufferUtils.compose

Added a utility method that composes data buffers into a single buffer.
Depending on the `DataBuffer` implementation, the returned buffer may be
a single buffer containing all data of the provided buffers, or it may
be a true composite that contains references to the buffers.

Issue: SPR-16365
This commit is contained in:
Arjen Poutsma
2018-01-11 17:19:32 +01:00
parent c60313de3f
commit 384a399fd2
6 changed files with 99 additions and 3 deletions

View File

@@ -479,5 +479,18 @@ public class DataBufferTests extends AbstractDataBufferAllocatingTestCase {
release(buffer);
}
@Test
public void composite() {
DataBuffer composite = this.bufferFactory.compose(Arrays.asList(stringBuffer("a"),
stringBuffer("b"), stringBuffer("c")));
assertEquals(3, composite.readableByteCount());
byte[] bytes = new byte[3];
composite.read(bytes);
assertArrayEquals(new byte[] {'a','b','c'}, bytes);
release(composite);
}
}

View File

@@ -351,5 +351,19 @@ public class DataBufferUtilsTests extends AbstractDataBufferAllocatingTestCase {
};
}
@Test
public void compose() {
DataBuffer foo = stringBuffer("foo");
DataBuffer bar = stringBuffer("bar");
DataBuffer baz = stringBuffer("baz");
Flux<DataBuffer> flux = Flux.just(foo, bar, baz);
DataBuffer result = DataBufferUtils.compose(flux).block(Duration.ofSeconds(5));
assertEquals("foobarbaz", DataBufferTestUtils.dumpString(result, StandardCharsets.UTF_8));
release(result);
}
}