Example usage for com.amazonaws.services.s3.transfer.internal DownloadImpl setMonitor

List of usage examples for com.amazonaws.services.s3.transfer.internal DownloadImpl setMonitor

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.transfer.internal DownloadImpl setMonitor.

Prototype

public void setMonitor(TransferMonitor monitor) 

Source Link

Document

Sets the monitor used to poll for transfer completion.

Usage

From source file:com.davidsoergel.s3napback.StreamingTransferManager.java

License:Apache License

private Download download(final GetObjectRequest getObjectRequest, final BufferedOutputStream os,
        final TransferStateChangeListener stateListener) {

    appendUserAgent(getObjectRequest, USER_AGENT);

    String description = "Downloading from " + getObjectRequest.getBucketName() + "/"
            + getObjectRequest.getKey();

    // Add our own transfer progress listener
    TransferProgressImpl transferProgress = new TransferProgressImpl();
    ProgressListenerChain listenerChain = new ProgressListenerChain(
            new TransferProgressUpdatingListener(transferProgress), getObjectRequest.getProgressListener());
    getObjectRequest.setProgressListener(listenerChain);

    final S3Object s3Object = s3.getObject(getObjectRequest);
    final DownloadImpl download = new DownloadImpl(description, transferProgress, listenerChain, s3Object,
            stateListener);//  ww w . ja va  2  s  .  co  m

    // null is returned when constraints aren't met
    if (s3Object == null) {
        download.setState(Transfer.TransferState.Canceled);
        download.setMonitor(new DownloadMonitor(download, null));
        return download;
    }

    long contentLength = s3Object.getObjectMetadata().getContentLength();
    if (getObjectRequest.getRange() != null && getObjectRequest.getRange().length == 2) {
        long startingByte = getObjectRequest.getRange()[0];
        long lastByte = getObjectRequest.getRange()[1];
        contentLength = lastByte - startingByte;
    }
    transferProgress.setTotalBytesToTransfer(contentLength);

    Future<?> future = threadPool.submit(new Callable<Object>() {
        //@Override
        public Object call() throws Exception {
            try {
                download.setState(Transfer.TransferState.InProgress);
                StreamingServiceUtils.downloadObjectToStream(s3Object, os);
                download.setState(Transfer.TransferState.Completed);
                return true;
            } catch (Exception e) {
                // Downloads aren't allowed to move from canceled to failed
                if (download.getState() != Transfer.TransferState.Canceled) {
                    download.setState(Transfer.TransferState.Failed);
                }
                throw e;
            }
        }
    });
    download.setMonitor(new DownloadMonitor(download, future));

    return download;
}