forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatableCipherInputStream.java
More file actions
77 lines (67 loc) · 3.01 KB
/
RepeatableCipherInputStream.java
File metadata and controls
77 lines (67 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright 2013-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Portions copyright 2006-2009 James Murty. Please see LICENSE.txt
* for applicable license terms and NOTICE.txt for applicable notices.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.services.s3.internal;
import java.io.IOException;
import java.io.InputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import com.amazonaws.services.s3.internal.crypto.CipherFactory;
/**
* Wraps an InputStream with a CipherInputStream to encrypt it, and handles
* resets by attempting to reset on the original, unencrypted data InputStream,
* and recreate an identical Cipher and identical CipherInputStream on the
* original data.
*/
public class RepeatableCipherInputStream extends AbstractRepeatableInputStream {
private CipherFactory cipherFactory;
private InputStream unencryptedDataStream;
/**
* Constructs a new repeatable cipher input stream using the specified
* InputStream as the source data, and the CipherFactory for building Cipher
* objects.
*
* @param input
* The original, unencrypted data stream. This stream should be
* markable/resetable in order for this class to work correctly.
* @param cipherFactory
* The factory used for creating identical cipher objects when
* this stream is reset and a new CipherInputStream is needed.
*/
public RepeatableCipherInputStream(InputStream input, CipherFactory cipherFactory) {
super(createCipherInputStream(input, cipherFactory));
this.unencryptedDataStream = input;
this.cipherFactory = cipherFactory;
// Mark the beginning of the data stream so we can reset back to it
unencryptedDataStream.mark(-1);
}
private static CipherInputStream createCipherInputStream(InputStream input, CipherFactory cipherFactory) {
Cipher cipher = cipherFactory.createCipher();
return new CipherInputStream(input, cipher);
}
@Override
protected void reopenWrappedStream() throws IOException {
// We don't need to call in.close() here, since what
// CipherInputStream.close() does is merely call close() of the
// underlying stream (which is the same object as the
// unencryptedDataStream).
// So this actually means we SHOULD NOT call in.close(), since we still
// need the unencryptedDataStream to be available for reset.
unencryptedDataStream.reset();
in = createCipherInputStream(unencryptedDataStream, cipherFactory);
}
}