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());
}
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());
}
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) {
}
}
}
}
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);
}
}
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);
}
}
Aggregations