Search in sources :

Example 1 with InputSubstream

use of com.amazonaws.services.s3.internal.InputSubstream in project aws-sdk-android by aws-amplify.

the class InputStreamsTest method testMarkReset.

/**
 * Tests that we can combine InputSubstream with RepeatableFileInputStream
 * and correctly mark/reset the streams.
 */
@Test
public void testMarkReset() throws Exception {
    File tempFile = File.createTempFile("aws-java-sdk-inputsubstream-test", ".dat");
    FileOutputStream outputStream = new FileOutputStream(tempFile);
    outputStream.write(sampleData.getBytes(StringUtils.UTF8));
    outputStream.close();
    RepeatableFileInputStream repeatableFileInputStream = new RepeatableFileInputStream(tempFile);
    InputSubstream in = new InputSubstream(repeatableFileInputStream, 10, 10, true);
    assertEquals(10, in.available());
    byte[] buffer = new byte[5];
    in.mark(1024);
    assertEquals(5, in.read(buffer));
    assertEquals("12345", new String(buffer, StringUtils.UTF8));
    assertEquals(5, in.available());
    in.reset();
    assertEquals(10, in.available());
    assertEquals(5, in.read(buffer));
    assertEquals("12345", new String(buffer, StringUtils.UTF8));
    assertEquals(5, in.available());
    assertEquals(5, in.read(buffer));
    assertEquals("67890", new String(buffer, StringUtils.UTF8));
    assertEquals(0, in.available());
}
Also used : InputSubstream(com.amazonaws.services.s3.internal.InputSubstream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) RepeatableFileInputStream(com.amazonaws.services.s3.internal.RepeatableFileInputStream) Test(org.junit.Test)

Example 2 with InputSubstream

use of com.amazonaws.services.s3.internal.InputSubstream in project aws-sdk-android by aws-amplify.

the class InputStreamsTest method testSimple.

/**
 * Tests the simple use case for InputSubstream
 */
@Test
public void testSimple() throws Exception {
    InputSubstream in = new InputSubstream(new ByteArrayInputStream(sampleData.getBytes(StringUtils.UTF8)), 10, 10, true);
    assertEquals(10, in.available());
    byte[] buffer = new byte[10];
    assertEquals(10, in.read(buffer));
    assertEquals("1234567890", new String(buffer, StringUtils.UTF8));
    assertEquals(0, in.available());
    CountingInputStream countingStream = new CountingInputStream(new InputSubstream(new ByteArrayInputStream(sampleData.getBytes(StringUtils.UTF8)), 10, 10, true));
    int c;
    System.out.print("Data: ");
    while ((c = countingStream.read()) > -1) {
        System.out.print((char) c);
    }
    System.out.println();
    assertEquals(10, countingStream.getByteCount());
    countingStream = new CountingInputStream(new InputSubstream(new ByteArrayInputStream(sampleData.getBytes(StringUtils.UTF8)), 10, 10, true));
    byte[] bytes = new byte[1];
    System.out.print("Data: ");
    while ((c = countingStream.read(bytes)) > -1) {
        System.out.print((char) bytes[0]);
    }
    System.out.println();
    assertEquals(10, countingStream.getByteCount());
}
Also used : InputSubstream(com.amazonaws.services.s3.internal.InputSubstream) ByteArrayInputStream(java.io.ByteArrayInputStream) CountingInputStream(com.amazonaws.util.CountingInputStream) Test(org.junit.Test)

Example 3 with InputSubstream

use of com.amazonaws.services.s3.internal.InputSubstream in project aws-sdk-android by aws-amplify.

the class AmazonS3Client method uploadPart.

/*
     * (non-Javadoc)
     * @see
     * com.amazonaws.services.s3.AmazonS3#uploadPart(com.amazonaws.services.
     * s3.model.UploadPartRequest)
     */
