Search in sources :

Example 26 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class ResumableDownloadMonitor method call.

@Override
public File call() throws Exception {
    downloadSubmitter.submit();
    futures.addAll(downloadSubmitter.getFutures());
    List<DownloadPart> downloadParts = new ArrayList<DownloadPart>();
    downloadParts.addAll(downloadSubmitter.getSkippedParts());
    try {
        for (Future<DownloadPart> future : futures) {
            try {
                downloadParts.add(future.get());
            } catch (Exception e) {
                throw new CosClientException("range download got exception: " + e.getCause().getMessage() + e.getMessage());
            }
        }
        // download finished.
        downloadRecord.getDumpFile().delete();
        destFileChannel.close();
        if ((downloadRecord.getCrc64ecma() != null) && !downloadRecord.getCrc64ecma().isEmpty()) {
            checkCRC(downloadParts);
        }
        downloadComplete();
        return destFile;
    } catch (CancellationException e) {
        transfer.setState(TransferState.Canceled);
        publishProgress(listener, ProgressEventType.TRANSFER_CANCELED_EVENT);
        throw new CosClientException("Download canceled");
    } catch (Exception e) {
        downloadFailed();
        throw e;
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) CosClientException(com.qcloud.cos.exception.CosClientException) ArrayList(java.util.ArrayList) CosClientException(com.qcloud.cos.exception.CosClientException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException)

Example 27 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class ResumableDownloadSubmitter method submit.

public void submit() throws Exception {
    long contentLength = Long.parseLong(downloadRecord.getContentLength());
    download.setState(TransferState.InProgress);
    if (contentLength < this.multiThreadThreshold) {
        throw new CosClientException("contentLenth " + contentLength + " < " + this.multiThreadThreshold + " should not use resumabledownload.");
    }
    long start = 0;
    publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
    while (contentLength > start) {
        long bytesToRead = Math.min(partSize, contentLength - start);
        long end = start + bytesToRead - 1;
        String block = String.format("%d-%d", start, end);
        if (downloadRecord.hasDownloadedBlocks(block)) {
            log.debug("part found in download record: " + block);
            CRC64 crc64 = new CRC64();
            byte[] buffer = new byte[1024 * 10];
            int readBytes;
            destRandomAccessFile.seek(start);
            while (bytesToRead > 0 && (readBytes = destRandomAccessFile.read(buffer)) != -1) {
                long updateBytes = Math.min(readBytes, bytesToRead);
                bytesToRead -= updateBytes;
                crc64.update(buffer, (int) updateBytes);
            }
            skippedParts.add(new DownloadPart(start, end, crc64.getValue()));
            transferProgress.updateProgress(end + 1 - start);
        } else {
            GetObjectRequest getObj = copyGetObjectRequest(req);
            getObj.setRange(start, end);
            if (threadPool.isShutdown()) {
                publishProgress(listener, ProgressEventType.TRANSFER_CANCELED_EVENT);
                throw new CancellationException("TransferManager has been shutdown");
            }
            futures.add(threadPool.submit(new RangeDownloadCallable(cos, getObj, destFile, destFileChannel, downloadRecord)));
        }
        start = end + 1;
    }
}
Also used : CRC64(com.qcloud.cos.utils.CRC64) CancellationException(java.util.concurrent.CancellationException) CosClientException(com.qcloud.cos.exception.CosClientException) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest)

Example 28 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class CopyMonitor method call.

