Package org.apache.hadoop.crypto
Class CryptoInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
org.apache.hadoop.crypto.CryptoInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable,Channel,ReadableByteChannel,ByteBufferPositionedReadable,ByteBufferReadable,CanSetDropBehind,CanSetReadahead,CanUnbuffer,HasEnhancedByteBufferAccess,HasFileDescriptor,PositionedReadable,Seekable,IOStatisticsSource,StreamCapabilities
@Private
@Evolving
public class CryptoInputStream
extends FilterInputStream
implements Seekable, PositionedReadable, ByteBufferReadable, HasFileDescriptor, CanSetDropBehind, CanSetReadahead, HasEnhancedByteBufferAccess, ReadableByteChannel, CanUnbuffer, StreamCapabilities, ByteBufferPositionedReadable, IOStatisticsSource
CryptoInputStream decrypts data. It is not thread-safe. AES CTR mode is
required in order to ensure that the plain text and cipher text have a 1:1
mapping. The decryption is buffer based. The key points of the decryption
are (1) calculating the counter and (2) padding through stream position:
counter = base + pos/(algorithm blocksize); padding = pos%(algorithm blocksize);
The underlying stream offset is maintained as state.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.hadoop.fs.StreamCapabilities
StreamCapabilities.StreamCapability -
Field Summary
Fields inherited from class java.io.FilterInputStream
inFields inherited from interface org.apache.hadoop.fs.StreamCapabilities
ABORTABLE_STREAM, DROPBEHIND, HFLUSH, HSYNC, IOSTATISTICS, IOSTATISTICS_CONTEXT, PREADBYTEBUFFER, READAHEAD, READBYTEBUFFER, UNBUFFER, VECTOREDIO, VECTOREDIO_BUFFERS_SLICED -
Constructor Summary
ConstructorsConstructorDescriptionCryptoInputStream(InputStream in, CryptoCodec codec, byte[] key, byte[] iv) CryptoInputStream(InputStream in, CryptoCodec codec, int bufferSize, byte[] key, byte[] iv) CryptoInputStream(InputStream in, CryptoCodec codec, int bufferSize, byte[] key, byte[] iv, long streamOffset) -
Method Summary
Modifier and TypeMethodDescriptionintvoidclose()Return a statistics instance.longgetPos()Get underlying stream position.booleanhasCapability(String capability) Query the stream for a specific capability.booleanisOpen()voidmark(int readLimit) booleanintread()intread(byte[] b, int off, int len) Decryption is buffer based.intread(long position, byte[] buffer, int offset, int length) Positioned read.intread(long position, ByteBuffer buf) Positioned read usingByteBuffers.intread(ByteBuffer buf) ByteBuffer read.read(ByteBufferPool bufferPool, int maxLength, EnumSet<ReadOption> opts) Get a ByteBuffer containing file data.voidreadFully(long position, byte[] buffer) Read number of bytes equal to the length of the buffer, from a given position within a file.voidreadFully(long position, byte[] buffer, int offset, int length) Positioned read fully.voidreadFully(long position, ByteBuffer buf) Positioned readFully usingByteBuffers.voidreleaseBuffer(ByteBuffer buffer) Release a ByteBuffer which was created by the enhanced ByteBuffer read function.voidreset()voidseek(long pos) Seek to a position.booleanseekToNewSource(long targetPos) Seeks a different copy of the data.voidsetDropBehind(Boolean dropCache) Configure whether the stream should drop the cache.voidsetReadahead(Long readahead) Set the readahead on this stream.longskip(long n) Skip n bytesvoidunbuffer()Reduce the buffering.Methods inherited from class java.io.FilterInputStream
readMethods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferToMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.hadoop.fs.PositionedReadable
maxReadSizeForVectorReads, minSeekForVectorReads, readVectored, readVectored
-
Constructor Details
-
CryptoInputStream
public CryptoInputStream(InputStream in, CryptoCodec codec, int bufferSize, byte[] key, byte[] iv) throws IOException - Throws:
IOException
-
CryptoInputStream
public CryptoInputStream(InputStream in, CryptoCodec codec, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException - Throws:
IOException
-
CryptoInputStream
public CryptoInputStream(InputStream in, CryptoCodec codec, byte[] key, byte[] iv) throws IOException - Throws:
IOException
-
-
Method Details
-
getWrappedStream
-
read
Decryption is buffer based. If there is data inoutBuffer, then read it out of this buffer. If there is no data inoutBuffer, then read more from the underlying stream and do the decryption.- Overrides:
readin classFilterInputStream- Parameters:
b- the buffer into which the decrypted data is read.off- the buffer offset.len- the maximum number of decrypted data bytes to read.- Returns:
- int the total number of decrypted data bytes read into the buffer.
- Throws:
IOException- raised on errors performing I/O.
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterInputStream- Throws:
IOException
-
read
Positioned read. It is thread-safe- Specified by:
readin interfacePositionedReadable- Parameters:
position- position within filebuffer- destination bufferoffset- offset in the bufferlength- number of bytes to read- Returns:
- actual number of bytes read; -1 means "none"
- Throws:
IOException- IO problems.
-
read
Positioned read usingByteBuffers. This method is thread-safe.- Specified by:
readin interfaceByteBufferPositionedReadable- Parameters:
position- position within filebuf- the ByteBuffer to receive the results of the read operation.- Returns:
- the number of bytes read, possibly zero, or -1 if reached end-of-stream
- Throws:
IOException- if there is some error performing the read
-
readFully
Positioned readFully usingByteBuffers. This method is thread-safe.- Specified by:
readFullyin interfaceByteBufferPositionedReadable- Parameters:
position- position within filebuf- the ByteBuffer to receive the results of the read operation.- Throws:
IOException- if there is some error performing the readEOFException- the end of the data was reached before the read operation completed- See Also:
-
readFully
Positioned read fully. It is thread-safe- Specified by:
readFullyin interfacePositionedReadable- Parameters:
position- position within filebuffer- destination bufferoffset- offset in the bufferlength- number of bytes to read- Throws:
IOException- IO problems.EOFException- the end of the data was reached before the read operation completed
-
readFully
Description copied from interface:PositionedReadableRead number of bytes equal to the length of the buffer, from a given position within a file. This does not change the current offset of a file, and is thread-safe. Warning: Not all filesystems satisfy the thread-safety requirement.- Specified by:
readFullyin interfacePositionedReadable- Parameters:
position- position within filebuffer- destination buffer- Throws:
IOException- IO problems.EOFException- the end of the data was reached before the read operation completed
-
seek
Seek to a position.- Specified by:
seekin interfaceSeekable- Parameters:
pos- offset from the start of the file.- Throws:
IOException- raised on errors performing I/O.
-
skip
Skip n bytes- Overrides:
skipin classFilterInputStream- Throws:
IOException
-
getPos
Get underlying stream position.- Specified by:
getPosin interfaceSeekable- Returns:
- offset from the start of the file.
- Throws:
IOException- raised on errors performing I/O.
-
read
ByteBuffer read.- Specified by:
readin interfaceByteBufferReadable- Specified by:
readin interfaceReadableByteChannel- Parameters:
buf- the ByteBuffer to receive the results of the read operation.- Returns:
- the number of bytes read, possibly zero, or -1 if reach end-of-stream
- Throws:
IOException- if there is some error performing the read
-
available
- Overrides:
availablein classFilterInputStream- Throws:
IOException
-
markSupported
public boolean markSupported()- Overrides:
markSupportedin classFilterInputStream
-
mark
public void mark(int readLimit) - Overrides:
markin classFilterInputStream
-
reset
- Overrides:
resetin classFilterInputStream- Throws:
IOException
-
seekToNewSource
Description copied from interface:SeekableSeeks a different copy of the data. Returns true if found a new source, false otherwise.- Specified by:
seekToNewSourcein interfaceSeekable- Parameters:
targetPos- target position.- Returns:
- true if found a new source, false otherwise.
- Throws:
IOException- raised on errors performing I/O.
-
read
public ByteBuffer read(ByteBufferPool bufferPool, int maxLength, EnumSet<ReadOption> opts) throws IOException, UnsupportedOperationException Description copied from interface:HasEnhancedByteBufferAccessGet a ByteBuffer containing file data. This ByteBuffer may come from the stream itself, via a call like mmap, or it may come from the ByteBufferFactory which is passed in as an argument.- Specified by:
readin interfaceHasEnhancedByteBufferAccess- Parameters:
bufferPool- If this is non-null, it will be used to create a fallback ByteBuffer when the stream itself cannot create one.maxLength- The maximum length of buffer to return. We may return a buffer which is shorter than this.opts- Options to use when reading.- Returns:
- We will always return an empty buffer if maxLength was 0, whether or not we are at EOF. If maxLength > 0, we will return null if the stream has reached EOF. Otherwise, we will return a ByteBuffer containing at least one byte. You must free this ByteBuffer when you are done with it by calling releaseBuffer on it. The buffer will continue to be readable until it is released in this manner. However, the input stream's close method may warn about unclosed buffers.
- Throws:
IOException- if there was an error reading.UnsupportedOperationException- if factory was null, and we needed an external byte buffer.
-
releaseBuffer
Description copied from interface:HasEnhancedByteBufferAccessRelease a ByteBuffer which was created by the enhanced ByteBuffer read function. You must not continue using the ByteBuffer after calling this function.- Specified by:
releaseBufferin interfaceHasEnhancedByteBufferAccess- Parameters:
buffer- The ByteBuffer to release.
-
setReadahead
Description copied from interface:CanSetReadaheadSet the readahead on this stream.- Specified by:
setReadaheadin interfaceCanSetReadahead- Parameters:
readahead- The readahead to use. null means to use the default.- Throws:
IOException- If there was an error changing the dropBehind setting. UnsupportedOperationException If this stream doesn't support setting readahead.UnsupportedOperationException
-
setDropBehind
Description copied from interface:CanSetDropBehindConfigure whether the stream should drop the cache.- Specified by:
setDropBehindin interfaceCanSetDropBehind- Parameters:
dropCache- Whether to drop the cache. null means to use the default value.- Throws:
IOException- If there was an error changing the dropBehind setting. UnsupportedOperationException If this stream doesn't support setting the drop-behind.UnsupportedOperationException
-
getFileDescriptor
- Specified by:
getFileDescriptorin interfaceHasFileDescriptor- Returns:
- the FileDescriptor
- Throws:
IOException- raised on errors performing I/O.
-
read
- Overrides:
readin classFilterInputStream- Throws:
IOException
-
isOpen
public boolean isOpen() -
unbuffer
public void unbuffer()Description copied from interface:CanUnbufferReduce the buffering. This will also free sockets and file descriptors held by the stream, if possible.- Specified by:
unbufferin interfaceCanUnbuffer
-
hasCapability
Description copied from interface:StreamCapabilitiesQuery the stream for a specific capability.- Specified by:
hasCapabilityin interfaceStreamCapabilities- Parameters:
capability- string to query the stream support for.- Returns:
- True if the stream supports capability.
-
getIOStatistics
Description copied from interface:IOStatisticsSourceReturn a statistics instance.It is not a requirement that the same instance is returned every time.
IOStatisticsSource.If the object implementing this is Closeable, this method may return null if invoked on a closed object, even if it returns a valid instance when called earlier.
- Specified by:
getIOStatisticsin interfaceIOStatisticsSource- Returns:
- an IOStatistics instance or null
-