/** * @return a response content processing function that converts the content to a string */ public static BiFunction<ReactiveResponse, Publisher<ContentChunk>, Publisher<String>> asString() { return (response, content) -> { BufferingProcessor result = new BufferingProcessor(response); content.subscribe(result); return result; }; } }
@Override public void onNext(ContentChunk chunk) { ByteBuffer buffer = chunk.buffer; byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); buffers.add(bytes); chunk.callback.succeeded(); upStreamRequest(1); }
@Override public void onComplete() { int length = buffers.stream().mapToInt(bytes -> bytes.length).sum(); byte[] bytes = new byte[length]; int offset = 0; for (byte[] b : buffers) { int l = b.length; System.arraycopy(b, 0, bytes, offset, l); offset += l; } String encoding = response.getEncoding(); if (encoding == null) { encoding = StandardCharsets.UTF_8.name(); } downStreamOnNext(new String(bytes, Charset.forName(encoding))); super.onComplete(); } }
@Override public void onComplete() { int length = buffers.stream().mapToInt(bytes -> bytes.length).sum(); byte[] bytes = new byte[length]; int offset = 0; for (byte[] b : buffers) { int l = b.length; System.arraycopy(b, 0, bytes, offset, l); offset += l; } String encoding = response.getEncoding(); if (encoding == null) { encoding = StandardCharsets.UTF_8.name(); } downStreamOnNext(new String(bytes, Charset.forName(encoding))); super.onComplete(); } }
@Override public void onNext(ContentChunk chunk) { ByteBuffer buffer = chunk.buffer; byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); buffers.add(bytes); chunk.callback.succeeded(); upStreamRequest(1); }
/** * @return a response content processing function that converts the content to a string */ public static BiFunction<ReactiveResponse, Publisher<ContentChunk>, Publisher<String>> asString() { return (response, content) -> { BufferingProcessor result = new BufferingProcessor(response); content.subscribe(result); return result; }; } }