@Override
public UploadPartResult uploadPart(UploadPartRequest uploadPartRequest) throws AmazonClientException, AmazonServiceException {
    assertParameterNotNull(uploadPartRequest, "The request parameter must be specified when uploading a part");
    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();
    assertParameterNotNull(bucketName, "The bucket name parameter must be specified when uploading a part");
    assertParameterNotNull(key, "The key parameter must be specified when uploading a part");
    assertParameterNotNull(uploadId, "The upload ID parameter must be specified when uploading a part");
    assertParameterNotNull(partNumber, "The part number parameter must be specified when uploading a part");
    assertParameterNotNull(partSize, "The part size parameter must be specified when uploading a part");
    final Request<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);
    }
    request.addHeader(Headers.CONTENT_LENGTH, Long.toString(partSize));
    /*
         * HttpUrlConnection seems to be buggy in terms of implementation of
         * expect continue.
         */
    // request.addHeader("Expect", "100-continue");
    populateRequesterPaysHeader(request, uploadPartRequest.isRequesterPays());
    // Populate the SSE-CPK parameters to the request header
    populateSSE_C(request, uploadPartRequest.getSSECustomerKey());
    InputStream inputStream = null;
    if (uploadPartRequest.getInputStream() != null) {
        inputStream = uploadPartRequest.getInputStream();
    } else if (uploadPartRequest.getFile() != null) {
        try {
            inputStream = new InputSubstream(new RepeatableFileInputStream(uploadPartRequest.getFile()), uploadPartRequest.getFileOffset(), partSize, true);
        } catch (final FileNotFoundException e) {
            throw new IllegalArgumentException("The specified file doesn't exist", e);
        }
    } else {
        throw new IllegalArgumentException("A File or InputStream must be specified when uploading part");
    }
    // until request is invoked.
    if (uploadPartRequest.getMd5Digest() == null && !ServiceUtils.skipMd5CheckPerRequest(uploadPartRequest, clientOptions) && inputStream.markSupported()) {
        try {
            final String contentMd5_b64 = Md5Utils.md5AsBase64(inputStream);
            addHeaderIfNotNull(request, Headers.CONTENT_MD5, contentMd5_b64);
            inputStream.reset();
        } catch (final Exception e) {
            throw new AmazonClientException("Unable to calculate MD5 hash: " + e.getMessage(), e);
        }
    }
    /*
         * This is compatible with progress listener set by either the legacy
         * method UploadPartRequest#setProgressListener or the new method
         * UploadPartRequest#setGeneralProgressListener.
         */
    final ProgressListener progressListener = uploadPartRequest.getGeneralProgressListener();
    final ProgressListenerCallbackExecutor progressListenerCallbackExecutor = ProgressListenerCallbackExecutor.wrapListener(progressListener);
    if (progressListenerCallbackExecutor != null) {
        inputStream = new ProgressReportingInputStream(inputStream, progressListenerCallbackExecutor);
        ((ProgressReportingInputStream) inputStream).setNotificationThreshold(this.notificationThreshold);
        fireProgressEvent(progressListenerCallbackExecutor, ProgressEvent.PART_STARTED_EVENT_CODE);
    }
    try {
        request.setContent(inputStream);
        final ObjectMetadata metadata = invoke(request, new S3MetadataResponseHandler(), bucketName, key);
        fireProgressEvent(progressListenerCallbackExecutor, ProgressEvent.PART_COMPLETED_EVENT_CODE);
        final UploadPartResult result = new UploadPartResult();
        result.setETag(metadata.getETag());
        result.setPartNumber(partNumber);
        result.setSSEAlgorithm(metadata.getSSEAlgorithm());
        result.setSSECustomerAlgorithm(metadata.getSSECustomerAlgorithm());
        result.setSSECustomerKeyMd5(metadata.getSSECustomerKeyMd5());
        result.setRequesterCharged(metadata.isRequesterCharged());
        return result;
    } catch (final AmazonClientException ace) {
        fireProgressEvent(progressListenerCallbackExecutor, ProgressEvent.PART_FAILED_EVENT_CODE);
        throw ace;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (final Exception e) {
            }
        }
    }
}
Also used : ServiceClientHolderInputStream(com.amazonaws.util.ServiceClientHolderInputStream) LengthCheckInputStream(com.amazonaws.util.LengthCheckInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ProgressReportingInputStream(com.amazonaws.event.ProgressReportingInputStream) RepeatableFileInputStream(com.amazonaws.services.s3.internal.RepeatableFileInputStream) InputStream(java.io.InputStream) AmazonClientException(com.amazonaws.AmazonClientException) FileNotFoundException(java.io.FileNotFoundException) RepeatableFileInputStream(com.amazonaws.services.s3.internal.RepeatableFileInputStream) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) FileNotFoundException(java.io.FileNotFoundException) AbortedException(com.amazonaws.AbortedException) ProgressListenerCallbackExecutor(com.amazonaws.event.ProgressListenerCallbackExecutor) InputSubstream(com.amazonaws.services.s3.internal.InputSubstream) ProgressListener(com.amazonaws.event.ProgressListener) S3MetadataResponseHandler(com.amazonaws.services.s3.internal.S3MetadataResponseHandler) ProgressReportingInputStream(com.amazonaws.event.ProgressReportingInputStream)

