/** * Creates a reusable data chunk. * * @param flush a signal that chunk should be written and flushed from any cache if possible * @param data a data chunk. Should not be reused until {@code releaseCallback} is used * @return a reusable data chunk with no release callback */ static DataChunk create(boolean flush, ByteBuffer data) { return create(flush, data, Utils.EMPTY_RUNNABLE); }
/** * Creates a simple {@link ByteBuffer} backed data chunk. The resulting * instance doesn't have any kind of a lifecycle and as such, it doesn't need * to be released. * * @param byteBuffer a byte buffer to create the request chunk from * @return a request chunk */ static DataChunk create(ByteBuffer byteBuffer) { return create(false, byteBuffer); }
/** * Creates a simple byte array backed data chunk. The resulting * instance doesn't have any kind of a lifecycle and as such, it doesn't need * to be released. * * @param bytes a byte array to create the request chunk from * @return a request chunk */ static DataChunk create(byte[] bytes) { return create(false, ByteBuffer.wrap(bytes)); }
private DataChunk allocateNewChunk() { return DataChunk.create(false, ByteBuffer.allocate(chunkCapacity)); }
@Override public Flow.Publisher<DataChunk> apply(CharSequence s) { if (s == null || s.length() == 0) { return ReactiveStreamsAdapter.publisherToFlow(Mono.empty()); } DataChunk chunk = DataChunk.create(false, charset.encode(s.toString())); return ReactiveStreamsAdapter.publisherToFlow(Mono.just(chunk)); } }
@Override public Flow.Publisher<DataChunk> apply(byte[] bytes) { if ((bytes == null) || (bytes.length == 0)) { return ReactiveStreamsAdapter.publisherToFlow(Mono.empty()); } byte[] bs; if (copy) { bs = new byte[bytes.length]; System.arraycopy(bytes, 0, bs, 0, bytes.length); } else { bs = bytes; } DataChunk chunk = DataChunk.create(false, ByteBuffer.wrap(bs)); return ReactiveStreamsAdapter.publisherToFlow(Mono.just(chunk)); } }
/** * Creates a publisher of single {@link String string}. * * @param publishedType a type. If contains charset then it is used, otherwise use {@code UTF-8}. If {@code null} then * {@code text/plain} is used as a default. * @param charSequence A sequence to publish. * @return new publisher. */ static MediaPublisher create(MediaType publishedType, CharSequence charSequence) { ByteBuffer data = Optional.ofNullable(publishedType) .flatMap(MediaType::charset) .map(Charset::forName) .orElse(StandardCharsets.UTF_8) .encode(charSequence.toString()); Flow.Publisher<DataChunk> publisher = ReactiveStreamsAdapter.publisherToFlow(Flux.just(DataChunk.create(data))); return new MediaPublisher() { @Override public MediaType mediaType() { return publishedType; } @Override public void subscribe(Flow.Subscriber<? super DataChunk> subscriber) { publisher.subscribe(subscriber); } }; } }
WritableByteChannel ch = Channels.newChannel(stream); ch.write(byteBuffer); return DataChunk.create(doFlush, ByteBuffer.wrap(stream.toByteArray())); } catch (IOException e) { throw new IllegalStateException("this never happens", e);
private DataChunk allocateNewChunk() { return DataChunk.create(false, ByteBuffer.allocate(chunkCapacity)); }
@Override public Flow.Publisher<DataChunk> apply(CharSequence s) { if (s == null || s.length() == 0) { return ReactiveStreamsAdapter.publisherToFlow(Mono.empty()); } DataChunk chunk = DataChunk.create(false, charset.encode(s.toString())); return ReactiveStreamsAdapter.publisherToFlow(Mono.just(chunk)); } }
@Override public Flow.Publisher<DataChunk> apply(byte[] bytes) { if ((bytes == null) || (bytes.length == 0)) { return ReactiveStreamsAdapter.publisherToFlow(Mono.empty()); } byte[] bs; if (copy) { bs = new byte[bytes.length]; System.arraycopy(bytes, 0, bs, 0, bytes.length); } else { bs = bytes; } DataChunk chunk = DataChunk.create(false, ByteBuffer.wrap(bs)); return ReactiveStreamsAdapter.publisherToFlow(Mono.just(chunk)); } }
WritableByteChannel ch = Channels.newChannel(stream); ch.write(byteBuffer); return DataChunk.create(doFlush, ByteBuffer.wrap(stream.toByteArray())); } catch (IOException e) { throw new IllegalStateException("this never happens", e);