@@ -125,7 +125,7 @@ private void putChunk(String host, int offset, int chunkSize, String context, Pr
125125 post (url , chunkBuffer , 0 , chunkSize , progress , _completionHandler , c );
126126 }
127127
128- private void makeFile (String host , CompletionHandler _completionHandler ) {
128+ private void makeFile (String host , CompletionHandler _completionHandler , UpCancellationSignal c ) {
129129 String mime = format (Locale .ENGLISH , "/mimeType/%s" , UrlSafeBase64 .encodeToString (options .mimeType ));
130130
131131 String keyStr = "" ;
@@ -145,8 +145,8 @@ private void makeFile(String host, CompletionHandler _completionHandler) {
145145 String url = format (Locale .ENGLISH , "http://%s/mkfile/%d%s%s%s" , host , size , mime , keyStr , paramStr );
146146 String bodyStr = StringUtils .join (contexts , "," );
147147 byte [] data = bodyStr .getBytes ();
148- // 不取消 makeFile 操作
149- post (url , data , 0 , data .length , null , _completionHandler , null );
148+
149+ post (url , data , 0 , data .length , null , _completionHandler , c );
150150 }
151151
152152 private void post (String url , byte [] data , int offset , int size , ProgressHandler progress ,
@@ -165,14 +165,19 @@ private int calcBlockSize(int offset) {
165165 }
166166
167167 private boolean isCancelled () {
168- return options .cancellationSignal .isCancelled ();
168+ return options .cancellationSignal != null && options . cancellationSignal .isCancelled ();
169169 }
170170
171171 private void nextTask (final int offset , final int retried , final String host ) {
172172 if (offset == size ) {
173173 CompletionHandler complete = new CompletionHandler () {
174174 @ Override
175175 public void complete (ResponseInfo info , JSONObject response ) {
176+ if (isCancelled ()){
177+ ResponseInfo i = ResponseInfo .cancelled ();
178+ completionHandler .complete (key , i , null );
179+ return ;
180+ }
176181 if (info .isOK ()) {
177182 removeRecord ();
178183 options .progressHandler .progress (key , 1.0 );
@@ -187,7 +192,7 @@ public void complete(ResponseInfo info, JSONObject response) {
187192 completionHandler .complete (key , info , response );
188193 }
189194 };
190- makeFile (host , complete );
195+ makeFile (host , complete , options . cancellationSignal );
191196 return ;
192197 }
193198
@@ -207,6 +212,11 @@ public void onProgress(int bytesWritten, int totalSize) {
207212 @ Override
208213 public void complete (ResponseInfo info , JSONObject response ) {
209214 if (!info .isOK ()) {
215+ if (isCancelled ()){
216+ ResponseInfo i = ResponseInfo .cancelled ();
217+ completionHandler .complete (key , i , null );
218+ return ;
219+ }
210220 if (info .statusCode == 701 ) {
211221 nextTask ((offset / Configuration .BLOCK_SIZE ) * Configuration .BLOCK_SIZE , retried , host );
212222 return ;
0 commit comments