Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2015-2015 Amazon Technologies, Inc.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at:
   *
   *    http://aws.amazon.com/apache2.0
   *
  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  * OR CONDITIONS OF ANY KIND, either express or implied. See the
  * License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.amazonaws.services.s3.transfer;
 
 import java.io.File;
 
 
 
 
 final class DownloadCallable implements Callable<File> {
     private static final Log log = LogFactory.getLog(DownloadCallable.class);
 
     private final AmazonS3 s3;
     private final CountDownLatch latch;
     private final GetObjectRequest req;
     private final boolean resumeExistingDownload;
     private final DownloadImpl download;
     private final File dstfile;
     private final long origStartingByte;
     
     private long expectedFileLength;
 
             GetObjectRequest req,
             boolean resumeExistingDownload,
             DownloadImpl downloadFile dstfilelong origStartingByte,
             long expectedFileLength
     {
         if (s3 == null || latch == null || req == null || dstfile == null || download == null)
             throw new IllegalArgumentException();
         this. = s3;
         this. = latch;
         this. = req;
         this. = resumeExistingDownload;
         this. = download;
         this. = dstfile;
         this. = origStartingByte;
         this. = expectedFileLength;
     }

    
This method must return a non-null object, or else the existing implementation in com.amazonaws.services.s3.transfer.internal.AbstractTransfer.waitForCompletion() would block forever.

Returns:
the downloaded file
 
     @Override
     public File call() throws Exception {
         try {
             .await();
             .setState(.);
             S3Object s3Object = retryableDownloadS3ObjectToFile(,
                 new DownloadTaskImpl(), 
                 );
 
             if (s3Object == null) {
                 .setState(.);
                 .setMonitor(new DownloadMonitor(null));
             } else {
                 .setState(.);
             }
             return ;
         } catch (Throwable t) {
             // Downloads aren't allowed to move from canceled to failed
             if (.getState() != .) {
                 .setState(.);
             }
            if (t instanceof Exception)
                throw (Exceptiont;
            else
                throw (Errort;
        }
    }

    
This method is called only if it is a resumed download. Adjust the range of the get request, and the expected (ie current) file length of the destination file to append to.
    private void adjustRequest(GetObjectRequest req) {
        long[] range = req.getRange();
        long lastByte = range[1];
        long totalBytesToDownload = lastByte - this. + 1;
        if (.exists()) {
            if (!FileLocks.lock()) {
                throw new FileLockException("Fail to lock " + 
                        + " for range adjustment");
            }
            try {
                 = .length();
                long startingByte = this. + ;
                .info("Adjusting request range from " + Arrays.toString(range)
                        + " to "
                        + Arrays.toString(new long[] { startingBytelastByte })
                        + " for file " + );
                req.setRange(startingBytelastByte);
                totalBytesToDownload = lastByte - startingByte + 1;
            } finally {
                FileLocks.unlock();
            }
        }
        if (totalBytesToDownload < 0) {
            throw new IllegalArgumentException(
                "Unable to determine the range for download operation. lastByte="
                        + lastByte + ", origStartingByte=" + 
                        + ", expectedFileLength=" + 
                        + ", totalBytesToDownload=" + totalBytesToDownload);
        }
    }
            RetryableS3DownloadTask retryableS3DownloadTaskboolean appendData) {
        boolean hasRetried = false;
        S3Object s3Object;
        for (;;) {
            if ( && hasRetried) {
                // Need to adjust the get range or else we risk corrupting the downloaded file
                adjustRequest();
            }
            s3Object = retryableS3DownloadTask.getS3ObjectStream();
            if (s3Object == null)
                return null;
            try {
                if ( &&  && !hasRetried) {
                    throw new AmazonClientException("testing");
                }
                ServiceUtils.downloadToFile(s3Objectfile,
                        retryableS3DownloadTask.needIntegrityCheck(),
                        appendData);
                return s3Object;
            } catch (AmazonClientException ace) {
                if (!ace.isRetryable())
                    throw ace;
                // Determine whether an immediate retry is needed according to the captured AmazonClientException.
                // (There are three cases when downloadObjectToFile() throws AmazonClientException:
                //      1) SocketException or SSLProtocolException when writing to disk (e.g. when user aborts the download)
                //      2) Other IOException when writing to disk
                //      3) MD5 hashes don't match
                // The current code will retry the download only when case 2) or 3) happens.
                if (ace.getCause() instanceof SocketException || ace.getCause() instanceof SSLProtocolException) {
                    throw ace;
                } else {
                    if (hasRetried)
                        throw ace;
                    else {
                        .info("Retry the download of object " + s3Object.getKey() + " (bucket " + s3Object.getBucketName() + ")"ace);
                        hasRetried = true;
                    }
                }
            } finally {
                s3Object.getObjectContent().abort();
            }
        }
    }
    private static boolean testing;
    
Used for testing purpose only.
    static void setTesting(boolean b) {
         = b;
    }
New to GrepCode? Check out our FAQ X