HADOOP-10653. Add a new constructor for CryptoInputStream that receives current position of wrapped stream. Contributed by Yi Liu
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/fs-encryption@1599228 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
871616b993
commit
75ec5792df
|
@ -21,6 +21,9 @@ fs-encryption (Unreleased)
|
||||||
|
|
||||||
HADOOP-10635. Add a method to CryptoCodec to generate SRNs for IV. (Yi Liu)
|
HADOOP-10635. Add a method to CryptoCodec to generate SRNs for IV. (Yi Liu)
|
||||||
|
|
||||||
|
HADOOP-10653. Add a new constructor for CryptoInputStream that
|
||||||
|
receives current position of wrapped stream. (Yi Liu)
|
||||||
|
|
||||||
OPTIMIZATIONS
|
OPTIMIZATIONS
|
||||||
|
|
||||||
BUG FIXES
|
BUG FIXES
|
||||||
|
|
|
@ -102,18 +102,22 @@ public class CryptoInputStream extends FilterInputStream implements
|
||||||
|
|
||||||
public CryptoInputStream(InputStream in, CryptoCodec codec,
|
public CryptoInputStream(InputStream in, CryptoCodec codec,
|
||||||
int bufferSize, byte[] key, byte[] iv) throws IOException {
|
int bufferSize, byte[] key, byte[] iv) throws IOException {
|
||||||
|
this(in, codec, bufferSize, key, iv,
|
||||||
|
CryptoStreamUtils.getInputStreamOffset(in));
|
||||||
|
}
|
||||||
|
|
||||||
|
public CryptoInputStream(InputStream in, CryptoCodec codec,
|
||||||
|
int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException {
|
||||||
super(in);
|
super(in);
|
||||||
this.bufferSize = CryptoStreamUtils.checkBufferSize(codec, bufferSize);
|
this.bufferSize = CryptoStreamUtils.checkBufferSize(codec, bufferSize);
|
||||||
this.codec = codec;
|
this.codec = codec;
|
||||||
this.key = key.clone();
|
this.key = key.clone();
|
||||||
this.initIV = iv.clone();
|
this.initIV = iv.clone();
|
||||||
this.iv = iv.clone();
|
this.iv = iv.clone();
|
||||||
|
this.streamOffset = streamOffset;
|
||||||
inBuffer = ByteBuffer.allocateDirect(this.bufferSize);
|
inBuffer = ByteBuffer.allocateDirect(this.bufferSize);
|
||||||
outBuffer = ByteBuffer.allocateDirect(this.bufferSize);
|
outBuffer = ByteBuffer.allocateDirect(this.bufferSize);
|
||||||
decryptor = getDecryptor();
|
decryptor = getDecryptor();
|
||||||
if (in instanceof Seekable) {
|
|
||||||
streamOffset = ((Seekable) in).getPos();
|
|
||||||
}
|
|
||||||
resetStreamOffset(streamOffset);
|
resetStreamOffset(streamOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,10 +20,13 @@ package org.apache.hadoop.crypto;
|
||||||
import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_SECURITY_CRYPTO_BUFFER_SIZE_DEFAULT;
|
import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_SECURITY_CRYPTO_BUFFER_SIZE_DEFAULT;
|
||||||
import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_SECURITY_CRYPTO_BUFFER_SIZE_KEY;
|
import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_SECURITY_CRYPTO_BUFFER_SIZE_KEY;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
import org.apache.hadoop.classification.InterfaceAudience;
|
import org.apache.hadoop.classification.InterfaceAudience;
|
||||||
import org.apache.hadoop.conf.Configuration;
|
import org.apache.hadoop.conf.Configuration;
|
||||||
|
import org.apache.hadoop.fs.Seekable;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
|
@ -52,4 +55,15 @@ public class CryptoStreamUtils {
|
||||||
"Minimum value of buffer size is " + MIN_BUFFER_SIZE + ".");
|
"Minimum value of buffer size is " + MIN_BUFFER_SIZE + ".");
|
||||||
return bufferSize - bufferSize % codec.getAlgorithmBlockSize();
|
return bufferSize - bufferSize % codec.getAlgorithmBlockSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If input stream is {@link org.apache.hadoop.fs.Seekable}, return it's
|
||||||
|
* current position, otherwise return 0;
|
||||||
|
*/
|
||||||
|
public static long getInputStreamOffset(InputStream in) throws IOException {
|
||||||
|
if (in instanceof Seekable) {
|
||||||
|
return ((Seekable) in).getPos();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue