LockableFileWriterpublic class LockableFileWriter extends Writer FileWriter that will create and honor lock files to allow simple
cross thread file lock handling.
This class provides a simple alternative to FileWriter
that will use a lock file to prevent duplicate writes.
By default, the file will be overwritten, but this may be changed to append.
The lock directory may be specified, but defaults to the system property
java.io.tmpdir .
The encoding may also be specified, and defaults to the platform default. |
Fields Summary |
---|
private static final String | LCKThe extension for the lock file. | private final Writer | outThe writer to decorate. | private final File | lockFileThe lock file. |
Constructors Summary |
---|
public LockableFileWriter(String fileName)Constructs a LockableFileWriter.
If the file exists, it is overwritten.
this(fileName, false, null);
| public LockableFileWriter(String fileName, boolean append)Constructs a LockableFileWriter.
this(fileName, append, null);
| public LockableFileWriter(String fileName, boolean append, String lockDir)Constructs a LockableFileWriter.
this(new File(fileName), append, lockDir);
| public LockableFileWriter(File file)Constructs a LockableFileWriter.
If the file exists, it is overwritten.
this(file, false, null);
| public LockableFileWriter(File file, boolean append)Constructs a LockableFileWriter.
this(file, append, null);
| public LockableFileWriter(File file, boolean append, String lockDir)Constructs a LockableFileWriter.
this(file, null, append, lockDir);
| public LockableFileWriter(File file, String encoding)Constructs a LockableFileWriter with a file encoding.
this(file, encoding, false, null);
| public LockableFileWriter(File file, String encoding, boolean append, String lockDir)Constructs a LockableFileWriter with a file encoding.
super();
// init file to create/append
file = file.getAbsoluteFile();
if (file.getParentFile() != null) {
FileUtils.forceMkdir(file.getParentFile());
}
if (file.isDirectory()) {
throw new IOException("File specified is a directory");
}
// init lock file
if (lockDir == null) {
lockDir = System.getProperty("java.io.tmpdir");
}
File lockDirFile = new File(lockDir);
FileUtils.forceMkdir(lockDirFile);
testLockDir(lockDirFile);
lockFile = new File(lockDirFile, file.getName() + LCK);
// check if locked
createLock();
// init wrapped writer
out = initWriter(file, encoding, append);
|
Methods Summary |
---|
public void | close()Closes the file writer.
try {
out.close();
} finally {
lockFile.delete();
}
| private void | createLock()Creates the lock file.
synchronized (LockableFileWriter.class) {
if (!lockFile.createNewFile()) {
throw new IOException("Can't write file, lock " +
lockFile.getAbsolutePath() + " exists");
}
lockFile.deleteOnExit();
}
| public void | flush()Flush the stream.
out.flush();
| private java.io.Writer | initWriter(java.io.File file, java.lang.String encoding, boolean append)Initialise the wrapped file writer.
Ensure that a cleanup occurs if the writer creation fails.
boolean fileExistedAlready = file.exists();
OutputStream stream = null;
Writer writer = null;
try {
if (encoding == null) {
writer = new FileWriter(file.getAbsolutePath(), append);
} else {
stream = new FileOutputStream(file.getAbsolutePath(), append);
writer = new OutputStreamWriter(stream, encoding);
}
} catch (IOException ex) {
IOUtils.closeQuietly(writer);
IOUtils.closeQuietly(stream);
lockFile.delete();
if (fileExistedAlready == false) {
file.delete();
}
throw ex;
} catch (RuntimeException ex) {
IOUtils.closeQuietly(writer);
IOUtils.closeQuietly(stream);
lockFile.delete();
if (fileExistedAlready == false) {
file.delete();
}
throw ex;
}
return writer;
| private void | testLockDir(java.io.File lockDir)Tests that we can write to the lock directory.
if (!lockDir.exists()) {
throw new IOException(
"Could not find lockDir: " + lockDir.getAbsolutePath());
}
if (!lockDir.canWrite()) {
throw new IOException(
"Could not write to lockDir: " + lockDir.getAbsolutePath());
}
| public void | write(int idx)Write a character.
out.write(idx);
| public void | write(char[] chr)Write the characters from an array.
out.write(chr);
| public void | write(char[] chr, int st, int end)Write the specified characters from an array.
out.write(chr, st, end);
| public void | write(java.lang.String str)Write the characters from a string.
out.write(str);
| public void | write(java.lang.String str, int st, int end)Write the specified characters from a string.
out.write(str, st, end);
|
|