Search in sources :

Example 1 with ResettableInputStream

use of com.amazonaws.internal.ResettableInputStream in project aws-sdk-android by aws-amplify.

the class S3CryptoModuleBase method newS3CipherLiteInputStream.

private CipherLiteInputStream newS3CipherLiteInputStream(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) {
            // S3 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 (final Exception e) {
        cleanupDataSource(req, fileOrig, isOrig, isCurr, log);
        throw new AmazonClientException("Unable to create cipher input stream", e);
    }
}
Also used : LengthCheckInputStream(com.amazonaws.util.LengthCheckInputStream) LengthCheckInputStream(com.amazonaws.util.LengthCheckInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ReleasableInputStream(com.amazonaws.internal.ReleasableInputStream) ResettableInputStream(com.amazonaws.internal.ResettableInputStream) SdkFilterInputStream(com.amazonaws.internal.SdkFilterInputStream) InputStream(java.io.InputStream) AmazonClientException(com.amazonaws.AmazonClientException) InstructionFile(com.amazonaws.services.s3.model.CryptoStorageMode.InstructionFile) File(java.io.File) ResettableInputStream(com.amazonaws.internal.ResettableInputStream) AmazonServiceException(com.amazonaws.AmazonServiceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AmazonClientException(com.amazonaws.AmazonClientException) IOException(java.io.IOException)

Example 2 with ResettableInputStream

use of com.amazonaws.internal.ResettableInputStream in project aws-sdk-android by aws-amplify.

the class S3CryptoModuleBase method newMultipartS3CipherInputStream.

protected final CipherLiteInputStream newMultipartS3CipherInputStream(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 (final Exception e) {
        cleanupDataSource(req, fileOrig, isOrig, isCurr, log);
        throw new AmazonClientException("Unable to create cipher input stream", e);
    }
}
Also used : InputSubstream(com.amazonaws.services.s3.internal.InputSubstream) LengthCheckInputStream(com.amazonaws.util.LengthCheckInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ReleasableInputStream(com.amazonaws.internal.ReleasableInputStream) ResettableInputStream(com.amazonaws.internal.ResettableInputStream) SdkFilterInputStream(com.amazonaws.internal.SdkFilterInputStream) InputStream(java.io.InputStream) AmazonClientException(com.amazonaws.AmazonClientException) InstructionFile(com.amazonaws.services.s3.model.CryptoStorageMode.InstructionFile) File(java.io.File) ResettableInputStream(com.amazonaws.internal.ResettableInputStream) AmazonServiceException(com.amazonaws.AmazonServiceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AmazonClientException(com.amazonaws.AmazonClientException) IOException(java.io.IOException)

Example 3 with ResettableInputStream

use of com.amazonaws.internal.ResettableInputStream in project sirius-biz by scireum.

the class Storage method updateFile.

/**
 * Updates the given file using the given data.
 *
 * @param file     the S3 file to update
 * @param data     the data to store
 * @param filename the original filename to save
 */
public void updateFile(@Nonnull StoredObject file, @Nonnull File data, @Nullable String filename) {
    try {
        try (InputStream in = new ResettableInputStream(data)) {
            String md5 = calculateMd5(data);
            updateFile(file, in, filename, md5, data.length());
        }
    } catch (Exception e) {
        throw Exceptions.handle().to(LOG).error(e).withSystemErrorMessage("Cannot upload the file: %s (%s) - %s (%s)", file, filename).handle();
    }
}
Also used : ResettableInputStream(com.amazonaws.internal.ResettableInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ResettableInputStream(com.amazonaws.internal.ResettableInputStream) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Aggregations

ResettableInputStream (com.amazonaws.internal.ResettableInputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 AmazonClientException (com.amazonaws.AmazonClientException)2 AmazonServiceException (com.amazonaws.AmazonServiceException)2 ReleasableInputStream (com.amazonaws.internal.ReleasableInputStream)2 SdkFilterInputStream (com.amazonaws.internal.SdkFilterInputStream)2 InstructionFile (com.amazonaws.services.s3.model.CryptoStorageMode.InstructionFile)2 LengthCheckInputStream (com.amazonaws.util.LengthCheckInputStream)2 File (java.io.File)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 InputSubstream (com.amazonaws.services.s3.internal.InputSubstream)1 SQLException (java.sql.SQLException)1