• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.core.internal.async;
17 
18 import java.nio.ByteBuffer;
19 import java.util.concurrent.CompletableFuture;
20 import software.amazon.awssdk.annotations.SdkInternalApi;
21 import software.amazon.awssdk.core.ResponseInputStream;
22 import software.amazon.awssdk.core.SdkResponse;
23 import software.amazon.awssdk.core.async.AsyncResponseTransformer;
24 import software.amazon.awssdk.core.async.SdkPublisher;
25 import software.amazon.awssdk.utils.async.InputStreamSubscriber;
26 
27 /**
28  * A {@link AsyncResponseTransformer} that allows performing blocking reads on the response data.
29  * <p>
30  * Created with {@link AsyncResponseTransformer#toBlockingInputStream()}.
31  */
32 @SdkInternalApi
33 public class InputStreamResponseTransformer<ResponseT extends SdkResponse>
34     implements AsyncResponseTransformer<ResponseT, ResponseInputStream<ResponseT>> {
35 
36     private volatile CompletableFuture<ResponseInputStream<ResponseT>> future;
37     private volatile ResponseT response;
38 
39     @Override
prepare()40     public CompletableFuture<ResponseInputStream<ResponseT>> prepare() {
41         CompletableFuture<ResponseInputStream<ResponseT>> result = new CompletableFuture<>();
42         this.future = result;
43         return result;
44     }
45 
46     @Override
onResponse(ResponseT response)47     public void onResponse(ResponseT response) {
48         this.response = response;
49     }
50 
51     @Override
onStream(SdkPublisher<ByteBuffer> publisher)52     public void onStream(SdkPublisher<ByteBuffer> publisher) {
53         InputStreamSubscriber inputStreamSubscriber = new InputStreamSubscriber();
54         publisher.subscribe(inputStreamSubscriber);
55         future.complete(new ResponseInputStream<>(response, inputStreamSubscriber));
56     }
57 
58     @Override
exceptionOccurred(Throwable error)59     public void exceptionOccurred(Throwable error) {
60         future.completeExceptionally(error);
61     }
62 }
63