public interface AsyncRequestBody extends SdkPublisher<ByteBuffer>
Publisher
of data (specifically ByteBuffer
chunks) and the HTTP client is the Subscriber of the data (i.e.
to write that data on the wire).
Publisher.subscribe(Subscriber)
should be implemented to tie this publisher to a subscriber. Ideally each call to subscribe
should reproduce the content (i.e if you are reading from a file each subscribe call should produce a
Subscription
that reads the file fully). This allows for automatic retries to be performed in the
SDK. If the content is not reproducible, an exception may be thrown from any subsequent Publisher.subscribe(Subscriber)
calls.
It is important to only send the number of chunks that the subscriber requests to avoid out of memory situations. The
subscriber does it's own buffering so it's usually not needed to buffer in the publisher. Additional permits for chunks will be
notified via the Subscription.request(long)
method.
FileAsyncRequestBody
,
ByteBuffersAsyncRequestBody
Modifier and Type | Method and Description |
---|---|
Optional<Long> |
contentLength() |
default String |
contentType() |
static AsyncRequestBody |
empty()
Creates an
AsyncRequestBody with no content. |
static BlockingInputStreamAsyncRequestBody |
forBlockingInputStream(Long contentLength)
Creates a
BlockingInputStreamAsyncRequestBody to use for writing an input stream to the downstream service. |
static BlockingOutputStreamAsyncRequestBody |
forBlockingOutputStream(Long contentLength)
Creates a
BlockingOutputStreamAsyncRequestBody to use for writing to the downstream service as if it's an output
stream. |
static AsyncRequestBody |
fromByteBuffer(ByteBuffer byteBuffer)
Creates an
AsyncRequestBody from a ByteBuffer . |
static AsyncRequestBody |
fromByteBuffers(ByteBuffer... byteBuffers)
Creates an
AsyncRequestBody from a ByteBuffer array. |
static AsyncRequestBody |
fromByteBuffersUnsafe(ByteBuffer... byteBuffers)
Creates an
AsyncRequestBody from a ByteBuffer array without copying the contents of each
ByteBuffer . |
static AsyncRequestBody |
fromByteBufferUnsafe(ByteBuffer byteBuffer)
|
static AsyncRequestBody |
fromBytes(byte[] bytes)
Creates an
AsyncRequestBody from a byte array. |
static AsyncRequestBody |
fromBytesUnsafe(byte[] bytes)
Creates an
AsyncRequestBody from a byte array without copying the contents of the byte array. |
static AsyncRequestBody |
fromFile(File file)
Creates an
AsyncRequestBody that produces data from the contents of a file. |
static AsyncRequestBody |
fromFile(Path path)
Creates an
AsyncRequestBody that produces data from the contents of a file. |
static AsyncRequestBody |
fromInputStream(InputStream inputStream,
Long contentLength,
ExecutorService executor)
Creates an
AsyncRequestBody from an InputStream . |
static AsyncRequestBody |
fromPublisher(org.reactivestreams.Publisher<ByteBuffer> publisher)
Creates an
AsyncRequestBody the produces data from the input ByteBuffer publisher. |
static AsyncRequestBody |
fromRemainingByteBuffer(ByteBuffer byteBuffer)
Creates an
AsyncRequestBody from the remaining readable bytes from a ByteBuffer . |
static AsyncRequestBody |
fromRemainingByteBuffers(ByteBuffer... byteBuffers)
Creates an
AsyncRequestBody from a ByteBuffer array. |
static AsyncRequestBody |
fromRemainingByteBuffersUnsafe(ByteBuffer... byteBuffers)
Creates an
AsyncRequestBody from a ByteBuffer array without copying the contents of each
ByteBuffer . |
static AsyncRequestBody |
fromRemainingByteBufferUnsafe(ByteBuffer byteBuffer)
|
static AsyncRequestBody |
fromString(String string)
Creates an
AsyncRequestBody that uses a single string as data with UTF_8 encoding. |
static AsyncRequestBody |
fromString(String string,
Charset cs)
Creates an
AsyncRequestBody that uses a single string as data. |
adapt, buffer, doAfterOnCancel, doAfterOnComplete, doAfterOnError, filter, filter, flatMapIterable, limit, map, subscribe
Optional<Long> contentLength()
default String contentType()
static AsyncRequestBody fromPublisher(org.reactivestreams.Publisher<ByteBuffer> publisher)
AsyncRequestBody
the produces data from the input ByteBuffer publisher. The data is delivered when the
publisher publishes the data.publisher
- Publisher of source dataAsyncRequestBody
that produces data send by the publisherstatic AsyncRequestBody fromFile(Path path)
AsyncRequestBody
that produces data from the contents of a file. See
FileAsyncRequestBody.builder()
to create a customized body implementation.path
- Path to file to read from.AsyncRequestBody
that reads data from the specified file.FileAsyncRequestBody
static AsyncRequestBody fromFile(File file)
AsyncRequestBody
that produces data from the contents of a file. See
FileAsyncRequestBody.builder()
to create a customized body implementation.file
- The file to read from.AsyncRequestBody
that reads data from the specified file.FileAsyncRequestBody
static AsyncRequestBody fromString(String string, Charset cs)
AsyncRequestBody
that uses a single string as data.string
- The string to provide.cs
- The Charset
to use.AsyncRequestBody
that uses the specified string.ByteBuffersAsyncRequestBody
static AsyncRequestBody fromString(String string)
AsyncRequestBody
that uses a single string as data with UTF_8 encoding.string
- The string to send.AsyncRequestBody
that uses the specified string.fromString(String, Charset)
static AsyncRequestBody fromBytes(byte[] bytes)
AsyncRequestBody
from a byte array. This will copy the contents of the byte array to prevent
modifications to the provided byte array from being reflected in the AsyncRequestBody
.bytes
- The bytes to send to the service.static AsyncRequestBody fromBytesUnsafe(byte[] bytes)
AsyncRequestBody
from a byte array without copying the contents of the byte array. This
introduces concurrency risks, allowing: (1) the caller to modify the byte array stored in this AsyncRequestBody
implementation AND (2) any users of fromBytesUnsafe(byte[])
to modify the byte array passed into this
AsyncRequestBody
implementation.
As the method name implies, this is unsafe. Use fromBytes(byte[])
unless you're sure you know the risks.
bytes
- The bytes to send to the service.static AsyncRequestBody fromByteBuffer(ByteBuffer byteBuffer)
AsyncRequestBody
from a ByteBuffer
. This will copy the contents of the ByteBuffer
to
prevent modifications to the provided ByteBuffer
from being reflected in the AsyncRequestBody
.
NOTE: This method ignores the current read position. Use fromRemainingByteBuffer(ByteBuffer)
if you need
it to copy only the remaining readable bytes.
byteBuffer
- ByteBuffer to send to the service.static AsyncRequestBody fromRemainingByteBuffer(ByteBuffer byteBuffer)
AsyncRequestBody
from the remaining readable bytes from a ByteBuffer
. This will copy the
remaining contents of the ByteBuffer
to prevent modifications to the provided ByteBuffer
from being
reflected in the AsyncRequestBody
.
Unlike fromByteBuffer(ByteBuffer)
, this method respects the current read position of the buffer and reads
only the remaining bytes.
byteBuffer
- ByteBuffer to send to the service.static AsyncRequestBody fromByteBufferUnsafe(ByteBuffer byteBuffer)
AsyncRequestBody
from a ByteBuffer
without copying the contents of the
ByteBuffer
. This introduces concurrency risks, allowing the caller to modify the ByteBuffer
stored in this
AsyncRequestBody
implementation.
NOTE: This method ignores the current read position. Use fromRemainingByteBufferUnsafe(ByteBuffer)
if you
need it to copy only the remaining readable bytes.
As the method name implies, this is unsafe. Use fromByteBuffer(ByteBuffer)
} unless you're sure you know the
risks.
byteBuffer
- ByteBuffer to send to the service.static AsyncRequestBody fromRemainingByteBufferUnsafe(ByteBuffer byteBuffer)
AsyncRequestBody
from a ByteBuffer
without copying the contents of the
ByteBuffer
. This introduces concurrency risks, allowing the caller to modify the ByteBuffer
stored in this
AsyncRequestBody
implementation.
Unlike fromByteBufferUnsafe(ByteBuffer)
, this method respects the current read position of
the buffer and reads only the remaining bytes.
As the method name implies, this is unsafe. Use fromByteBuffer(ByteBuffer)
} unless you're sure you know the
risks.
byteBuffer
- ByteBuffer to send to the service.static AsyncRequestBody fromByteBuffers(ByteBuffer... byteBuffers)
AsyncRequestBody
from a ByteBuffer
array. This will copy the contents of each ByteBuffer
to prevent modifications to any provided ByteBuffer
from being reflected in the AsyncRequestBody
.
NOTE: This method ignores the current read position of each ByteBuffer
. Use
fromRemainingByteBuffers(ByteBuffer...)
if you need it to copy only the remaining readable bytes.
byteBuffers
- ByteBuffer array to send to the service.static AsyncRequestBody fromRemainingByteBuffers(ByteBuffer... byteBuffers)
AsyncRequestBody
from a ByteBuffer
array. This will copy the remaining contents of each
ByteBuffer
to prevent modifications to any provided ByteBuffer
from being reflected in the
AsyncRequestBody
.
Unlike fromByteBufferUnsafe(ByteBuffer)
,
this method respects the current read position of each buffer and reads only the remaining bytes.
byteBuffers
- ByteBuffer array to send to the service.static AsyncRequestBody fromByteBuffersUnsafe(ByteBuffer... byteBuffers)
AsyncRequestBody
from a ByteBuffer
array without copying the contents of each
ByteBuffer
. This introduces concurrency risks, allowing the caller to modify any ByteBuffer
stored in this
AsyncRequestBody
implementation.
NOTE: This method ignores the current read position of each ByteBuffer
. Use
fromRemainingByteBuffers(ByteBuffer...)
if you need it to copy only the remaining readable bytes.
As the method name implies, this is unsafe. Use fromByteBuffers(ByteBuffer...)
unless you're sure you know the
risks.
byteBuffers
- ByteBuffer array to send to the service.static AsyncRequestBody fromRemainingByteBuffersUnsafe(ByteBuffer... byteBuffers)
AsyncRequestBody
from a ByteBuffer
array without copying the contents of each
ByteBuffer
. This introduces concurrency risks, allowing the caller to modify any ByteBuffer
stored in this
AsyncRequestBody
implementation.
Unlike fromByteBuffersUnsafe(ByteBuffer...)
,
this method respects the current read position of each buffer and reads only the remaining bytes.
As the method name implies, this is unsafe. Use fromByteBuffers(ByteBuffer...)
unless you're sure you know the
risks.
byteBuffers
- ByteBuffer array to send to the service.static AsyncRequestBody fromInputStream(InputStream inputStream, Long contentLength, ExecutorService executor)
AsyncRequestBody
from an InputStream
.
An ExecutorService
is required in order to perform the blocking data reads, to prevent blocking the
non-blocking event loop threads owned by the SDK.
static BlockingInputStreamAsyncRequestBody forBlockingInputStream(Long contentLength)
BlockingInputStreamAsyncRequestBody
to use for writing an input stream to the downstream service.
Example Usage
static BlockingOutputStreamAsyncRequestBody forBlockingOutputStream(Long contentLength)
BlockingOutputStreamAsyncRequestBody
to use for writing to the downstream service as if it's an output
stream. Retries are not supported for this request body.
The caller is responsible for calling OutputStream.close()
on the
BlockingOutputStreamAsyncRequestBody.outputStream()
when writing is complete.
Example Usage
static AsyncRequestBody empty()
AsyncRequestBody
with no content.Copyright © 2023. All rights reserved.