|
17 | 17 |
|
18 | 18 | import java.io.File;
|
19 | 19 | import java.time.Instant;
|
20 |
| -import java.util.Objects; |
21 | 20 | import java.util.concurrent.CompletableFuture;
|
| 21 | +import java.util.function.Supplier; |
22 | 22 | import software.amazon.awssdk.annotations.SdkInternalApi;
|
23 |
| -import software.amazon.awssdk.core.exception.SdkClientException; |
24 | 23 | import software.amazon.awssdk.services.s3.model.GetObjectResponse;
|
25 |
| -import software.amazon.awssdk.transfer.s3.internal.progress.DefaultTransferProgress; |
26 |
| -import software.amazon.awssdk.transfer.s3.internal.progress.DefaultTransferProgressSnapshot; |
27 | 24 | import software.amazon.awssdk.transfer.s3.model.CompletedFileDownload;
|
28 | 25 | import software.amazon.awssdk.transfer.s3.model.DownloadFileRequest;
|
29 | 26 | import software.amazon.awssdk.transfer.s3.model.FileDownload;
|
30 | 27 | import software.amazon.awssdk.transfer.s3.model.ResumableFileDownload;
|
31 | 28 | import software.amazon.awssdk.transfer.s3.progress.TransferProgress;
|
32 | 29 | import software.amazon.awssdk.transfer.s3.progress.TransferProgressSnapshot;
|
33 |
| -import software.amazon.awssdk.utils.Logger; |
| 30 | +import software.amazon.awssdk.utils.Lazy; |
34 | 31 | import software.amazon.awssdk.utils.ToString;
|
35 | 32 | import software.amazon.awssdk.utils.Validate;
|
36 | 33 |
|
37 | 34 | @SdkInternalApi
|
38 | 35 | public final class DefaultFileDownload implements FileDownload {
|
39 |
| - private static final Logger log = Logger.loggerFor(FileDownload.class); |
40 | 36 | private final CompletableFuture<CompletedFileDownload> completionFuture;
|
41 |
| - private final CompletableFuture<TransferProgress> progressFuture; |
42 |
| - private final CompletableFuture<DownloadFileRequest> requestFuture; |
43 |
| - private volatile ResumableFileDownload resumableFileDownload; |
44 |
| - private final Object lock = new Object(); |
| 37 | + private final Lazy<ResumableFileDownload> resumableFileDownload; |
| 38 | + private final TransferProgress progress; |
| 39 | + private final Supplier<DownloadFileRequest> requestSupplier; |
| 40 | + private final ResumableFileDownload resumedDownload; |
45 | 41 |
|
46 | 42 | public DefaultFileDownload(CompletableFuture<CompletedFileDownload> completedFileDownloadFuture,
|
47 |
| - CompletableFuture<TransferProgress> progressFuture, |
48 |
| - CompletableFuture<DownloadFileRequest> requestFuture) { |
| 43 | + TransferProgress progress, |
| 44 | + Supplier<DownloadFileRequest> requestSupplier, |
| 45 | + ResumableFileDownload resumedDownload) { |
49 | 46 | this.completionFuture = Validate.paramNotNull(completedFileDownloadFuture, "completedFileDownloadFuture");
|
50 |
| - this.progressFuture = Validate.paramNotNull(progressFuture, "progressFuture"); |
51 |
| - this.requestFuture = Validate.paramNotNull(requestFuture, "requestFuture"); |
| 47 | + this.progress = Validate.paramNotNull(progress, "progress"); |
| 48 | + this.requestSupplier = Validate.paramNotNull(requestSupplier, "requestSupplier"); |
| 49 | + this.resumableFileDownload = new Lazy<>(this::doPause); |
| 50 | + this.resumedDownload = resumedDownload; |
52 | 51 | }
|
53 | 52 |
|
54 | 53 | @Override
|
55 | 54 | public TransferProgress progress() {
|
56 |
| - return progressFuture.isDone() ? progressFuture.join() : |
57 |
| - new DefaultTransferProgress(DefaultTransferProgressSnapshot.builder().build()); |
| 55 | + return progress; |
58 | 56 | }
|
59 | 57 |
|
60 | 58 | @Override
|
61 | 59 | public ResumableFileDownload pause() {
|
62 |
| - log.debug(() -> "Start to pause "); |
63 |
| - if (resumableFileDownload == null) { |
64 |
| - synchronized (lock) { |
65 |
| - if (resumableFileDownload == null) { |
66 |
| - completionFuture.cancel(true); |
67 |
| - |
68 |
| - if (!requestFuture.isDone() || !progressFuture.isDone()) { |
69 |
| - throw SdkClientException.create("DownloadFileRequest is unknown, not able to pause. This is likely " |
70 |
| - + "because you are trying to pause a resumed download request that " |
71 |
| - + "hasn't started yet. Please try later"); |
72 |
| - } |
73 |
| - DownloadFileRequest request = requestFuture.join(); |
74 |
| - TransferProgress progress = progressFuture.join(); |
75 |
| - |
76 |
| - Instant s3objectLastModified = null; |
77 |
| - Long totalBytesTransferred = null; |
78 |
| - TransferProgressSnapshot snapshot = progress.snapshot(); |
79 |
| - if (snapshot.sdkResponse().isPresent() && snapshot.sdkResponse().get() instanceof GetObjectResponse) { |
80 |
| - GetObjectResponse getObjectResponse = (GetObjectResponse) snapshot.sdkResponse().get(); |
81 |
| - s3objectLastModified = getObjectResponse.lastModified(); |
82 |
| - totalBytesTransferred = getObjectResponse.contentLength(); |
83 |
| - } |
84 |
| - File destination = request.destination().toFile(); |
85 |
| - long length = destination.length(); |
86 |
| - Instant fileLastModified = Instant.ofEpochMilli(destination.lastModified()); |
87 |
| - resumableFileDownload = ResumableFileDownload.builder() |
88 |
| - .downloadFileRequest(request) |
89 |
| - .s3ObjectLastModified(s3objectLastModified) |
90 |
| - .fileLastModified(fileLastModified) |
91 |
| - .bytesTransferred(length) |
92 |
| - .totalSizeInBytes(totalBytesTransferred) |
93 |
| - .build(); |
94 |
| - } |
95 |
| - |
96 |
| - } |
97 |
| - } |
98 |
| - return resumableFileDownload; |
99 |
| - } |
100 |
| - |
101 |
| - @Override |
102 |
| - public CompletableFuture<CompletedFileDownload> completionFuture() { |
103 |
| - return completionFuture; |
| 60 | + return resumableFileDownload.getValue(); |
104 | 61 | }
|
105 | 62 |
|
106 |
| - @Override |
107 |
| - public boolean equals(Object o) { |
108 |
| - if (this == o) { |
109 |
| - return true; |
110 |
| - } |
111 |
| - if (o == null || getClass() != o.getClass()) { |
112 |
| - return false; |
113 |
| - } |
114 |
| - |
115 |
| - DefaultFileDownload that = (DefaultFileDownload) o; |
| 63 | + private ResumableFileDownload doPause() { |
| 64 | + completionFuture.cancel(true); |
116 | 65 |
|
117 |
| - if (!Objects.equals(completionFuture, that.completionFuture)) { |
118 |
| - return false; |
119 |
| - } |
| 66 | + Instant s3objectLastModified = null; |
| 67 | + Long totalSizeInBytes = null; |
| 68 | + TransferProgressSnapshot snapshot = progress.snapshot(); |
120 | 69 |
|
121 |
| - if (!Objects.equals(requestFuture, that.requestFuture)) { |
122 |
| - return false; |
| 70 | + if (snapshot.sdkResponse().isPresent() && snapshot.sdkResponse().get() instanceof GetObjectResponse) { |
| 71 | + GetObjectResponse getObjectResponse = (GetObjectResponse) snapshot.sdkResponse().get(); |
| 72 | + s3objectLastModified = getObjectResponse.lastModified(); |
| 73 | + totalSizeInBytes = getObjectResponse.contentLength(); |
| 74 | + } else if (resumedDownload != null) { |
| 75 | + s3objectLastModified = resumedDownload.s3ObjectLastModified().orElse(null); |
| 76 | + totalSizeInBytes = resumedDownload.totalSizeInBytes().orElse(null); |
123 | 77 | }
|
124 | 78 |
|
125 |
| - return Objects.equals(progressFuture, that.progressFuture); |
| 79 | + DownloadFileRequest request = requestSupplier.get(); |
| 80 | + File destination = request.destination().toFile(); |
| 81 | + long length = destination.length(); |
| 82 | + Instant fileLastModified = Instant.ofEpochMilli(destination.lastModified()); |
| 83 | + return ResumableFileDownload.builder() |
| 84 | + .downloadFileRequest(request) |
| 85 | + .s3ObjectLastModified(s3objectLastModified) |
| 86 | + .fileLastModified(fileLastModified) |
| 87 | + .bytesTransferred(length) |
| 88 | + .totalSizeInBytes(totalSizeInBytes) |
| 89 | + .build(); |
126 | 90 | }
|
127 | 91 |
|
128 | 92 | @Override
|
129 |
| - public int hashCode() { |
130 |
| - int result = completionFuture != null ? completionFuture.hashCode() : 0; |
131 |
| - result = 31 * result + (requestFuture != null ? requestFuture.hashCode() : 0); |
132 |
| - result = 31 * result + (progressFuture != null ? progressFuture.hashCode() : 0); |
133 |
| - return result; |
| 93 | + public CompletableFuture<CompletedFileDownload> completionFuture() { |
| 94 | + return completionFuture; |
134 | 95 | }
|
135 | 96 |
|
136 | 97 | @Override
|
137 | 98 | public String toString() {
|
138 | 99 | return ToString.builder("DefaultFileDownload")
|
139 | 100 | .add("completionFuture", completionFuture)
|
140 |
| - .add("progress", progressFuture) |
141 |
| - .add("request", requestFuture) |
| 101 | + .add("progress", progress) |
| 102 | + .add("request", requestSupplier.get()) |
142 | 103 | .build();
|
143 | 104 | }
|
144 | 105 | }
|
0 commit comments