com.ksyun.ks3.service.encryption.internal.RepeatableCipherInputStream Maven / Gradle / Ivy
package com.ksyun.ks3.service.encryption.internal;
import java.io.FilterInputStream;
import java.io.InputStream;
import javax.crypto.CipherInputStream;
/**
* 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.
*
* It's repeatable if and only if the underlying unencryptedDataStream is
* repeatable - if the underlying input stream is not repeatable and you're
* going to buffer to make it repeatable anyways, it makes more sense to do
* so after wrapping in this object, so we buffer the encrypted data and don't
* have to bother re-encrypting on retry.
*
* This stream only supports being marked before the first call to
* {@code read} or {@code skip}, since it's not possible to rewind the
* encryption state of a {@code CipherInputStream} to an arbitrary point. If
* you call {@code mark} after calling {@code read} or {@code skip}, it will
* throw an {@code UnsupportedOperationException}.
*/
public class RepeatableCipherInputStream extends
AbstractRepeatableCipherInputStream {
/**
* 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(final InputStream input,
final CipherFactory cipherFactory) {
super(input,
newCipherInputStream(input, cipherFactory),
cipherFactory);
}
@Override
protected FilterInputStream createCipherInputStream(
InputStream unencryptedDataStream, CipherFactory cipherFactory) {
return newCipherInputStream(unencryptedDataStream, cipherFactory);
}
private static FilterInputStream newCipherInputStream(
InputStream unencryptedDataStream, CipherFactory cipherFactory) {
return new CipherInputStream(unencryptedDataStream,
cipherFactory.createCipher());
}
}