Search in sources :

Example 1 with TransferCompletionFilter

use of com.qcloud.cos.event.TransferCompletionFilter in project cos-java-sdk-v5 by tencentyun.

the class TransferManager method doDownload.

/**
 * Same as public interface, but adds a state listener so that callers can be notified of state
 * changes to the download.
 *
 * @see TransferManager#download(GetObjectRequest, File)
 */
private Download doDownload(final GetObjectRequest getObjectRequest, final File file, final TransferStateChangeListener stateListener, final COSProgressListener cosProgressListener, final boolean resumeExistingDownload) {
    appendSingleObjectUserAgent(getObjectRequest);
    String description = "Downloading from " + getObjectRequest.getBucketName() + "/" + getObjectRequest.getKey();
    TransferProgress transferProgress = new TransferProgress();
    // COS progress listener to capture the persistable transfer when available
    COSProgressListenerChain listenerChain = new COSProgressListenerChain(// The listener for updating transfer progress
    new TransferProgressUpdatingListener(transferProgress), getObjectRequest.getGeneralProgressListener(), // Listeners
    cosProgressListener);
    // included in
    // the original
    // request
    // The listener chain used by the low-level GetObject request.
    // This listener chain ignores any COMPLETE event, so that we could
    // delay firing the signal until the high-level download fully finishes.
    getObjectRequest.setGeneralProgressListener(new ProgressListenerChain(new TransferCompletionFilter(), listenerChain));
    long startingByte = 0;
    long lastByte;
    long[] range = getObjectRequest.getRange();
    if (range != null && range.length == 2) {
        startingByte = range[0];
        lastByte = range[1];
    } else {
        GetObjectMetadataRequest getObjectMetadataRequest = new GetObjectMetadataRequest(getObjectRequest.getBucketName(), getObjectRequest.getKey());
        if (getObjectRequest.getSSECustomerKey() != null)
            getObjectMetadataRequest.setSSECustomerKey(getObjectRequest.getSSECustomerKey());
        if (getObjectRequest.getVersionId() != null)
            getObjectMetadataRequest.setVersionId(getObjectRequest.getVersionId());
        final ObjectMetadata objectMetadata = cos.getObjectMetadata(getObjectMetadataRequest);
        lastByte = objectMetadata.getContentLength() - 1;
    }
    final long origStartingByte = startingByte;
    // We still pass the unfiltered listener chain into DownloadImpl
    final DownloadImpl download = new DownloadImpl(description, transferProgress, listenerChain, null, stateListener, getObjectRequest, file);
    long totalBytesToDownload = lastByte - startingByte + 1;
    transferProgress.setTotalBytesToTransfer(totalBytesToDownload);
    long fileLength = -1;
    if (resumeExistingDownload) {
        if (!FileLocks.lock(file)) {
            throw new FileLockException("Fail to lock " + file + " for resume download");
        }
        try {
            if (file.exists()) {
                fileLength = file.length();
                startingByte = startingByte + fileLength;
                getObjectRequest.setRange(startingByte, lastByte);
                transferProgress.updateProgress(Math.min(fileLength, totalBytesToDownload));
                totalBytesToDownload = lastByte - startingByte + 1;
                if (log.isDebugEnabled()) {
                    log.debug("Resume download: totalBytesToDownload=" + totalBytesToDownload + ", origStartingByte=" + origStartingByte + ", startingByte=" + startingByte + ", lastByte=" + lastByte + ", numberOfBytesRead=" + fileLength + ", file: " + file);
                }
            }
        } finally {
            FileLocks.unlock(file);
        }
    }
    if (totalBytesToDownload < 0) {
        throw new IllegalArgumentException("Unable to determine the range for download operation.");
    }
    final CountDownLatch latch = new CountDownLatch(1);
    Future<?> future = threadPool.submit(new DownloadCallable(cos, latch, getObjectRequest, resumeExistingDownload, download, file, origStartingByte, fileLength));
    download.setMonitor(new DownloadMonitor(download, future));
    latch.countDown();
    return download;
}
Also used : FileLockException(com.qcloud.cos.exception.FileLockException) TransferCompletionFilter(com.qcloud.cos.event.TransferCompletionFilter) CountDownLatch(java.util.concurrent.CountDownLatch) TransferProgressUpdatingListener(com.qcloud.cos.event.TransferProgressUpdatingListener) MultipleFileTransferProgressUpdatingListener(com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener) GetObjectMetadataRequest(com.qcloud.cos.model.GetObjectMetadataRequest) COSProgressListenerChain(com.qcloud.cos.event.COSProgressListenerChain) COSProgressListenerChain(com.qcloud.cos.event.COSProgressListenerChain) ProgressListenerChain(com.qcloud.cos.event.ProgressListenerChain) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 2 with TransferCompletionFilter