@Override
public CopyResult call() throws Exception {
    try {
        CopyResult result = multipartCopyCallable.call();
        if (result == null) {
            futures.addAll(multipartCopyCallable.getFutures());
            futureReference.set(threadPool.submit(new CompleteMultipartCopy(multipartCopyCallable.getMultipartUploadId(), cos, origReq, futures, listener, this)));
        } else {
            copyComplete();
        }
        return result;
    } catch (CancellationException e) {
        transfer.setState(TransferState.Canceled);
        publishProgress(listener, ProgressEventType.TRANSFER_CANCELED_EVENT);
        throw new CosClientException("Upload canceled");
    } catch (Exception e) {
        transfer.setState(TransferState.Failed);
        publishProgress(listener, ProgressEventType.TRANSFER_FAILED_EVENT);
        throw e;
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) CosClientException(com.qcloud.cos.exception.CosClientException) CosClientException(com.qcloud.cos.exception.CosClientException) CancellationException(java.util.concurrent.CancellationException) CopyResult(com.qcloud.cos.model.CopyResult)

Example 29 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class AclXmlFactory method convertToXmlByteArray.

/**
 * Converts the specified AccessControlList object to an XML fragment that can be sent to COS.
 *
 * @param acl The AccessControlList to convert to XML.
 *
 * @return an XML representation of the Access Control List object, suitable to send in a
 *         request to COS.
 */
public byte[] convertToXmlByteArray(AccessControlList acl) throws CosClientException {
    Owner owner = acl.getOwner();
    if (owner == null) {
        throw new CosClientException("Invalid AccessControlList: missing an COS Owner");
    }
    XmlWriter xml = new XmlWriter();
    xml.start("AccessControlPolicy");
    xml.start("Owner");
    if (owner.getId() != null) {
        xml.start("ID").value(owner.getId()).end();
    }
    if (owner.getDisplayName() != null) {
        xml.start("DisplayName").value(owner.getDisplayName()).end();
    }
    xml.end();
    xml.start("AccessControlList");
    for (Grant grant : acl.getGrantsAsList()) {
        xml.start("Grant");
        convertToXml(grant.getGrantee(), xml);
        xml.start("Permission").value(grant.getPermission().toString()).end();
        xml.end();
    }
    xml.end();
    xml.end();
    return xml.getBytes();
}
Also used : CosClientException(com.qcloud.cos.exception.CosClientException) XmlWriter(com.qcloud.cos.internal.XmlWriter)

Example 30 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class Message method validateMessageCrc.

private static void validateMessageCrc(ByteBuffer buf, int totalLength) {
    Checksum crc = new CRC32();
    Checksums.update(crc, (ByteBuffer) buf.duplicate().limit(buf.position() + totalLength - 4));
    long computedMessageCrc = crc.getValue();
    long wireMessageCrc = Utils.toUnsignedLong(buf.getInt(buf.position() + totalLength - 4));
    if (wireMessageCrc != computedMessageCrc) {
        throw new CosClientException(new CRC32MismatchException(format("Message checksum failure: expected 0x%x, computed 0x%x", wireMessageCrc, computedMessageCrc)));
    }
}
Also used : CRC32(java.util.zip.CRC32) Checksum(java.util.zip.Checksum) CosClientException(com.qcloud.cos.exception.CosClientException) CRC32MismatchException(com.qcloud.cos.exception.CRC32MismatchException)

Aggregations

CosClientException (com.qcloud.cos.exception.CosClientException)111 CosServiceException (com.qcloud.cos.exception.CosServiceException)64 COSCredentials (com.qcloud.cos.auth.COSCredentials)41 ClientConfig (com.qcloud.cos.ClientConfig)39 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)39 Region (com.qcloud.cos.region.Region)39 COSClient (com.qcloud.cos.COSClient)37 IOException (java.io.IOException)31 File (java.io.File)28 ByteArrayInputStream (java.io.ByteArrayInputStream)18 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)15 TransferManager (com.qcloud.cos.transfer.TransferManager)14 ExecutorService (java.util.concurrent.ExecutorService)14 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)13 URISyntaxException (java.net.URISyntaxException)13 MultiObjectDeleteException (com.qcloud.cos.exception.MultiObjectDeleteException)12 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)12 SecretKey (javax.crypto.SecretKey)12 MalformedURLException (java.net.MalformedURLException)11 PutObjectResult (com.qcloud.cos.model.PutObjectResult)10