FileMessageFactorypublic class FileMessageFactory extends Object 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. |
Fields Summary |
---|
public static org.apache.juli.logging.Log | log | public static final int | READ_SIZEThe number of bytes that we read from file | protected File | fileThe file that we are reading/writing | protected boolean | openForWriteTrue means that we are writing with this factory. False means that we are
reading with this factory | protected boolean | closedOnce the factory is used, it can not be reused. | protected FileInputStream | inWhen openForWrite=false, the input stream is held by this variable | protected FileOutputStream | outWhen openForWrite=true, the output stream is held by this variable | protected int | nrOfMessagesProcessedThe number of messages we have read or written | protected long | sizeThe total size of the file | protected long | totalNrOfMessagesThe total number of packets that we split this file into | protected byte[] | dataThe bytes that we hold the data in, not thread safe. |
Constructors Summary |
---|
private FileMessageFactory(File f, boolean openForWrite)Private constructor, either instantiates a factory to read or write.
When openForWrite==true, then a the file, f, will be created and an
output stream is opened to write to it.
When openForWrite==false, an input stream is opened, the file has to
exist.
this.file = f;
this.openForWrite = openForWrite;
if (log.isDebugEnabled())
log.debug("open file " + f + " write " + openForWrite);
if (openForWrite) {
if (!file.exists())
file.createNewFile();
out = new FileOutputStream(f);
} else {
size = file.length();
totalNrOfMessages = (size / READ_SIZE) + 1;
in = new FileInputStream(f);
}//end if
|
Methods Summary |
---|
protected void | checkState(boolean openForWrite)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.
if (this.openForWrite != openForWrite) {
cleanup();
if (openForWrite)
throw new IllegalArgumentException(
"Can't write message, this factory is reading.");
else
throw new IllegalArgumentException(
"Can't read message, this factory is writing.");
}
if (this.closed) {
cleanup();
throw new IllegalArgumentException("Factory has been closed.");
}
| public void | cleanup()Closes the factory, its streams and sets all its references to null
if (in != null)
try {
in.close();
} catch (Exception ignore) {
}
if (out != null)
try {
out.close();
} catch (Exception ignore) {
}
in = null;
out = null;
size = 0;
closed = true;
data = null;
nrOfMessagesProcessed = 0;
totalNrOfMessages = 0;
| public java.io.File | getFile()
return file;
| public static org.apache.catalina.ha.deploy.FileMessageFactory | getInstance(java.io.File f, boolean openForWrite)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.
return new FileMessageFactory(f, openForWrite);
| public static void | main(java.lang.String[] args)Example usage.
System.out
.println("Usage: FileMessageFactory fileToBeRead fileToBeWritten");
System.out
.println("Usage: This will make a copy of the file on the local file system");
FileMessageFactory read = getInstance(new File(args[0]), false);
FileMessageFactory write = getInstance(new File(args[1]), true);
FileMessage msg = new FileMessage(null, args[0], args[0]);
msg = read.readMessage(msg);
System.out.println("Expecting to write " + msg.getTotalNrOfMsgs()
+ " messages.");
int cnt = 0;
while (msg != null) {
write.writeMessage(msg);
cnt++;
msg = read.readMessage(msg);
}//while
System.out.println("Actually wrote " + cnt + " messages.");
| 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. 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.
checkState(false);
int length = in.read(data);
if (length == -1) {
cleanup();
return null;
} else {
f.setData(data, length);
f.setTotalLength(size);
f.setTotalNrOfMsgs(totalNrOfMessages);
f.setMessageNumber(++nrOfMessagesProcessed);
return f;
}//end if
| public boolean | writeMessage(FileMessage msg)Writes a message to file. If (msg.getMessageNumber() ==
msg.getTotalNrOfMsgs()) the output stream will be closed after writing.
if (!openForWrite)
throw new IllegalArgumentException(
"Can't write message, this factory is reading.");
if (log.isDebugEnabled())
log.debug("Message " + msg + " data " + msg.getData()
+ " data length " + msg.getDataLength() + " out " + out);
if (out != null) {
out.write(msg.getData(), 0, msg.getDataLength());
nrOfMessagesProcessed++;
out.flush();
if (msg.getMessageNumber() == msg.getTotalNrOfMsgs()) {
out.close();
cleanup();
return true;
}//end if
} else {
if (log.isWarnEnabled())
log.warn("Receive Message again -- Sender ActTimeout to short [ path: "
+ msg.getContextPath()
+ " war: "
+ msg.getFileName()
+ " data: "
+ msg.getData()
+ " data length: " + msg.getDataLength() + " ]");
}
return false;
|
|