Search in sources :

Example 1 with ResettableInputStream

use of com.qcloud.cos.internal.ResettableInputStream in project cos-java-sdk-v5 by tencentyun.

the class COSClient method uploadPart.

@Override
public UploadPartResult uploadPart(UploadPartRequest uploadPartRequest) throws CosClientException, CosServiceException {
    rejectNull(uploadPartRequest, "The request parameter must be specified when uploading a part");
    final File fileOrig = uploadPartRequest.getFile();
    final InputStream isOrig = uploadPartRequest.getInputStream();
    final String bucketName = uploadPartRequest.getBucketName();
    final String key = uploadPartRequest.getKey();
    final String uploadId = uploadPartRequest.getUploadId();
    final int partNumber = uploadPartRequest.getPartNumber();
    final long partSize = uploadPartRequest.getPartSize();
    rejectNull(bucketName, "The bucket name parameter must be specified when uploading a part");
    rejectNull(key, "The key parameter must be specified when uploading a part");
    rejectNull(uploadId, "The upload ID parameter must be specified when uploading a part");
    rejectNull(partNumber, "The part number parameter must be specified when uploading a part");
    rejectNull(partSize, "The part size parameter must be specified when uploading a part");
    rejectNull(clientConfig.getRegion(), "region is null, region in clientConfig must be specified when uploading a part");
    CosHttpRequest<UploadPartRequest> request = createRequest(bucketName, key, uploadPartRequest, HttpMethodName.PUT);
    request.addParameter("uploadId", uploadId);
    request.addParameter("partNumber", Integer.toString(partNumber));
    final ObjectMetadata objectMetadata = uploadPartRequest.getObjectMetadata();
    if (objectMetadata != null)
        populateRequestMetadata(request, objectMetadata);
    addHeaderIfNotNull(request, Headers.CONTENT_MD5, uploadPartRequest.getMd5Digest());
    request.addHeader(Headers.CONTENT_LENGTH, Long.toString(partSize));
    // Populate the SSE-C parameters to the request header
    populateSSE_C(request, uploadPartRequest.getSSECustomerKey());
    // Populate the traffic limit parameter to the request header
    populateTrafficLimit(request, uploadPartRequest.getTrafficLimit());
    InputStream isCurr = isOrig;
    try {
        if (fileOrig == null) {
            if (isOrig == null) {
                throw new IllegalArgumentException("A File or InputStream must be specified when uploading part");
            } else {
                // When isCurr is a FileInputStream, this wrapping enables
                // unlimited mark-and-reset
                isCurr = ReleasableInputStream.wrap(isCurr);
            }
        } else {
            try {
                isCurr = new ResettableInputStream(fileOrig);
            } catch (IOException e) {
                throw new IllegalArgumentException("Failed to open file " + fileOrig, e);
            }
        }
        isCurr = new InputSubstream(isCurr, uploadPartRequest.getFileOffset(), partSize, uploadPartRequest.isLastPart());
        MD5DigestCalculatingInputStream md5DigestStream = null;
        if (uploadPartRequest.getMd5Digest() == null && !skipMd5CheckStrategy.skipClientSideValidationPerRequest(uploadPartRequest)) {
            /*
                 * If the user hasn't set the content MD5, then we don't want to buffer the whole
                 * stream in memory just to calculate it. Instead, we can calculate it on the fly
                 * and validate it with the returned ETag from the object upload.
                 */
            isCurr = md5DigestStream = new MD5DigestCalculatingInputStream(isCurr);
        }
        return doUploadPart(bucketName, key, uploadId, partNumber, partSize, request, isCurr, md5DigestStream);
    } finally {
        CosDataSource.Utils.cleanupDataSource(uploadPartRequest, fileOrig, isOrig, isCurr, log);
    }
}
Also used : DigestValidationInputStream(com.qcloud.cos.internal.DigestValidationInputStream) ReleasableInputStream(com.qcloud.cos.internal.ReleasableInputStream) MD5DigestCalculatingInputStream(com.qcloud.cos.internal.MD5DigestCalculatingInputStream) LengthCheckInputStream(com.qcloud.cos.internal.LengthCheckInputStream) ServiceClientHolderInputStream(com.qcloud.cos.internal.ServiceClientHolderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ResettableInputStream(com.qcloud.cos.internal.ResettableInputStream) SdkFilterInputStream(com.qcloud.cos.internal.SdkFilterInputStream) InputStream(java.io.InputStream) MD5DigestCalculatingInputStream(com.qcloud.cos.internal.MD5DigestCalculatingInputStream) IOException(java.io.IOException) InputSubstream(com.qcloud.cos.internal.InputSubstream) File(java.io.File) ResettableInputStream(com.qcloud.cos.internal.ResettableInputStream)

Example 2 with ResettableInputStream

use of com.qcloud.cos.internal.ResettableInputStream in project cos-java-sdk-v5 by tencentyun.

the class COSCryptoModuleBase method newCOSCipherLiteInputStream.

private CipherLiteInputStream newCOSCipherLiteInputStream(AbstractPutObjectRequest req, ContentCryptoMaterial cekMaterial, long plaintextLength) {
    final File fileOrig = req.getFile();
    final InputStream isOrig = req.getInputStream();
    InputStream isCurr = null;
    try {
        if (fileOrig == null) {
            // When input is a FileInputStream, this wrapping enables
            // unlimited mark-and-reset
            isCurr = isOrig == null ? null : ReleasableInputStream.wrap(isOrig);
        } else {
            isCurr = new ResettableInputStream(fileOrig);
        }
        if (plaintextLength > -1) {
            // COS allows a single PUT to be no more than 5GB, which
            // therefore won't exceed the maximum length that can be
            // encrypted either using any cipher such as CBC or GCM.
            // This ensures the plain-text read from the underlying data
            // stream has the same length as the expected total.
            isCurr = new LengthCheckInputStream(isCurr, plaintextLength, EXCLUDE_SKIPPED_BYTES);
        }
        final CipherLite cipherLite = cekMaterial.getCipherLite();
        if (cipherLite.markSupported()) {
            return new CipherLiteInputStream(isCurr, cipherLite, DEFAULT_BUFFER_SIZE);
        } else {
            return new RenewableCipherLiteInputStream(isCurr, cipherLite, DEFAULT_BUFFER_SIZE);
        }
    } catch (Exception e) {
        cleanupDataSource(req, fileOrig, isOrig, isCurr, log);
        throw new CosClientException("Unable to create cipher input stream", e);
    }
}
Also used : LengthCheckInputStream(com.qcloud.cos.internal.LengthCheckInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ResettableInputStream(com.qcloud.cos.internal.ResettableInputStream) ReleasableInputStream(com.qcloud.cos.internal.ReleasableInputStream) SdkFilterInputStream(com.qcloud.cos.internal.SdkFilterInputStream) LengthCheckInputStream(com.qcloud.cos.internal.LengthCheckInputStream) InputStream(java.io.InputStream) CosClientException(com.qcloud.cos.exception.CosClientException) InstructionFile(com.qcloud.cos.internal.crypto.CryptoStorageMode.InstructionFile) File(java.io.File) ResettableInputStream(com.qcloud.cos.internal.ResettableInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with ResettableInputStream

use of com.qcloud.cos.internal.ResettableInputStream in project cos-java-sdk-v5 by tencentyun.

the class COSCryptoModuleBase method newMultipartCOSCipherInputStream.

protected final CipherLiteInputStream newMultipartCOSCipherInputStream(UploadPartRequest req, CipherLite cipherLite) {
    final File fileOrig = req.getFile();
    final InputStream isOrig = req.getInputStream();
    InputStream isCurr = null;
    try {
        if (fileOrig == null) {
            if (isOrig == null) {
                throw new IllegalArgumentException("A File or InputStream must be specified when uploading part");
            }
            isCurr = isOrig;
        } else {
            isCurr = new ResettableInputStream(fileOrig);
        }
        isCurr = new InputSubstream(isCurr, req.getFileOffset(), req.getPartSize(), req.isLastPart());
        return cipherLite.markSupported() ? new CipherLiteInputStream(isCurr, cipherLite, DEFAULT_BUFFER_SIZE, IS_MULTI_PART, req.isLastPart()) : new RenewableCipherLiteInputStream(isCurr, cipherLite, DEFAULT_BUFFER_SIZE, IS_MULTI_PART, req.isLastPart());
    } catch (Exception e) {
        cleanupDataSource(req, fileOrig, isOrig, isCurr, log);
        throw new CosClientException("Unable to create cipher input stream", e);
    }
}
Also used : InputSubstream(com.qcloud.cos.internal.InputSubstream) ByteArrayInputStream(java.io.ByteArrayInputStream) ResettableInputStream(com.qcloud.cos.internal.ResettableInputStream) ReleasableInputStream(com.qcloud.cos.internal.ReleasableInputStream) SdkFilterInputStream(com.qcloud.cos.internal.SdkFilterInputStream) LengthCheckInputStream(com.qcloud.cos.internal.LengthCheckInputStream) InputStream(java.io.InputStream) CosClientException(com.qcloud.cos.exception.CosClientException) InstructionFile(com.qcloud.cos.internal.crypto.CryptoStorageMode.InstructionFile) File(java.io.File) ResettableInputStream(com.qcloud.cos.internal.ResettableInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

LengthCheckInputStream (com.qcloud.cos.internal.LengthCheckInputStream)3 ReleasableInputStream (com.qcloud.cos.internal.ReleasableInputStream)3 ResettableInputStream (com.qcloud.cos.internal.ResettableInputStream)3 SdkFilterInputStream (com.qcloud.cos.internal.SdkFilterInputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 File (java.io.File)3 InputStream (java.io.InputStream)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 CosClientException (com.qcloud.cos.exception.CosClientException)2 CosServiceException (com.qcloud.cos.exception.CosServiceException)2 InputSubstream (com.qcloud.cos.internal.InputSubstream)2 InstructionFile (com.qcloud.cos.internal.crypto.CryptoStorageMode.InstructionFile)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 DigestValidationInputStream (com.qcloud.cos.internal.DigestValidationInputStream)1 MD5DigestCalculatingInputStream (com.qcloud.cos.internal.MD5DigestCalculatingInputStream)1 ServiceClientHolderInputStream (com.qcloud.cos.internal.ServiceClientHolderInputStream)1 IOException (java.io.IOException)1