use of com.qcloud.cos.event.TransferCompletionFilter in project cos-java-sdk-v5 by tencentyun.

the class TransferManager method doResumableDownload.

private Download doResumableDownload(final GetObjectRequest getObjectRequest, final File destFile, final TransferStateChangeListener stateListener, final COSProgressListener cosProgressListener, String resumableTaskFilePath, int multiThreadThreshold, int partSize) {
    PersistableResumeDownload downloadRecord = getPersistableResumeRecord(getObjectRequest, destFile, resumableTaskFilePath);
    long bytesToDownload = Long.parseLong(downloadRecord.getContentLength());
    if (bytesToDownload < multiThreadThreshold) {
        downloadRecord.getDumpFile().delete();
        return doDownload(getObjectRequest, destFile, stateListener, cosProgressListener, OVERWRITE_MODE);
    }
    appendSingleObjectUserAgent(getObjectRequest);
    String description = "Resumable downloading from " + getObjectRequest.getBucketName() + "/" + getObjectRequest.getKey();
    TransferProgress transferProgress = new TransferProgress();
    // COS progress listener to capture the persistable transfer when available
    COSProgressListenerChain listenerChain = new COSProgressListenerChain(// The listener for updating transfer progress
    new TransferProgressUpdatingListener(transferProgress), getObjectRequest.getGeneralProgressListener(), cosProgressListener);
    getObjectRequest.setGeneralProgressListener(new ProgressListenerChain(new TransferCompletionFilter(), listenerChain));
    RandomAccessFile destRandomAccessFile;
    FileChannel destFileChannel;
    try {
        destRandomAccessFile = new RandomAccessFile(destFile, "rw");
        destRandomAccessFile.setLength(bytesToDownload);
        destFileChannel = destRandomAccessFile.getChannel();
    } catch (Exception e) {
        throw new CosClientException("resumable download got exception:" + e.getCause().getMessage() + e.getMessage());
    }
    transferProgress.setTotalBytesToTransfer(bytesToDownload);
    DownloadImpl download = new DownloadImpl(description, transferProgress, listenerChain, null, stateListener, getObjectRequest, destFile);
    ResumableDownloadSubmitter submitter = new ResumableDownloadSubmitter(cos, threadPool, getObjectRequest, download, destFile, destRandomAccessFile, destFileChannel, downloadRecord, partSize, multiThreadThreshold, transferProgress, listenerChain);
    ResumableDownloadMonitor monitor = ResumableDownloadMonitor.create(listenerChain, submitter, download, threadPool, downloadRecord, destFile, destFileChannel);
    download.setMonitor(monitor);
    return download;
}
Also used : TransferCompletionFilter(com.qcloud.cos.event.TransferCompletionFilter) FileChannel(java.nio.channels.FileChannel) CosClientException(com.qcloud.cos.exception.CosClientException) AbortedException(com.qcloud.cos.exception.AbortedException) FileLockException(com.qcloud.cos.exception.FileLockException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException) TransferProgressUpdatingListener(com.qcloud.cos.event.TransferProgressUpdatingListener) MultipleFileTransferProgressUpdatingListener(com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener) RandomAccessFile(java.io.RandomAccessFile) COSProgressListenerChain(com.qcloud.cos.event.COSProgressListenerChain) COSProgressListenerChain(com.qcloud.cos.event.COSProgressListenerChain) ProgressListenerChain(com.qcloud.cos.event.ProgressListenerChain)

Aggregations

COSProgressListenerChain (com.qcloud.cos.event.COSProgressListenerChain)2 MultipleFileTransferProgressUpdatingListener (com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener)2 ProgressListenerChain (com.qcloud.cos.event.ProgressListenerChain)2 TransferCompletionFilter (com.qcloud.cos.event.TransferCompletionFilter)2 TransferProgressUpdatingListener (com.qcloud.cos.event.TransferProgressUpdatingListener)2 FileLockException (com.qcloud.cos.exception.FileLockException)2 AbortedException (com.qcloud.cos.exception.AbortedException)1 CosClientException (com.qcloud.cos.exception.CosClientException)1 CosServiceException (com.qcloud.cos.exception.CosServiceException)1 GetObjectMetadataRequest (com.qcloud.cos.model.GetObjectMetadataRequest)1 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 FileChannel (java.nio.channels.FileChannel)1 CountDownLatch (java.util.concurrent.CountDownLatch)1