|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.io.OutputStream
ccs.utils.SwappingBuffer
public class SwappingBuffer
A dual-mode buffer that efficiently stores an arbitrary amount of transient data. For small amounts, it uses memory. However, once the buffer size passes a threshold, it automatically copies this to a disk file and uses that from then on. The disk file is encrypted with a throw-away key; in this way if the app crashes before it has wiped any disk file, there is no security breach.
A SwappingBuffer is strictly a "single-shot" object. It has 4 lifecycle stages: writable, unwritable, read-committed and dead. It proceeds through each of these stages, in the above order:
close
makes the buffer unwritable. No further writes
are allowed.getInputStream
or getByteArray
may be called from
either the writable or unwritable stage, but not read-committed or dead. They
make the buffer read-committed. No further writes are allowed, and neither
of the above methods may be called again.writeTo
may be called from either the writable
or unwritable stages, and takes the buffer straight to stage "dead".These restrictive rules prevent the buffer from having to duplicate large chunks of memory, which is inefficient.
This class is MT-UNSAFE, and must not be accessible by multiple threads.
Any app which uses a SwappingBuffer MUST call FileKiller.waitForCompletion
as part of its shutdown process.
Nested Class Summary | |
---|---|
protected static class |
SwappingBuffer.DestructiveBAIS
|
protected static class |
SwappingBuffer.PeekableBAOS
|
Field Summary | |
---|---|
protected SwappingBuffer.PeekableBAOS |
baos
|
protected BCCHBlockCipher |
cipher
|
protected CipherOutputStream |
cos
|
static int |
DEAD
Lifecycle stage. |
static int |
DEFAULTSWAP
The default buffer length after which the buffer is swapped to disk; 512K. |
protected java.io.File |
f
|
protected boolean |
isFile
|
java.lang.String |
mimeType
The MIME type of the content of this buffer. |
protected int |
phase
|
static int |
READCOMMITTED
Lifecycle stage. |
static int |
UNWRITABLE
Lifecycle stage. |
protected byte[] |
wad
|
static int |
WRITABLE
Lifecycle stage. |
Constructor Summary | |
---|---|
SwappingBuffer()
Creates a new SwappingBuffer with a default threshold (512K) and default auto-destruct. |
|
SwappingBuffer(byte[] contents)
Creates a new "pre-filled" SwappingBuffer whose contents are the supplied buffer. |
|
SwappingBuffer(java.io.InputStream srcIS,
long total)
Creates a new "pre-filled" SwappingBuffer which wraps the supplied InputStream. |
|
SwappingBuffer(int threshold)
Creates a new SwappingBuffer with the supplied threshold and default auto-destruct. |
|
SwappingBuffer(int threshold,
int autoDestruct)
Creates a new SwappingBuffer with the supplied threshold and auto-destruct level. |
Method Summary | |
---|---|
void |
close()
Closes the output stream and releases any system resources associated with it. |
protected void |
finalize()
|
void |
flush()
Attempts to flush the SwappingBuffer. |
byte[] |
getByteArray()
Returns the contents of the buffer as a byte array. |
java.io.File |
getFile()
Returns the swap file, if one is in use, otherwise null. |
java.lang.String |
getFilePath()
Returns the path to the swap file, if one is in use, otherwise null. |
java.io.InputStream |
getInputStream()
Returns a buffered input stream which reads from this buffer. |
int |
getLifecycleStage()
Returns the current lifecycle stage of the SwappingBuffer: this is one of WRITABLE, UNWRITABLE, READCOMMITTED or DEAD. |
boolean |
isWritable()
Whether the SwappingBuffer is still writable. |
void |
randomRead(byte[] wad,
int woff,
int len,
long boff)
Copies a part of the buffer into the supplied byte array. |
long |
size()
returns the current length of data in the buffer. |
void |
write(byte[] b,
int off,
int len)
Writes len bytes from the specified byte array
starting at offset off to the output stream. |
void |
write(int b)
Writes the specified byte to the output stream. |
void |
writeTo(java.io.OutputStream os)
Writes the contents of the buffer to the supplied stream. |
Methods inherited from class java.io.OutputStream |
---|
write |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int DEFAULTSWAP
public static final int WRITABLE
public static final int UNWRITABLE
public static final int READCOMMITTED
public static final int DEAD
public java.lang.String mimeType
protected SwappingBuffer.PeekableBAOS baos
protected CipherOutputStream cos
protected java.io.File f
protected byte[] wad
protected boolean isFile
protected int phase
protected BCCHBlockCipher cipher
Constructor Detail |
---|
public SwappingBuffer()
public SwappingBuffer(int threshold)
public SwappingBuffer(int threshold, int autoDestruct)
threshold
- The threshold at which to swap to disk.autoDestruct
- The auto-destruct level for disk files; a constant from
FileKiller
.public SwappingBuffer(byte[] contents)
public SwappingBuffer(java.io.InputStream srcIS, long total)
srcIS
- The InputStream to wrap.total
- The amount of data available on the stream. It is the caller's
responsibility to get this right: dummy values are not acceptable. Use
a DammedInputStream
if necessary.Method Detail |
---|
public java.io.InputStream getInputStream() throws java.io.IOException
java.io.IOException
- if there is a problem during the process.public byte[] getByteArray()
public void randomRead(byte[] wad, int woff, int len, long boff) throws java.io.IOException
boff
- the encryption means that all
of the file before the start of the buffer must be read and then discarded.
If the buffer wraps an InputStream, this call will throw.
wad
- The array to copy into.woff
- The offset in wad whither to start copying.len
- The number of bytes to copy.boff
- The offset in the main buffer whence to start copying.
java.io.IOException
- If the buffer is swapped to disk, and the disk access fails.public void writeTo(java.io.OutputStream os) throws java.io.IOException
os
- The stream to write to.
java.io.IOException
- if the call failed.public int getLifecycleStage()
public boolean isWritable()
public java.lang.String getFilePath()
public java.io.File getFile()
public void write(int b) throws java.io.IOException
write
in class java.io.OutputStream
b
- The byte.
java.io.IOException
- if the write fails.public void write(byte[] b, int off, int len) throws java.io.IOException
len
bytes from the specified byte array
starting at offset off
to the output stream.
write
in class java.io.OutputStream
b
- The array.off
- The offsetlen
- The length.
java.io.IOException
- if the write fails.public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.OutputStream
java.io.IOException
- if the flush fails.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in class java.io.OutputStream
java.io.IOException
- if the close fails.public long size()
protected void finalize() throws java.io.IOException
finalize
in class java.lang.Object
java.io.IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |