public class FileMessageFactory extends Object
Modifier and Type | Field and Description |
---|---|
protected boolean |
closed
Once the factory is used, it can not be reused.
|
protected long |
creationTime
The time this instance was created.
|
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 int |
maxValidTime
The maximum valid time(in seconds) from creationTime.
|
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
|
Modifier and Type | Method and Description |
---|---|
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.
|
int |
getMaxValidTime() |
boolean |
isValid() |
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. |
void |
setMaxValidTime(int maxValidTime) |
boolean |
writeMessage(FileMessage msg)
Writes a message to file.
|
public static final int READ_SIZE
protected final File file
protected final boolean openForWrite
protected boolean closed
protected FileInputStream in
protected FileOutputStream out
protected int nrOfMessagesProcessed
protected long size
protected long totalNrOfMessages
protected AtomicLong lastMessageProcessed
protected final Map<Long,FileMessage> msgBuffer
protected byte[] data
protected boolean isWriting
protected long creationTime
protected int maxValidTime
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 itFileNotFoundException
- -
if the file to be read doesn't existIOException
- -
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 dataIllegalArgumentException
- -
if the factory is for writing or is closedIOException
- -
if a file read exception occurspublic boolean writeMessage(FileMessage msg) throws IllegalArgumentException, IOException
msg
- FileMessage - message containing data to be writtenIllegalArgumentException
- -
if the factory is opened for read or closedIOException
- -
if a file write error occurspublic void cleanup()
protected void checkState(boolean openForWrite) throws IllegalArgumentException
openForWrite
- booleanIllegalArgumentException
public static void main(String[] args) throws Exception
args
- String[], args[0] - read from filename, args[1] write to
filenameException
public File getFile()
public boolean isValid()
public int getMaxValidTime()
public void setMaxValidTime(int maxValidTime)
Copyright © 2000-2014 Apache Software Foundation. All Rights Reserved.