Example 4 with InputSubstream

use of com.amazonaws.services.s3.internal.InputSubstream 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 5 with InputSubstream

use of com.amazonaws.services.s3.internal.InputSubstream in project aws-sdk-android by aws-amplify.

the class EncryptionUtils method getEncryptedInputStream.

/**
 * Retrives the encrypted input stream.
 * @param request the UploadPartRequest to encrypt.
 * @param cipherFactory the CipherFactory used to encrypt.
 * @return the encrypted input stream.
 */
public static ByteRangeCapturingInputStream getEncryptedInputStream(UploadPartRequest request, CipherFactory cipherFactory) {
    try {
        InputStream originalInputStream = request.getInputStream();
        if (request.getFile() != null) {
            originalInputStream = new InputSubstream(new RepeatableFileInputStream(request.getFile()), request.getFileOffset(), request.getPartSize(), request.isLastPart());
        }
        originalInputStream = new RepeatableCipherInputStream(originalInputStream, cipherFactory);
        if (!request.isLastPart()) {
            // We want to prevent the final padding from being sent on the
            // stream...
            originalInputStream = new InputSubstream(originalInputStream, 0, request.getPartSize(), false);
        }
        long partSize = request.getPartSize();
        int cipherBlockSize = cipherFactory.createCipher().getBlockSize();
        return new ByteRangeCapturingInputStream(originalInputStream, partSize - cipherBlockSize, partSize);
    } catch (Exception e) {
        throw new AmazonClientException("Unable to create cipher input stream: " + e.getMessage(), e);
    }
}
Also used : RepeatableCipherInputStream(com.amazonaws.services.s3.internal.RepeatableCipherInputStream) InputSubstream(com.amazonaws.services.s3.internal.InputSubstream) LengthCheckInputStream(com.amazonaws.util.LengthCheckInputStream) RepeatableFileInputStream(com.amazonaws.services.s3.internal.RepeatableFileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) RepeatableCipherInputStream(com.amazonaws.services.s3.internal.RepeatableCipherInputStream) S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) InputStream(java.io.InputStream) AmazonClientException(com.amazonaws.AmazonClientException) RepeatableFileInputStream(com.amazonaws.services.s3.internal.RepeatableFileInputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AmazonClientException(com.amazonaws.AmazonClientException)

Aggregations

InputSubstream (com.amazonaws.services.s3.internal.InputSubstream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 AmazonClientException (com.amazonaws.AmazonClientException)3 RepeatableFileInputStream (com.amazonaws.services.s3.internal.RepeatableFileInputStream)3 LengthCheckInputStream (com.amazonaws.util.LengthCheckInputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 File (java.io.File)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Test (org.junit.Test)2 AbortedException (com.amazonaws.AbortedException)1 ProgressListener (com.amazonaws.event.ProgressListener)1 ProgressListenerCallbackExecutor (com.amazonaws.event.ProgressListenerCallbackExecutor)1 ProgressReportingInputStream (com.amazonaws.event.ProgressReportingInputStream)1 ReleasableInputStream (com.amazonaws.internal.ReleasableInputStream)1 ResettableInputStream (com.amazonaws.internal.ResettableInputStream)1 SdkFilterInputStream (com.amazonaws.internal.SdkFilterInputStream)1 RepeatableCipherInputStream (com.amazonaws.services.s3.internal.RepeatableCipherInputStream)1 S3MetadataResponseHandler (com.amazonaws.services.s3.internal.S3MetadataResponseHandler)1