30
30
import software .amazon .awssdk .core .internal .async .FileAsyncResponseTransformer ;
31
31
import software .amazon .awssdk .core .internal .async .InputStreamResponseTransformer ;
32
32
import software .amazon .awssdk .core .internal .async .PublisherAsyncResponseTransformer ;
33
+ import software .amazon .awssdk .core .internal .async .SplittingTransformer ;
33
34
import software .amazon .awssdk .utils .Validate ;
34
35
35
36
/**
38
39
* <h2>Synchronization</h2>
39
40
* <p>
40
41
* All operations, including those called on the {@link org.reactivestreams.Subscriber} of the stream are guaranteed to be
41
- * synchronized externally; i.e. no two methods on this interface or on the {@link org.reactivestreams.Subscriber} will be
42
- * invoked concurrently. It is <b>not</b> guaranteed that the methods will being invoked by the same thread.
42
+ * synchronized externally; i.e. no two methods on this interface or on the {@link org.reactivestreams.Subscriber} will be invoked
43
+ * concurrently. It is <b>not</b> guaranteed that the methods will being invoked by the same thread.
43
44
* <p>
44
45
* <h2>Invocation Order</h2>
45
46
* <p>
@@ -81,11 +82,10 @@ public interface AsyncResponseTransformer<ResponseT, ResultT> {
81
82
/**
82
83
* Initial call to enable any setup required before the response is handled.
83
84
* <p>
84
- * Note that this will be called for each request attempt, up to the number of retries allowed by the configured {@link
85
- * software.amazon.awssdk.core.retry.RetryPolicy}.
85
+ * Note that this will be called for each request attempt, up to the number of retries allowed by the configured
86
+ * {@link software.amazon.awssdk.core.retry.RetryPolicy}.
86
87
* <p>
87
- * This method is guaranteed to be called before the request is executed, and before {@link #onResponse(Object)} is
88
- * signaled.
88
+ * This method is guaranteed to be called before the request is executed, and before {@link #onResponse(Object)} is signaled.
89
89
*
90
90
* @return The future holding the transformed response.
91
91
*/
@@ -106,18 +106,38 @@ public interface AsyncResponseTransformer<ResponseT, ResultT> {
106
106
void onStream (SdkPublisher <ByteBuffer > publisher );
107
107
108
108
/**
109
- * Called when an error is encountered while making the request or receiving the response.
110
- * Implementations should free up any resources in this method. This method may be called
111
- * multiple times during the lifecycle of a request if automatic retries are enabled.
109
+ * Called when an error is encountered while making the request or receiving the response. Implementations should free up any
110
+ * resources in this method. This method may be called multiple times during the lifecycle of a request if automatic retries
111
+ * are enabled.
112
112
*
113
113
* @param error Error that occurred.
114
114
*/
115
115
void exceptionOccurred (Throwable error );
116
116
117
117
/**
118
- * Creates an {@link AsyncResponseTransformer} that writes all the content to the given file. In the event of an error,
119
- * the SDK will attempt to delete the file (whatever has been written to it so far). If the file already exists, an
120
- * exception will be thrown.
118
+ * todo - javadoc
119
+ * todo - use configuration class as input instead of bufferSize
120
+ *
121
+ * @return
122
+ */
123
+ default SplitAsyncResponseTransformer <ResponseT , ResultT > split (long bufferSize ) {
124
+ CompletableFuture <ResultT > future = new CompletableFuture <>();
125
+ SdkPublisher <AsyncResponseTransformer <ResponseT , ResponseT >> transformer = SplittingTransformer
126
+ .<ResponseT , ResultT >builder ()
127
+ .upstreamResponseTransformer (this )
128
+ .maximumBufferSize (bufferSize )
129
+ .returnFuture (future )
130
+ .build ();
131
+ return SplitAsyncResponseTransformer .<ResponseT , ResultT >builder ()
132
+ .asyncResponseTransformerPublisher (transformer )
133
+ .future (future )
134
+ .build ();
135
+ }
136
+
137
+ /**
138
+ * Creates an {@link AsyncResponseTransformer} that writes all the content to the given file. In the event of an error, the
139
+ * SDK will attempt to delete the file (whatever has been written to it so far). If the file already exists, an exception will
140
+ * be thrown.
121
141
*
122
142
* @param path Path to file to write to.
123
143
* @param <ResponseT> Pojo Response type.
@@ -129,8 +149,8 @@ static <ResponseT> AsyncResponseTransformer<ResponseT, ResponseT> toFile(Path pa
129
149
}
130
150
131
151
/**
132
- * Creates an {@link AsyncResponseTransformer} that writes all the content to the given file with the specified {@link
133
- * FileTransformerConfiguration}.
152
+ * Creates an {@link AsyncResponseTransformer} that writes all the content to the given file with the specified
153
+ * {@link FileTransformerConfiguration}.
134
154
*
135
155
* @param path Path to file to write to.
136
156
* @param config configuration for the transformer
@@ -143,8 +163,8 @@ static <ResponseT> AsyncResponseTransformer<ResponseT, ResponseT> toFile(Path pa
143
163
}
144
164
145
165
/**
146
- * This is a convenience method that creates an instance of the {@link FileTransformerConfiguration} builder,
147
- * avoiding the need to create one manually via {@link FileTransformerConfiguration#builder()}.
166
+ * This is a convenience method that creates an instance of the {@link FileTransformerConfiguration} builder, avoiding the
167
+ * need to create one manually via {@link FileTransformerConfiguration#builder()}.
148
168
*
149
169
* @see #toFile(Path, FileTransformerConfiguration)
150
170
*/
@@ -155,9 +175,9 @@ static <ResponseT> AsyncResponseTransformer<ResponseT, ResponseT> toFile(
155
175
}
156
176
157
177
/**
158
- * Creates an {@link AsyncResponseTransformer} that writes all the content to the given file. In the event of an error,
159
- * the SDK will attempt to delete the file (whatever has been written to it so far). If the file already exists, an
160
- * exception will be thrown.
178
+ * Creates an {@link AsyncResponseTransformer} that writes all the content to the given file. In the event of an error, the
179
+ * SDK will attempt to delete the file (whatever has been written to it so far). If the file already exists, an exception will
180
+ * be thrown.
161
181
*
162
182
* @param file File to write to.
163
183
* @param <ResponseT> Pojo Response type.
@@ -168,8 +188,8 @@ static <ResponseT> AsyncResponseTransformer<ResponseT, ResponseT> toFile(File fi
168
188
}
169
189
170
190
/**
171
- * Creates an {@link AsyncResponseTransformer} that writes all the content to the given file with the specified {@link
172
- * FileTransformerConfiguration}.
191
+ * Creates an {@link AsyncResponseTransformer} that writes all the content to the given file with the specified
192
+ * {@link FileTransformerConfiguration}.
173
193
*
174
194
* @param file File to write to.
175
195
* @param config configuration for the transformer
@@ -182,8 +202,8 @@ static <ResponseT> AsyncResponseTransformer<ResponseT, ResponseT> toFile(File fi
182
202
}
183
203
184
204
/**
185
- * This is a convenience method that creates an instance of the {@link FileTransformerConfiguration} builder,
186
- * avoiding the need to create one manually via {@link FileTransformerConfiguration#builder()}.
205
+ * This is a convenience method that creates an instance of the {@link FileTransformerConfiguration} builder, avoiding the
206
+ * need to create one manually via {@link FileTransformerConfiguration#builder()}.
187
207
*
188
208
* @see #toFile(File, FileTransformerConfiguration)
189
209
*/
@@ -237,16 +257,14 @@ static <ResponseT extends SdkResponse> AsyncResponseTransformer<ResponseT, Respo
237
257
}
238
258
239
259
/**
240
- * Creates an {@link AsyncResponseTransformer} that allows reading the response body content as an
241
- * {@link InputStream}.
260
+ * Creates an {@link AsyncResponseTransformer} that allows reading the response body content as an {@link InputStream}.
242
261
* <p>
243
- * When this transformer is used with an async client, the {@link CompletableFuture} that the client returns will
244
- * be completed once the {@link SdkResponse} is available and the response body <i>begins</i> streaming. This
245
- * behavior differs from some other transformers, like {@link #toFile(Path)} and {@link #toBytes()}, which only
246
- * have their {@link CompletableFuture} completed after the entire response body has finished streaming.
262
+ * When this transformer is used with an async client, the {@link CompletableFuture} that the client returns will be completed
263
+ * once the {@link SdkResponse} is available and the response body <i>begins</i> streaming. This behavior differs from some
264
+ * other transformers, like {@link #toFile(Path)} and {@link #toBytes()}, which only have their {@link CompletableFuture}
265
+ * completed after the entire response body has finished streaming.
247
266
* <p>
248
- * You are responsible for performing blocking reads from this input stream and closing the stream when you are
249
- * finished.
267
+ * You are responsible for performing blocking reads from this input stream and closing the stream when you are finished.
250
268
* <p>
251
269
* Example usage:
252
270
* <pre>
@@ -260,7 +278,7 @@ static <ResponseT extends SdkResponse> AsyncResponseTransformer<ResponseT, Respo
260
278
* </pre>
261
279
*/
262
280
static <ResponseT extends SdkResponse >
263
- AsyncResponseTransformer <ResponseT , ResponseInputStream <ResponseT >> toBlockingInputStream () {
281
+ AsyncResponseTransformer <ResponseT , ResponseInputStream <ResponseT >> toBlockingInputStream () {
264
282
return new InputStreamResponseTransformer <>();
265
283
}
266
284
}
0 commit comments