public class FileMessageFactory
extends java.lang.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 java.io.File |
file
The file that we are reading/writing
|
protected java.io.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 java.util.concurrent.atomic.AtomicLong |
lastMessageProcessed
The number of the last message processed.
|
protected int |
maxValidTime
The maximum valid time(in seconds) from creationTime.
|
protected java.util.Map<java.lang.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 java.io.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
|
java.io.File |
getFile() |
static FileMessageFactory |
getInstance(java.io.File f,
boolean openForWrite)
Creates a factory to read or write from a file.
|
int |
getMaxValidTime() |
boolean |
isValid() |
static void |
main(java.lang.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 java.io.File file
protected final boolean openForWrite
protected boolean closed
protected java.io.FileInputStream in
protected java.io.FileOutputStream out
protected int nrOfMessagesProcessed
protected long size
protected long totalNrOfMessages
protected java.util.concurrent.atomic.AtomicLong lastMessageProcessed
protected final java.util.Map<java.lang.Long,FileMessage> msgBuffer
protected byte[] data
protected boolean isWriting
protected long creationTime
protected int maxValidTime
public static FileMessageFactory getInstance(java.io.File f, boolean openForWrite) throws java.io.FileNotFoundException, java.io.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 itjava.io.FileNotFoundException
- -
if the file to be read doesn't existjava.io.IOException
- -
if it fails to create the file that is to be writtenpublic FileMessage readMessage(FileMessage f) throws java.lang.IllegalArgumentException, java.io.IOException
f
- FileMessage - the message to be populated with file datajava.lang.IllegalArgumentException
- -
if the factory is for writing or is closedjava.io.IOException
- -
if a file read exception occurspublic boolean writeMessage(FileMessage msg) throws java.lang.IllegalArgumentException, java.io.IOException
msg
- FileMessage - message containing data to be writtenjava.lang.IllegalArgumentException
- -
if the factory is opened for read or closedjava.io.IOException
- -
if a file write error occurspublic void cleanup()
protected void checkState(boolean openForWrite) throws java.lang.IllegalArgumentException
openForWrite
- booleanjava.lang.IllegalArgumentException
public static void main(java.lang.String[] args) throws java.lang.Exception
args
- String[], args[0] - read from filename, args[1] write to
filenamejava.lang.Exception
public java.io.File getFile()
public boolean isValid()
public int getMaxValidTime()
public void setMaxValidTime(int maxValidTime)
Copyright © 2000-2018 Apache Software Foundation. All Rights Reserved.