|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectariba.util.core.FileReplacer
public class FileReplacer
Utility class that supports replacing a given base file
with a new file (typically, a new version of that file.)
OutputStream
to write a changed version of the file, applying the
changes, then reverting the changes (you can apply
and revert changes as many times as desired) before finally
committing either the applied or reverted changes
(dropping the files used for temporary storage.)
The temporary files are stored in the same directory as the base file.
File.rename() is used to apply and revert which is an
attempt to make the file replacement happen as quickly as possible.
Important note: This class is in no way multi-thread safe.
| Nested Class Summary | |
|---|---|
static class |
FileReplacer.BadStateException
|
| Constructor Summary | |
|---|---|
FileReplacer(File baseFile)
|
|
FileReplacer(File directory,
String baseFileName)
|
|
| Method Summary | |
|---|---|
void |
applyChanges()
Closes the stream opened in getOutputStream() and renames
the temporary file to realms.xml (after making a backup
copy of it). |
void |
checkGoodState()
|
void |
commit()
Drops the temporary and backup files associated with this FileReplacer thereby making the changes finally durable. |
String |
getBadStateMessage()
|
File |
getBaseFile()
Returns the file on which this replacer is based. |
FileInputStream |
getBaseFileInputStream()
Returns a FileInputStream on the base file. |
OutputStream |
getOutputStream()
Returns the output stream associated with the temporary file. |
FileInputStream |
getTempFileInputStream()
Returns a FileInputStream on the temp file. |
FilenameFilter |
getTempFilenameFilter()
Returns a FilenameFilter object that can be used
for filtering the temporary files that are created by
FileReplacer pattern. |
static FilenameFilter |
getTempFilenameFilter(File file)
Returns a FilenameFilter object that can be used
for filtering the temporary files that are created by
FileReplacer pattern. |
boolean |
hasAppliedChanges()
Returns true if pending changes have been applied to
the base file. |
boolean |
hasNoChanges()
Returns true if no changes have been made to the
base file (either pending or applied) and false
otherwise. |
boolean |
hasPendingChanges()
Returns true if there are any pending changes to be
made to the base file. |
boolean |
isInGoodState()
Returns whether or not this is in a valid state. |
OutputStream |
makeOutputStream()
Closes the existing output stream on the temporary file (if any) creates a new output stream and returns it. |
void |
repairFromBadState()
|
void |
revertChanges()
Closes the stream opened in getOutputStream() and
deletes the temporary file. |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public FileReplacer(File directory,
String baseFileName)
directory - the directory in which to create the temporary filebaseFileName - the name of the filepublic FileReplacer(File baseFile)
| Method Detail |
|---|
public boolean hasNoChanges()
true if no changes have been made to the
base file (either pending or applied) and false
otherwise.
public boolean hasPendingChanges()
true if there are any pending changes to be
made to the base file.
public boolean hasAppliedChanges()
true if pending changes have been applied to
the base file.
public File getBaseFile()
public FileInputStream getBaseFileInputStream()
throws IOException
FileInputStream on the base file.
Note that a reference is retained to the returned file stream.
It will be forcibly closed when applyChanges() is
called.
IOException
public FileInputStream getTempFileInputStream()
throws IOException
FileInputStream on the temp file.
The temp file must exist--it is only created when this
hasPendingChanges().
Note that a reference is retained to the returned file stream.
It will be forcibly closed when applyChanges() is
called.
IOException
public OutputStream getOutputStream()
throws IOException
FileReplacer must
either have no changes or must
have pending changes when this
method is called. It is an error to try to obtain an output
stream if this has applied changes.
(If the client wishes to do this, he must first revert
the applied changes and then request the stream.
It is the client's responsibility to close the stream.
getOutputStream in interface OutputStreamHolderIOException - if an exception occurs while creating or opening
the stream
public OutputStream makeOutputStream()
throws IOException
IOException
public void applyChanges()
throws IOException,
FileReplacer.BadStateException
getOutputStream() and renames
the temporary file to realms.xml (after making a backup
copy of it).
IOException
FileReplacer.BadStateException
public void revertChanges()
throws IOException,
FileReplacer.BadStateException
getOutputStream() and
deletes the temporary file.
IOException
FileReplacer.BadStateExceptionpublic void commit()
FileReplacer thereby making the changes finally durable.
(There is no way to further apply or revert the changes.)
After this method is called this goes to the
hasNoChanges() state.
public String getBadStateMessage()
public boolean isInGoodState()
this is in a valid state.
There is one invalid state for objects of this class, which is the
state in which there exists a backup file (of the base file) and
a temporary file (containing the changes to base file.)
In the normal course this should not happen: either the changes are applied
and the base file is backed up (to allow a revert) or the
changes are not applied, the base file is unchanged and there's no
backup file.
However, during applyChanges() or revertChanges()
the base file is deleted and the temporary or backup file is
renamed to the base file, respectively. In either case it's possible
for the delete to succeed and the rename to fail leaving us in
this invalid state.
public void checkGoodState()
throws FileReplacer.BadStateException
FileReplacer.BadStateException
public void repairFromBadState()
throws IOException
IOExceptionpublic FilenameFilter getTempFilenameFilter()
FilenameFilter object that can be used
for filtering the temporary files that are created by
FileReplacer pattern.
public static FilenameFilter getTempFilenameFilter(File file)
FilenameFilter object that can be used
for filtering the temporary files that are created by
FileReplacer pattern.
file - the file that the filter pattern is based on.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||