Package org.apache.hadoop.crypto
Class CryptoOutputStream
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
org.apache.hadoop.crypto.CryptoOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable,CanSetDropBehind,IOStatisticsSource,StreamCapabilities,Syncable
@Private
@Evolving
public class CryptoOutputStream
extends FilterOutputStream
implements Syncable, CanSetDropBehind, StreamCapabilities, IOStatisticsSource
CryptoOutputStream encrypts 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 encryption is buffer based. The key points of the encryption are
(1) calculating counter and (2) padding through stream position.
counter = base + pos/(algorithm blocksize); padding = pos%(algorithm blocksize);
The underlying stream offset is maintained as state. Note that while some of this class' methods are synchronized, this is just to match the threadsafety behavior of DFSOutputStream. See HADOOP-11710.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.hadoop.fs.StreamCapabilities
StreamCapabilities.StreamCapability -
Field Summary
Fields inherited from class java.io.FilterOutputStream
outFields 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
ConstructorsConstructorDescriptionCryptoOutputStream(OutputStream out, CryptoCodec codec, byte[] key, byte[] iv) CryptoOutputStream(OutputStream out, CryptoCodec codec, byte[] key, byte[] iv, long streamOffset) CryptoOutputStream(OutputStream out, CryptoCodec codec, byte[] key, byte[] iv, long streamOffset, boolean closeOutputStream) CryptoOutputStream(OutputStream out, CryptoCodec codec, int bufferSize, byte[] key, byte[] iv) CryptoOutputStream(OutputStream out, CryptoCodec codec, int bufferSize, byte[] key, byte[] iv, long streamOffset) CryptoOutputStream(OutputStream out, CryptoCodec codec, int bufferSize, byte[] key, byte[] iv, long streamOffset, boolean closeOutputStream) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidflush()To flush, we need to encrypt the data in the buffer and write to the underlying stream, then do the flush.Return a statistics instance.booleanhasCapability(String capability) Query the stream for a specific capability.voidhflush()Flush out the data in client's user buffer.voidhsync()Similar to posix fsync, flush out the data in client's user buffer all the way to the disk device (but the disk may have it in its cache).voidsetDropBehind(Boolean dropCache) Configure whether the stream should drop the cache.voidwrite(byte[] b, int off, int len) Encryption is buffer based.voidwrite(int b) Methods inherited from class java.io.FilterOutputStream
writeMethods inherited from class java.io.OutputStream
nullOutputStream
-
Constructor Details
-
CryptoOutputStream
public CryptoOutputStream(OutputStream out, CryptoCodec codec, int bufferSize, byte[] key, byte[] iv) throws IOException - Throws:
IOException
-
CryptoOutputStream
public CryptoOutputStream(OutputStream out, CryptoCodec codec, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException - Throws:
IOException
-
CryptoOutputStream
public CryptoOutputStream(OutputStream out, CryptoCodec codec, int bufferSize, byte[] key, byte[] iv, long streamOffset, boolean closeOutputStream) throws IOException - Throws:
IOException
-
CryptoOutputStream
public CryptoOutputStream(OutputStream out, CryptoCodec codec, byte[] key, byte[] iv) throws IOException - Throws:
IOException
-
CryptoOutputStream
public CryptoOutputStream(OutputStream out, CryptoCodec codec, byte[] key, byte[] iv, long streamOffset) throws IOException - Throws:
IOException
-
CryptoOutputStream
public CryptoOutputStream(OutputStream out, CryptoCodec codec, byte[] key, byte[] iv, long streamOffset, boolean closeOutputStream) throws IOException - Throws:
IOException
-
-
Method Details
-
getWrappedStream
-
write
Encryption is buffer based. If there is enough room ininBuffer, then write to this buffer. IfinBufferis full, then do encryption and write data to the underlying stream.- Overrides:
writein classFilterOutputStream- Parameters:
b- the data.off- the start offset in the data.len- the number of bytes to write.- Throws:
IOException- raised on errors performing I/O.
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterOutputStream- Throws:
IOException
-
flush
To flush, we need to encrypt the data in the buffer and write to the underlying stream, then do the flush.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classFilterOutputStream- Throws:
IOException
-
write
- Overrides:
writein classFilterOutputStream- Throws:
IOException
-
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
-
hflush
Description copied from interface:SyncableFlush out the data in client's user buffer. After the return of this call, new readers will see the data.- Specified by:
hflushin interfaceSyncable- Throws:
IOException- if any error occurs
-
hsync
Description copied from interface:SyncableSimilar to posix fsync, flush out the data in client's user buffer all the way to the disk device (but the disk may have it in its cache).- Specified by:
hsyncin interfaceSyncable- Throws:
IOException- if error occurs
-
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
-