| java.lang.Object org.apache.catalina.cluster.deploy.FileMessageFactory
FileMessageFactory | public class FileMessageFactory (Code) | | This factory is used to read files and write files by splitting them up into smaller
messages. So that entire files don't have to be read into memory.
The factory can be used as a reader or writer but not both at the same time.
When done reading or writing the factory will close the input or output streams
and mark the factory as closed. It is not possible to use it after that.
To force a cleanup, call cleanup() from the calling object.
This class is not thread safe.
author: Filip Hanik version: 1.0 |
Method Summary | |
protected void | checkState(boolean openForWrite) Check to make sure the factory is able to perform the
function it is asked to do. | public void | cleanup() | public File | getFile() | public static FileMessageFactory | getInstance(File f, boolean openForWrite) Creates a factory to read or write from a file. | public static void | main(String[] args) Example usage. | public FileMessage | readMessage(FileMessage f) Reads file data into the file message and sets the
size, totalLength, totalNrOfMsgs and the message number
If EOF is reached, the factory returns null, and closes itself,
otherwise the same message is returned as was passed in.
This makes sure that not more memory is ever used. | public boolean | writeMessage(FileMessage msg) Writes a message to file. |
READ_SIZE | final public static int READ_SIZE(Code) | | The number of bytes that we read from file
|
closed | protected boolean closed(Code) | | Once the factory is used, it can not be reused.
|
data | protected byte[] data(Code) | | The bytes that we hold the data in, not thread safe.
|
file | protected File file(Code) | | The file that we are reading/writing
|
nrOfMessagesProcessed | protected int nrOfMessagesProcessed(Code) | | The number of messages we have read or written
|
openForWrite | protected boolean openForWrite(Code) | | True means that we are writing with this factory.
False means that we are reading with this factory
|
size | protected long size(Code) | | The total size of the file
|
totalNrOfMessages | protected long totalNrOfMessages(Code) | | The total number of packets that we split this file into
|
checkState | protected void checkState(boolean openForWrite) throws IllegalArgumentException(Code) | | Check to make sure the factory is able to perform the
function it is asked to do. Invoked by readMessage/writeMessage before
those methods proceed.
Parameters: openForWrite - boolean throws: IllegalArgumentException - |
cleanup | public void cleanup()(Code) | | Closes the factory, its streams and sets all its references to null
|
getInstance | public static FileMessageFactory getInstance(File f, boolean openForWrite) throws FileNotFoundException, IOException(Code) | | Creates a factory to read or write from a file.
When opening for read, the readMessage can be invoked, and when
opening for write the writeMessage can be invoked.
Parameters: f - File - the file to be read or written Parameters: openForWrite - boolean - true, means we are writing to the file, false means we are reading from it throws: FileNotFoundException - - if the file to be read doesn't exist throws: IOException - - if it fails to create the file that is to be written FileMessageFactory |
main | public static void main(String[] args) throws Exception(Code) | | Example usage.
Parameters: args - String[], args[0] - read from filename, args[1] write to filename throws: Exception - |
readMessage | public FileMessage readMessage(FileMessage f) throws IllegalArgumentException, IOException(Code) | | Reads file data into the file message and sets the
size, totalLength, totalNrOfMsgs and the message number
If EOF is reached, the factory returns null, and closes itself,
otherwise the same message is returned as was passed in.
This makes sure that not more memory is ever used.
To remember, neither the file message or the factory are thread safe.
dont hand off the message to one thread and read the same with another.
Parameters: f - FileMessage - the message to be populated with file data throws: IllegalArgumentException - - if the factory is for writing or is closed throws: IOException - - if a file read exception occurs FileMessage - returns the same message passed in as a parameter, or null if EOF |
writeMessage | public boolean writeMessage(FileMessage msg) throws IllegalArgumentException, IOException(Code) | | Writes a message to file. If (msg.getMessageNumber() == msg.getTotalNrOfMsgs())
the output stream will be closed after writing.
Parameters: msg - FileMessage - message containing data to be written throws: IllegalArgumentException - - if the factory is opened for read or closed throws: IOException - - if a file write error occurs returns true if the file is complete and outputstream is closed, false otherwise. |
|
|