use of io.servicetalk.buffer.api.CompositeBuffer in project servicetalk by apple.
the class ClientEffectiveStrategyTest method getResponse.
private String getResponse(ClientType clientType, StreamingHttpClient client) throws Exception {
switch(clientType) {
case Blocking:
BlockingHttpClient blockingClient = client.asBlockingClient();
return blockingClient.request(blockingClient.get("/")).payloadBody().toString(StandardCharsets.US_ASCII);
case BlockingStreaming:
BlockingStreamingHttpClient blockingStreamingClient = client.asBlockingStreamingClient();
Supplier<CompositeBuffer> supplier = client.executionContext().bufferAllocator()::newCompositeBuffer;
return StreamSupport.stream(blockingStreamingClient.request(blockingStreamingClient.get("/")).payloadBody().spliterator(), false).reduce((Buffer base, Buffer buffer) -> (base instanceof CompositeBuffer ? ((CompositeBuffer) base) : supplier.get().addBuffer(base)).addBuffer(buffer)).map(buffer -> buffer.toString(StandardCharsets.US_ASCII)).orElse("");
case AsyncStreaming:
return client.request(client.get("/")).flatMap(resp -> resp.payloadBody().collect(() -> client.executionContext().bufferAllocator().newCompositeBuffer(), CompositeBuffer::addBuffer)).toFuture().get().toString(StandardCharsets.US_ASCII);
case Async:
HttpClient httpClient = client.asClient();
return httpClient.request(httpClient.get("/")).toFuture().get().payloadBody().toString(StandardCharsets.US_ASCII);
default:
fail("Unexpected client type " + clientType);
/* NOTREACHED */
return "failed";
}
}
use of io.servicetalk.buffer.api.CompositeBuffer in project servicetalk by apple.
the class HttpDataSourceTransformations method aggregatePayloadAndTrailers.
static Single<PayloadAndTrailers> aggregatePayloadAndTrailers(final DefaultPayloadInfo payloadInfo, final Publisher<?> payloadAndTrailers, final BufferAllocator allocator) {
if (payloadAndTrailers == empty()) {
payloadInfo.setEmpty(true).setMayHaveTrailersAndGenericTypeBuffer(false);
return succeeded(EMPTY_PAYLOAD_AND_TRAILERS);
}
return payloadAndTrailers.collect(PayloadAndTrailers::new, (pair, nextItem) -> {
if (nextItem instanceof Buffer) {
try {
Buffer buffer = (Buffer) nextItem;
if (isAlwaysEmpty(pair.payload)) {
pair.payload = buffer;
} else if (pair.payload instanceof CompositeBuffer) {
((CompositeBuffer) pair.payload).addBuffer(buffer);
} else {
Buffer oldBuffer = pair.payload;
pair.payload = allocator.newCompositeBuffer(MAX_VALUE).addBuffer(oldBuffer).addBuffer(buffer);
}
} catch (IllegalArgumentException cause) {
BufferOverflowException ex = new BufferOverflowException();
ex.initCause(cause);
throw ex;
}
} else if (nextItem instanceof HttpHeaders) {
pair.trailers = (HttpHeaders) nextItem;
} else {
throw new UnsupportedHttpChunkException(nextItem);
}
return pair;
}).map(pair -> {
if (isAlwaysEmpty(pair.payload)) {
payloadInfo.setEmpty(true);
}
if (pair.trailers == null) {
payloadInfo.setMayHaveTrailersAndGenericTypeBuffer(false);
}
return pair;
});
}
use of io.servicetalk.buffer.api.CompositeBuffer in project servicetalk by apple.
the class CancellableResources method postRsStreams.
@Consumes(TEXT_PLAIN)
@Produces(TEXT_PLAIN)
@Path("/rs-streams")
@POST
public Publisher<Buffer> postRsStreams(@QueryParam("subscribe") final boolean subscribe, final Publisher<Buffer> requestContent, @Context final ConnectionContext ctx) {
final BufferAllocator allocator = ctx.executionContext().bufferAllocator();
final CompositeBuffer responseBuffer = allocator.newCompositeBuffer(2).addBuffer(allocator.fromAscii("GOT: "));
// subscribe when it gets the response back from the router
if (subscribe) {
return from(responseBuffer.writeAscii(getContentAsString(requestContent)));
} else {
final AtomicBoolean first = new AtomicBoolean(true);
return requestContent.map(c -> first.compareAndSet(true, false) ? responseBuffer.addBuffer(c) : c);
}
}
use of io.servicetalk.buffer.api.CompositeBuffer in project servicetalk by apple.
the class NettyChannelContentCodec method drainChannelQueueToSingleBuffer.
@Nullable
private static Buffer drainChannelQueueToSingleBuffer(final Queue<Object> queue, final BufferAllocator allocator) {
if (queue.isEmpty()) {
return null;
}
if (queue.size() == 1) {
return newBufferFrom((ByteBuf) queue.poll());
} else {
int accumulateSize = 0;
int components = 0;
for (Object buffer : queue) {
accumulateSize += ((ByteBuf) buffer).readableBytes();
components++;
}
ByteBuf part;
if (accumulateSize <= MAX_SIZE_FOR_MERGED_BUFFER) {
// Try to merge everything together if total size is less than 1KiB (small chunks, ie. footer).
// CompositeBuffer can require some additional work to write and may limit how much data we can
// pass to writev (max of 1024 pointers), so if there are small chunks it may be better to combine them.
Buffer merged = allocator.newBuffer();
while ((part = (ByteBuf) queue.poll()) != null) {
merged.writeBytes(newBufferFrom(part));
}
return merged;
}
CompositeBuffer composite = allocator.newCompositeBuffer(components);
while ((part = (ByteBuf) queue.poll()) != null) {
composite.addBuffer(newBufferFrom(part));
}
return composite;
}
}
use of io.servicetalk.buffer.api.CompositeBuffer in project servicetalk by apple.
the class ProtoDeserializerTest method multipleMessagesInCompositeBuffer.
@Test
void multipleMessagesInCompositeBuffer() throws IOException {
final CompositeBuffer composite = DEFAULT_ALLOCATOR.newCompositeBuffer();
Buffer msg = grpcBufferFor("Hello");
while (msg.readableBytes() > 0) {
composite.addBuffer(msg.readSlice(1));
}
composite.addBuffer(grpcBufferFor("Hello1"));
List<String> deserialized = deserialize(composite);
assertThat("Unexpected messages deserialized.", deserialized, contains("Hello", "Hello1"));
}
Aggregations