org.apache.commons.io.output

Class LockableFileWriter

public 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.

Version: $Id: LockableFileWriter.java 610010 2008-01-08 14:50:59Z niallp $

Author: Scott Sanders Michael Salmon Jon S. Stevens Daniel Rall Stephen Colebourne Andy Lehane

Field Summary
FilelockFile
The lock file.
static StringLCK
The extension for the lock file.
Writerout
The writer to decorate.
Constructor Summary
LockableFileWriter(String fileName)
Constructs a LockableFileWriter.
LockableFileWriter(String fileName, boolean append)
Constructs a LockableFileWriter.
LockableFileWriter(String fileName, boolean append, String lockDir)
Constructs a LockableFileWriter.
LockableFileWriter(File file)
Constructs a LockableFileWriter.
LockableFileWriter(File file, boolean append)
Constructs a LockableFileWriter.
LockableFileWriter(File file, boolean append, String lockDir)
Constructs a LockableFileWriter.
LockableFileWriter(File file, String encoding)
Constructs a LockableFileWriter with a file encoding.
LockableFileWriter(File file, String encoding, boolean append, String lockDir)
Constructs a LockableFileWriter with a file encoding.
Method Summary
voidclose()
Closes the file writer.
voidcreateLock()
Creates the lock file.
voidflush()
Flush the stream.
WriterinitWriter(File file, String encoding, boolean append)
Initialise the wrapped file writer.
voidtestLockDir(File lockDir)
Tests that we can write to the lock directory.
voidwrite(int idx)
Write a character.
voidwrite(char[] chr)
Write the characters from an array.
voidwrite(char[] chr, int st, int end)
Write the specified characters from an array.
voidwrite(String str)
Write the characters from a string.
voidwrite(String str, int st, int end)
Write the specified characters from a string.

Field Detail

lockFile

private final File lockFile
The lock file.

LCK

private static final String LCK
The extension for the lock file.

out

private final Writer out
The writer to decorate.

Constructor Detail

LockableFileWriter

public LockableFileWriter(String fileName)
Constructs a LockableFileWriter. If the file exists, it is overwritten.

Parameters: fileName the file to write to, not null

Throws: NullPointerException if the file is null IOException in case of an I/O error

LockableFileWriter

public LockableFileWriter(String fileName, boolean append)
Constructs a LockableFileWriter.

Parameters: fileName file to write to, not null append true if content should be appended, false to overwrite

Throws: NullPointerException if the file is null IOException in case of an I/O error

LockableFileWriter

public LockableFileWriter(String fileName, boolean append, String lockDir)
Constructs a LockableFileWriter.

Parameters: fileName the file to write to, not null append true if content should be appended, false to overwrite lockDir the directory in which the lock file should be held

Throws: NullPointerException if the file is null IOException in case of an I/O error

LockableFileWriter

public LockableFileWriter(File file)
Constructs a LockableFileWriter. If the file exists, it is overwritten.

Parameters: file the file to write to, not null

Throws: NullPointerException if the file is null IOException in case of an I/O error

LockableFileWriter

public LockableFileWriter(File file, boolean append)
Constructs a LockableFileWriter.

Parameters: file the file to write to, not null append true if content should be appended, false to overwrite

Throws: NullPointerException if the file is null IOException in case of an I/O error

LockableFileWriter

public LockableFileWriter(File file, boolean append, String lockDir)
Constructs a LockableFileWriter.

Parameters: file the file to write to, not null append true if content should be appended, false to overwrite lockDir the directory in which the lock file should be held

Throws: NullPointerException if the file is null IOException in case of an I/O error

LockableFileWriter

public LockableFileWriter(File file, String encoding)
Constructs a LockableFileWriter with a file encoding.

Parameters: file the file to write to, not null encoding the encoding to use, null means platform default

Throws: NullPointerException if the file is null IOException in case of an I/O error

LockableFileWriter

public LockableFileWriter(File file, String encoding, boolean append, String lockDir)
Constructs a LockableFileWriter with a file encoding.

Parameters: file the file to write to, not null encoding the encoding to use, null means platform default append true if content should be appended, false to overwrite lockDir the directory in which the lock file should be held

Throws: NullPointerException if the file is null IOException in case of an I/O error

Method Detail

close

public void close()
Closes the file writer.

Throws: IOException if an I/O error occurs

createLock

private void createLock()
Creates the lock file.

Throws: IOException if we cannot create the file

flush

public void flush()
Flush the stream.

Throws: IOException if an I/O error occurs

initWriter

private Writer initWriter(File file, String encoding, boolean append)
Initialise the wrapped file writer. Ensure that a cleanup occurs if the writer creation fails.

Parameters: file the file to be accessed encoding the encoding to use append true to append

Returns: The initialised writer

Throws: IOException if an error occurs

testLockDir

private void testLockDir(File lockDir)
Tests that we can write to the lock directory.

Parameters: lockDir the File representing the lock directory

Throws: IOException if we cannot write to the lock directory IOException if we cannot find the lock file

write

public void write(int idx)
Write a character.

Parameters: idx the character to write

Throws: IOException if an I/O error occurs

write

public void write(char[] chr)
Write the characters from an array.

Parameters: chr the characters to write

Throws: IOException if an I/O error occurs

write

public void write(char[] chr, int st, int end)
Write the specified characters from an array.

Parameters: chr the characters to write st The start offset end The number of characters to write

Throws: IOException if an I/O error occurs

write

public void write(String str)
Write the characters from a string.

Parameters: str the string to write

Throws: IOException if an I/O error occurs

write

public void write(String str, int st, int end)
Write the specified characters from a string.

Parameters: str the string to write st The start offset end The number of characters to write

Throws: IOException if an I/O error occurs

Copyright (c) 2002-2011 Apache Software Foundation