Search in sources :

Example 1 with COSObjectInputStream

use of com.qcloud.cos.model.COSObjectInputStream in project cos-java-sdk-v5 by tencentyun.

the class COSCryptoModuleAE method decrypt.

/*
     * Private helper methods
     */
/**
 * Returns an updated object where the object content input stream contains the decrypted
 * contents.
 *
 * @param wrapper The object whose contents are to be decrypted.
 * @param cekMaterial The instruction that will be used to decrypt the object data.
 * @return The updated object where the object content input stream contains the decrypted
 *         contents.
 */
private COSObjectWrapper decrypt(COSObjectWrapper wrapper, ContentCryptoMaterial cekMaterial, long[] range) {
    COSObjectInputStream objectContent = wrapper.getObjectContent();
    wrapper.setObjectContent(new COSObjectInputStream(new CipherLiteInputStream(objectContent, cekMaterial.getCipherLite(), DEFAULT_BUFFER_SIZE), objectContent.getHttpRequest()));
    return wrapper;
}
Also used : COSObjectInputStream(com.qcloud.cos.model.COSObjectInputStream)

Example 2 with COSObjectInputStream

use of com.qcloud.cos.model.COSObjectInputStream in project cos-java-sdk-v5 by tencentyun.

the class COSCryptoModuleAE method adjustToDesiredRange.

/**
 * Adjusts the retrieved COSObject so that the object contents contain only the range of bytes
 * desired by the user. Since encrypted contents can only be retrieved in CIPHER_BLOCK_SIZE (16
 * bytes) chunks, the COSObject potentially contains more bytes than desired, so this method
 * adjusts the contents range.
 *
 * @param cosObject The COSObject retrieved from COS that could possibly contain more bytes than
 *        desired by the user.
 * @param range A two-element array of longs corresponding to the start and finish (inclusive)
 *        of a desired range of bytes.
 * @param instruction Instruction file in JSON or null if no instruction file is involved
 * @return The COSObject with adjusted object contents containing only the range desired by the
 *         user. If the range specified is invalid, then the COSObject is returned without any
 *         modifications.
 */
protected final COSObjectWrapper adjustToDesiredRange(COSObjectWrapper cosObject, long[] range, Map<String, String> instruction) {
    if (range == null)
        return cosObject;
    // Figure out the original encryption scheme used, which can be
    // different from the crypto scheme used for decryption.
    ContentCryptoScheme encryptionScheme = cosObject.encryptionSchemeOf(instruction);
    // range get on data encrypted using AES_GCM
    final long instanceLen = cosObject.getObjectMetadata().getInstanceLength();
    final long maxOffset = instanceLen - encryptionScheme.getTagLengthInBits() / 8 - 1;
    if (range[1] > maxOffset) {
        range[1] = maxOffset;
        if (range[0] > range[1]) {
            // Return empty content
            // First let's close the existing input stream to avoid resource
            // leakage
            IOUtils.closeQuietly(cosObject.getObjectContent(), log);
            cosObject.setObjectContent(new ByteArrayInputStream(new byte[0]));
            return cosObject;
        }
    }
    if (range[0] > range[1]) {
        // Make no modifications if range is invalid.
        return cosObject;
    }
    try {
        COSObjectInputStream objectContent = cosObject.getObjectContent();
        InputStream adjustedRangeContents = new AdjustedRangeInputStream(objectContent, range[0], range[1]);
        cosObject.setObjectContent(new COSObjectInputStream(adjustedRangeContents, objectContent.getHttpRequest()));
        return cosObject;
    } catch (IOException e) {
        throw new CosClientException("Error adjusting output to desired byte range: " + e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) COSObjectInputStream(com.qcloud.cos.model.COSObjectInputStream) InputStream(java.io.InputStream) CosClientException(com.qcloud.cos.exception.CosClientException) COSObjectInputStream(com.qcloud.cos.model.COSObjectInputStream) IOException(java.io.IOException)

Example 3 with COSObjectInputStream

use of com.qcloud.cos.model.COSObjectInputStream in project cos-java-sdk-v5 by tencentyun.

the class COSObjectResponseHandler method handle.

@Override
public CosServiceResponse<COSObject> handle(CosHttpResponse response) throws Exception {
    COSObject object = new COSObject();
    CosServiceResponse<COSObject> cosResponse = parseResponseMetadata(response);
    ObjectMetadata metadata = object.getObjectMetadata();
    populateObjectMetadata(response, metadata);
    object.setObjectContent(new COSObjectInputStream(response.getContent(), response.getHttpRequest()));
    cosResponse.setResult(object);
    return cosResponse;
}
Also used : COSObject(com.qcloud.cos.model.COSObject) COSObjectInputStream(com.qcloud.cos.model.COSObjectInputStream) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Aggregations

COSObjectInputStream (com.qcloud.cos.model.COSObjectInputStream)3 CosClientException (com.qcloud.cos.exception.CosClientException)1 COSObject (com.qcloud.cos.model.COSObject)1 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1