@@ -69,13 +69,16 @@ public class MultipartDownloaderSubscriber implements Subscriber<AsyncResponseTr
69
69
*/
70
70
private final CompletableFuture <Void > future = new CompletableFuture <>();
71
71
72
- private final Object lock = new Object ();
73
-
74
72
/**
75
73
* The etag of the object being downloaded.
76
74
*/
77
75
private volatile String eTag ;
78
76
77
+ /**
78
+ * The Subscription lock
79
+ */
80
+ private final Object lock = new Object ();
81
+
79
82
public MultipartDownloaderSubscriber (S3AsyncClient s3 , GetObjectRequest getObjectRequest ) {
80
83
this (s3 , getObjectRequest , 0 );
81
84
}
@@ -88,7 +91,6 @@ public MultipartDownloaderSubscriber(S3AsyncClient s3, GetObjectRequest getObjec
88
91
89
92
@ Override
90
93
public void onSubscribe (Subscription s ) {
91
- log .trace (() -> "onSubscribe" );
92
94
if (this .subscription != null ) {
93
95
s .cancel ();
94
96
return ;
@@ -99,20 +101,19 @@ public void onSubscribe(Subscription s) {
99
101
100
102
@ Override
101
103
public void onNext (AsyncResponseTransformer <GetObjectResponse , GetObjectResponse > asyncResponseTransformer ) {
102
- log .trace (() -> String .format ("onNext, completed part = %d" , completedParts .get ()));
103
104
if (asyncResponseTransformer == null ) {
104
105
subscription .cancel ();
105
106
throw new NullPointerException ("onNext must not be called with null asyncResponseTransformer" );
106
107
}
107
108
108
109
int nextPartToGet = completedParts .get () + 1 ;
109
110
110
- if ( totalParts != null && nextPartToGet > totalParts ) {
111
- synchronized ( lock ) {
112
- logMulitpartComplete ( totalParts );
111
+ synchronized ( lock ) {
112
+ if ( totalParts != null && nextPartToGet > totalParts ) {
113
+ log . debug (() -> String . format ( "Completing multipart download after a total of %d parts downloaded." , totalParts ) );
113
114
subscription .cancel ();
115
+ return ;
114
116
}
115
- return ;
116
117
}
117
118
118
119
GetObjectRequest actualRequest = nextRequest (nextPartToGet );
@@ -128,10 +129,6 @@ public void onNext(AsyncResponseTransformer<GetObjectResponse, GetObjectResponse
128
129
});
129
130
}
130
131
131
- private void logMulitpartComplete (int totalParts ) {
132
- log .debug (() -> String .format ("Completing multipart download after a total of %d parts downloaded." , totalParts ));
133
- }
134
-
135
132
private void requestMoreIfNeeded (GetObjectResponse response ) {
136
133
int totalComplete = completedParts .incrementAndGet ();
137
134
MultipartDownloadUtils .multipartDownloadResumeContext (getObjectRequest )
@@ -153,14 +150,15 @@ private void requestMoreIfNeeded(GetObjectResponse response) {
153
150
if (partCount != null && totalParts == null ) {
154
151
log .debug (() -> String .format ("Total amount of parts of the object to download: %d" , partCount ));
155
152
MultipartDownloadUtils .multipartDownloadResumeContext (getObjectRequest )
156
- .ifPresent (ctx -> ctx .totalParts (partCount ));
153
+ .ifPresent (ctx -> ctx .totalParts (partCount ));
157
154
totalParts = partCount ;
158
155
}
156
+
159
157
synchronized (lock ) {
160
158
if (totalParts != null && totalParts > 1 && totalComplete < totalParts ) {
161
159
subscription .request (1 );
162
160
} else {
163
- logMulitpartComplete ( totalParts );
161
+ log . debug (() -> String . format ( "Completing multipart download after a total of %d parts downloaded." , totalParts ) );
164
162
subscription .cancel ();
165
163
}
166
164
}
0 commit comments