Interface AsyncRequestBody
- All Superinterfaces:
org.reactivestreams.Publisher<ByteBuffer>,SdkPublisher<ByteBuffer>
- All Known Implementing Classes:
AsyncRequestBodyListener.NotifyingAsyncRequestBody,BlockingInputStreamAsyncRequestBody,BlockingOutputStreamAsyncRequestBody,ByteBuffersAsyncRequestBody,ChecksumCalculatingAsyncRequestBody,ChecksumCalculatingAsyncRequestBody,CompressionAsyncRequestBody,CrtContentLengthOnlyAsyncFileRequestBody,FileAsyncRequestBody,InputStreamWithExecutorAsyncRequestBody
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.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault Stringstatic AsyncRequestBodyempty()Creates anAsyncRequestBodywith no content.forBlockingInputStream(Long contentLength) Creates aBlockingInputStreamAsyncRequestBodyto use for writing an input stream to the downstream service.forBlockingOutputStream(Long contentLength) Creates aBlockingOutputStreamAsyncRequestBodyto use for writing to the downstream service as if it's an output stream.static AsyncRequestBodyfromByteBuffer(ByteBuffer byteBuffer) Creates anAsyncRequestBodyfrom aByteBuffer.static AsyncRequestBodyfromByteBuffers(ByteBuffer... byteBuffers) Creates anAsyncRequestBodyfrom aByteBufferarray.static AsyncRequestBodyfromByteBuffersUnsafe(ByteBuffer... byteBuffers) Creates anAsyncRequestBodyfrom aByteBufferarray without copying the contents of eachByteBuffer.static AsyncRequestBodyfromByteBufferUnsafe(ByteBuffer byteBuffer) static AsyncRequestBodyfromBytes(byte[] bytes) Creates anAsyncRequestBodyfrom a byte array.static AsyncRequestBodyfromBytesUnsafe(byte[] bytes) Creates anAsyncRequestBodyfrom a byte array without copying the contents of the byte array.static AsyncRequestBodyCreates anAsyncRequestBodythat produces data from the contents of a file.static AsyncRequestBodyCreates anAsyncRequestBodythat produces data from the contents of a file.static AsyncRequestBodyfromFile(Consumer<FileRequestBodyConfiguration.Builder> configuration) Creates anAsyncRequestBodythat produces data from the contents of a file.static AsyncRequestBodyfromFile(FileRequestBodyConfiguration configuration) Creates anAsyncRequestBodythat produces data from the contents of a file.static AsyncRequestBodyfromInputStream(InputStream inputStream, Long contentLength, ExecutorService executor) Creates anAsyncRequestBodyfrom anInputStream.static AsyncRequestBodyThis is a convenience method that passes an instance of theAsyncRequestBodyFromInputStreamConfigurationbuilder, avoiding the need to create one manually viaAsyncRequestBodyFromInputStreamConfiguration.builder().static AsyncRequestBodyfromInputStream(AsyncRequestBodyFromInputStreamConfiguration configuration) Creates anAsyncRequestBodyfrom anInputStreamwith the providedAsyncRequestBodySplitConfiguration.static AsyncRequestBodyfromPublisher(org.reactivestreams.Publisher<ByteBuffer> publisher) Creates anAsyncRequestBodythe produces data from the input ByteBuffer publisher.static AsyncRequestBodyfromRemainingByteBuffer(ByteBuffer byteBuffer) Creates anAsyncRequestBodyfrom the remaining readable bytes from aByteBuffer.static AsyncRequestBodyfromRemainingByteBuffers(ByteBuffer... byteBuffers) Creates anAsyncRequestBodyfrom aByteBufferarray.static AsyncRequestBodyfromRemainingByteBuffersUnsafe(ByteBuffer... byteBuffers) Creates anAsyncRequestBodyfrom aByteBufferarray without copying the contents of eachByteBuffer.static AsyncRequestBodyfromRemainingByteBufferUnsafe(ByteBuffer byteBuffer) static AsyncRequestBodyfromString(String string) Creates anAsyncRequestBodythat uses a single string as data with UTF_8 encoding.static AsyncRequestBodyfromString(String string, Charset cs) Creates anAsyncRequestBodythat uses a single string as data.default SdkPublisher<AsyncRequestBody> split(Consumer<AsyncRequestBodySplitConfiguration.Builder> splitConfiguration) This is a convenience method that passes an instance of theAsyncRequestBodySplitConfigurationbuilder, avoiding the need to create one manually viaAsyncRequestBodySplitConfiguration.builder().default SdkPublisher<AsyncRequestBody> split(AsyncRequestBodySplitConfiguration splitConfiguration) Converts thisAsyncRequestBodyto a publisher ofAsyncRequestBodys, each of which publishes a specific portion of the original data, based on the providedAsyncRequestBodySplitConfiguration.Methods inherited from interface org.reactivestreams.Publisher
subscribeMethods inherited from interface software.amazon.awssdk.core.async.SdkPublisher
addTrailingData, buffer, doAfterOnCancel, doAfterOnComplete, doAfterOnError, filter, filter, flatMapIterable, limit, map, subscribe
-
Method Details
-
contentLength
-
contentType
- Returns:
- The content type of the data being produced.
-
fromPublisher
Creates anAsyncRequestBodythe produces data from the input ByteBuffer publisher. The data is delivered when the publisher publishes the data.- Parameters:
publisher- Publisher of source data- Returns:
- Implementation of
AsyncRequestBodythat produces data send by the publisher
-
fromFile
Creates anAsyncRequestBodythat produces data from the contents of a file. SeeFileAsyncRequestBody.builder()to create a customized body implementation.- Parameters:
path- Path to file to read from.- Returns:
- Implementation of
AsyncRequestBodythat reads data from the specified file. - See Also:
-
fromFile
Creates anAsyncRequestBodythat produces data from the contents of a file. SeefromFile(FileRequestBodyConfiguration)to create a customized body implementation.- Parameters:
file- The file to read from.- Returns:
- Implementation of
AsyncRequestBodythat reads data from the specified file.
-
fromFile
Creates anAsyncRequestBodythat produces data from the contents of a file.- Parameters:
configuration- configuration for how the SDK should read the file- Returns:
- Implementation of
AsyncRequestBodythat reads data from the specified file.
-
fromFile
Creates anAsyncRequestBodythat produces data from the contents of a file.This is a convenience method that creates an instance of the
FileRequestBodyConfigurationbuilder, avoiding the need to create one manually viaFileRequestBodyConfiguration.builder().- Parameters:
configuration- configuration for how the SDK should read the file- Returns:
- Implementation of
AsyncRequestBodythat reads data from the specified file.
-
fromString
Creates anAsyncRequestBodythat uses a single string as data.- Parameters:
string- The string to provide.cs- TheCharsetto use.- Returns:
- Implementation of
AsyncRequestBodythat uses the specified string. - See Also:
-
fromString
Creates anAsyncRequestBodythat uses a single string as data with UTF_8 encoding.- Parameters:
string- The string to send.- Returns:
- Implementation of
AsyncRequestBodythat uses the specified string. - See Also:
-
fromBytes
Creates anAsyncRequestBodyfrom a byte array. This will copy the contents of the byte array to prevent modifications to the provided byte array from being reflected in theAsyncRequestBody.- Parameters:
bytes- The bytes to send to the service.- Returns:
- AsyncRequestBody instance.
-
fromBytesUnsafe
Creates anAsyncRequestBodyfrom 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 thisAsyncRequestBodyimplementation AND (2) any users offromBytesUnsafe(byte[])to modify the byte array passed into thisAsyncRequestBodyimplementation.As the method name implies, this is unsafe. Use
fromBytes(byte[])unless you're sure you know the risks.- Parameters:
bytes- The bytes to send to the service.- Returns:
- AsyncRequestBody instance.
-
fromByteBuffer
Creates anAsyncRequestBodyfrom aByteBuffer. This will copy the contents of theByteBufferto prevent modifications to the providedByteBufferfrom being reflected in theAsyncRequestBody.NOTE: This method ignores the current read position. Use
fromRemainingByteBuffer(ByteBuffer)if you need it to copy only the remaining readable bytes.- Parameters:
byteBuffer- ByteBuffer to send to the service.- Returns:
- AsyncRequestBody instance.
-
fromRemainingByteBuffer
Creates anAsyncRequestBodyfrom the remaining readable bytes from aByteBuffer. This will copy the remaining contents of theByteBufferto prevent modifications to the providedByteBufferfrom being reflected in theAsyncRequestBody.Unlike
fromByteBuffer(ByteBuffer), this method respects the current read position of the buffer and reads only the remaining bytes.- Parameters:
byteBuffer- ByteBuffer to send to the service.- Returns:
- AsyncRequestBody instance.
-
fromByteBufferUnsafe
Creates anAsyncRequestBodyfrom aByteBufferwithout copying the contents of theByteBuffer. This introduces concurrency risks, allowing the caller to modify theByteBufferstored in thisAsyncRequestBodyimplementation.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.- Parameters:
byteBuffer- ByteBuffer to send to the service.- Returns:
- AsyncRequestBody instance.
-
fromRemainingByteBufferUnsafe
Creates anAsyncRequestBodyfrom aByteBufferwithout copying the contents of theByteBuffer. This introduces concurrency risks, allowing the caller to modify theByteBufferstored in thisAsyncRequestBodyimplementation.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.- Parameters:
byteBuffer- ByteBuffer to send to the service.- Returns:
- AsyncRequestBody instance.
-
fromByteBuffers
Creates anAsyncRequestBodyfrom aByteBufferarray. This will copy the contents of eachByteBufferto prevent modifications to any providedByteBufferfrom being reflected in theAsyncRequestBody.NOTE: This method ignores the current read position of each
ByteBuffer. UsefromRemainingByteBuffers(ByteBuffer...)if you need it to copy only the remaining readable bytes.- Parameters:
byteBuffers- ByteBuffer array to send to the service.- Returns:
- AsyncRequestBody instance.
-
fromRemainingByteBuffers
Creates anAsyncRequestBodyfrom aByteBufferarray. This will copy the remaining contents of eachByteBufferto prevent modifications to any providedByteBufferfrom being reflected in theAsyncRequestBody.Unlike
fromByteBufferUnsafe(ByteBuffer), this method respects the current read position of each buffer and reads only the remaining bytes.- Parameters:
byteBuffers- ByteBuffer array to send to the service.- Returns:
- AsyncRequestBody instance.
-
fromByteBuffersUnsafe
Creates anAsyncRequestBodyfrom aByteBufferarray without copying the contents of eachByteBuffer. This introduces concurrency risks, allowing the caller to modify anyByteBufferstored in thisAsyncRequestBodyimplementation.NOTE: This method ignores the current read position of each
ByteBuffer. UsefromRemainingByteBuffers(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.- Parameters:
byteBuffers- ByteBuffer array to send to the service.- Returns:
- AsyncRequestBody instance.
-
fromRemainingByteBuffersUnsafe
Creates anAsyncRequestBodyfrom aByteBufferarray without copying the contents of eachByteBuffer. This introduces concurrency risks, allowing the caller to modify anyByteBufferstored in thisAsyncRequestBodyimplementation.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.- Parameters:
byteBuffers- ByteBuffer array to send to the service.- Returns:
- AsyncRequestBody instance.
-
fromInputStream
static AsyncRequestBody fromInputStream(InputStream inputStream, Long contentLength, ExecutorService executor) Creates anAsyncRequestBodyfrom anInputStream.An
ExecutorServiceis required in order to perform the blocking data reads, to prevent blocking the non-blocking event loop threads owned by the SDK. -
fromInputStream
Creates anAsyncRequestBodyfrom anInputStreamwith the providedAsyncRequestBodySplitConfiguration. -
fromInputStream
static AsyncRequestBody fromInputStream(Consumer<AsyncRequestBodyFromInputStreamConfiguration.Builder> configuration) This is a convenience method that passes an instance of theAsyncRequestBodyFromInputStreamConfigurationbuilder, avoiding the need to create one manually viaAsyncRequestBodyFromInputStreamConfiguration.builder().- See Also:
-
forBlockingInputStream
Creates aBlockingInputStreamAsyncRequestBodyto use for writing an input stream to the downstream service.By default, it will time out if streaming hasn't started within 10 seconds, and use application/octet-stream as content type. You can configure it via
BlockingInputStreamAsyncRequestBody.builder()Example Usage
S3AsyncClient s3 = S3AsyncClient.create(); // Use one client for your whole application! byte[] dataToSend = "Hello".getBytes(StandardCharsets.UTF_8); InputStream streamToSend = new ByteArrayInputStream(); long streamToSendLength = dataToSend.length(); // Start the operation BlockingInputStreamAsyncRequestBody body = AsyncRequestBody.forBlockingInputStream(streamToSendLength); CompletableFuture<PutObjectResponse> responseFuture = s3.putObject(r -> r.bucket("bucketName").key("key"), body); // Write the input stream to the running operation body.writeInputStream(streamToSend); // Wait for the service to respond. PutObjectResponse response = responseFuture.join(); -
forBlockingOutputStream
Creates aBlockingOutputStreamAsyncRequestBodyto 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 theBlockingOutputStreamAsyncRequestBody.outputStream()when writing is complete.By default, it will time out if streaming hasn't started within 10 seconds, and you can configure the timeout via
BlockingOutputStreamAsyncRequestBody.builder()Example Usage
S3AsyncClient s3 = S3AsyncClient.create(); // Use one client for your whole application! byte[] dataToSend = "Hello".getBytes(StandardCharsets.UTF_8); long lengthOfDataToSend = dataToSend.length(); // Start the operation BlockingInputStreamAsyncRequestBody body = AsyncRequestBody.forBlockingOutputStream(lengthOfDataToSend); CompletableFuture<PutObjectResponse> responseFuture = s3.putObject(r -> r.bucket("bucketName").key("key"), body); // Write the input stream to the running operation try (CancellableOutputStream outputStream = body.outputStream()) { outputStream.write(dataToSend); } // Wait for the service to respond. PutObjectResponse response = responseFuture.join();- See Also:
-
empty
Creates anAsyncRequestBodywith no content.- Returns:
- AsyncRequestBody instance.
-
split
Converts thisAsyncRequestBodyto a publisher ofAsyncRequestBodys, each of which publishes a specific portion of the original data, based on the providedAsyncRequestBodySplitConfiguration. The default chunk size is 2MB and the default buffer size is 8MB.By default, if content length of this
AsyncRequestBodyis present, each dividedAsyncRequestBodyis delivered to the subscriber right after it's initialized. On the other hand, if content length is null, it is sent after the entire content for that chunk is buffered. In this case, the configuredmaxMemoryUsageInBytesmust be larger than or equal tochunkSizeInBytes. Note that this behavior may be different if a specific implementation of this interface overrides this method.- See Also:
-
split
default SdkPublisher<AsyncRequestBody> split(Consumer<AsyncRequestBodySplitConfiguration.Builder> splitConfiguration) This is a convenience method that passes an instance of theAsyncRequestBodySplitConfigurationbuilder, avoiding the need to create one manually viaAsyncRequestBodySplitConfiguration.builder().- See Also:
-