|
Apache Tomcat 7.0.28 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.catalina.ha.deploy.FileMessageFactory
public class FileMessageFactory
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.
Field Summary | |
---|---|
protected boolean |
closed
Once the factory is used, it can not be reused. |
protected byte[] |
data
The bytes that we hold the data in, not thread safe. |
protected File |
file
The file that we are reading/writing |
protected FileInputStream |
in
When openForWrite=false, the input stream is held by this variable |
protected boolean |
isWriting
Flag that indicates if a thread is writing messages to disk. |
protected AtomicLong |
lastMessageProcessed
The number of the last message processed. |
protected Map<Long,FileMessage> |
msgBuffer
Messages received out of order are held in the buffer until required. |
protected int |
nrOfMessagesProcessed
The number of messages we have written |
protected boolean |
openForWrite
True means that we are writing with this factory. |
protected FileOutputStream |
out
When openForWrite=true, the output stream is held by this variable |
static int |
READ_SIZE
The number of bytes that we read from file |
protected long |
size
The total size of the file |
protected long |
totalNrOfMessages
The total number of packets that we split this file into |
Method Summary | |
---|---|
protected void |
checkState(boolean openForWrite)
Check to make sure the factory is able to perform the function it is asked to do. |
void |
cleanup()
Closes the factory, its streams and sets all its references to null |
File |
getFile()
|
static FileMessageFactory |
getInstance(File f,
boolean openForWrite)
Creates a factory to read or write from a file. |
static void |
main(String[] args)
Example usage. |
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. |
boolean |
writeMessage(FileMessage msg)
Writes a message to file. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int READ_SIZE
protected File file
protected boolean openForWrite
protected boolean closed
protected FileInputStream in
protected FileOutputStream out
protected int nrOfMessagesProcessed
protected long size
protected long totalNrOfMessages
protected AtomicLong lastMessageProcessed
protected Map<Long,FileMessage> msgBuffer
protected byte[] data
protected boolean isWriting
Method Detail |
---|
public static FileMessageFactory getInstance(File f, boolean openForWrite) throws FileNotFoundException, IOException
f
- File - the file to be read or writtenopenForWrite
- boolean - true, means we are writing to the file, false means
we are reading from it
FileNotFoundException
- -
if the file to be read doesn't exist
IOException
- -
if it fails to create the file that is to be writtenpublic FileMessage readMessage(FileMessage f) throws IllegalArgumentException, IOException
f
- FileMessage - the message to be populated with file data
IllegalArgumentException
- -
if the factory is for writing or is closed
IOException
- -
if a file read exception occurspublic boolean writeMessage(FileMessage msg) throws IllegalArgumentException, IOException
msg
- FileMessage - message containing data to be written
IllegalArgumentException
- -
if the factory is opened for read or closed
IOException
- -
if a file write error occurspublic void cleanup()
protected void checkState(boolean openForWrite) throws IllegalArgumentException
openForWrite
- boolean
IllegalArgumentException
public static void main(String[] args) throws Exception
args
- String[], args[0] - read from filename, args[1] write to
filename
Exception
public File getFile()
|
Apache Tomcat 7.0.28 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |