public class FileStreamStorage extends StreamStorage
A configurable StreamStorage that:
dispose() methodInputStream returned by the getInputStream() method is closed. The FileStreamStorage has two distinct states:
StreamStorage is ONLY writable and NOT readable.StreamStorage is ONLY readable and NOT writable. A new instance will always start in a write state, ready to accept bytes and any call to the getInputStream() will fail.
Once all the data has been written, the close() method needs to be called to close the write channel and switch the
FileStreamStorage to the read state. At that point the data can be read via getInputStream().
| Modifier | Constructor and Description |
|---|---|
protected |
FileStreamStorage(File file,
int threshold,
boolean append)
Constructor that sets the threshold to the user specified value.
|
protected |
FileStreamStorage(File file,
int threshold,
boolean deleteFilesOnClose,
boolean deleteFilesOnDispose,
boolean append,
long maxCapacity)
Constructor that sets the threshold to the user specified value.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
static FileStreamStorage |
deferred(File file,
int threshold)
Returns a reference to a
FileStreamStorage where the data is written to a file when the data is greater in
size than the threshold specified. |
FileStreamStorage |
deleteFilesOnClose()
Configures the current
FileStreamStorage to delete the underlying file after calling the close() method. |
FileStreamStorage |
deleteFilesOnDispose()
Configures the current
FileStreamStorage to delete the underlying file after calling the dispose() ()} method. |
static FileStreamStorage |
directToFile(File file,
boolean append)
Returns a reference to a
FileStreamStorage where the data is always written to the file specified. |
boolean |
dispose()
Dismisses the
DeferredFileStreamStorage closing quietly the OutputStream and deleting the underlying file if it exists. |
void |
flush() |
InputStream |
getInputStream()
Returns the
InputStream to read back data from the store. |
boolean |
isInMemory()
Returns if the data has been flushed to disk or if it's still in memory.
|
FileStreamStorage |
maxCapacity(long maxCapacity)
Configures the current
FileStreamStorage to accept up to maxCapacity bytes. |
void |
write(byte[] b) |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
protected FileStreamStorage(File file, int threshold, boolean deleteFilesOnClose, boolean deleteFilesOnDispose, boolean append, long maxCapacity)
Constructor that sets the threshold to the user specified value.
file - The file that will be used to store the data if the threshold is reached.threshold - The threshold in bytes. Data smaller than the threshold are kept in memory. If the threshold is reached, the data is flushed to disk.deleteFilesOnClose - boolean indicating whether the file should be deleted after been read.deleteFilesOnDispose - boolean indicating whether the file should be deleted after dismiss has been called.maxCapacity - The maximum amount of bytes that the storage can accept. A value of -1 indicates infinite, the value 0 or a value lower that -1 is not accepted.protected FileStreamStorage(File file, int threshold, boolean append)
Constructor that sets the threshold to the user specified value.
file - The file that will be used to store the data if the threshold is reached.threshold - The threshold in bytes. Data smaller than the threshold are kept in memory. If the threshold is reached, the data is flushed to disk.public static FileStreamStorage deferred(File file, int threshold)
Returns a reference to a FileStreamStorage where the data is written to a file when the data is greater in
size than the threshold specified.
file - The file that will be used to store the data if the threshold is reached.threshold - The threshold in bytes. Data smaller than the threshold are kept in memory. If the threshold is reached, the data is flushed to the file.public static FileStreamStorage directToFile(File file, boolean append)
Returns a reference to a FileStreamStorage where the data is always written to the file specified.
file - The file that will be used to store the data.append - boolean that indicates whether any existing data in the file is going to be appended to or overridden.public FileStreamStorage deleteFilesOnClose()
Configures the current FileStreamStorage to delete the underlying file after calling the close() method.
public FileStreamStorage deleteFilesOnDispose()
Configures the current FileStreamStorage to delete the underlying file after calling the dispose() ()} method.
public FileStreamStorage maxCapacity(long maxCapacity)
Configures the current FileStreamStorage to accept up to maxCapacity bytes.
If the threshold is exceeded the storage will throw an IllegalStateException
maxCapacity - The max capacity in bytes accepted by this storage. A value of -1 indicates infinite, the value 0 or a value lower that -1 is not accepted.public void write(int b)
throws IOException
write in class OutputStreamIOExceptionpublic void write(byte[] b,
int off,
int len)
throws IOException
write in class OutputStreamIOExceptionpublic void write(byte[] b)
throws IOException
write in class OutputStreamIOExceptionpublic void flush()
throws IOException
flush in interface Flushableflush in class OutputStreamIOExceptionpublic void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class OutputStreamIOExceptionpublic InputStream getInputStream()
Returns the InputStream to read back data from the store.
getInputStream in class StreamStorageInputStream to read back data from the store.public boolean isInMemory()
Returns if the data has been flushed to disk or if it's still in memory.
public boolean dispose()
Dismisses the DeferredFileStreamStorage closing quietly the OutputStream and deleting the underlying file if it exists.
This method is useful just in case of errors to free the resources and once called the DeferredFileStreamStorage is not usable anymore.
true if and only if the file was created and it has been deleted successfully; false otherwise.Copyright © 2017. All rights reserved.