gov.llnl.babel.backend.writers

Class ChangeWriter


public class ChangeWriter
extends Writer

This class will write a file leaving its last time modified unchanged if the content is unchanged. When overwriting a file, this java.io.Writer will initially write a temporary file. When close() is called, it compares the contents of the original file with the temporary file. If the contents have changed, it will overwrite the original file with the contents of the temporary file; otherwise, the original file is left unchanged. Thus, the last time modified of the original file is only changed when the contents change. The temporary file is deleted when close() is called.

Constructor Summary

ChangeWriter(File destination, File directory)
Create a writer to write a new file or overwrite an old file.

Method Summary

void
close()
Complete writing to the original file if needed.
static Writer
createWriter(String filename, String directory)
Create a java.io.Writer for a particular filename and directory combination.
protected void
finalize()
Make sure to close the writer if the programmer should forget to do so.
void
flush()
This will flush all the buffered output into the temporary file.
void
write(String str)
Write a string.
void
write(String str, int off, int len)
Write a substring.
void
write(char[] cbuf)
Write a character array.
void
write(char[] cbuf, int off, int len)
Write part of a character array.
void
write(int c)
Write a character.

Constructor Details

ChangeWriter

public ChangeWriter(File destination,
                    File directory)
            throws IOException
Create a writer to write a new file or overwrite an old file. You may prefer to use createWriter(String,String) instead of this constructor; it uses the simpler java.io.FileWriter if destination does not already exist.
Parameters:
destination - this is the location of the new file
directory - this is the directory where the temporary file will be created. If null, a system dependent temporary will be used (e.g., /tmp on Unix).

Method Details

close

public void close()
            throws IOException
Complete writing to the original file if needed. This compares the contents of the temporary file and the original file. If they differ or if the original file does not exist, this copies the contents from the temporary file to the original file. This will remove the temporary file.

createWriter

public static Writer createWriter(String filename,
                                  String directory)
            throws IOException
Create a java.io.Writer for a particular filename and directory combination.
Parameters:
filename - the filename without any leading directory information.
directory - the directory name. This string will be prepended to filename to get the whole path to the file. If it's not an empty string, it should end with the directory separator character, java.io.File.separatorChar.
Returns:
a writer that will write to (directory + filename). This is typically a ChangeWriter or a java.io.FileWriter. The final type shouldn't really matter to the caller.

finalize

protected void finalize()
            throws Throwable
Make sure to close the writer if the programmer should forget to do so.

flush

public void flush()
            throws IOException

write

public void write(String str)
            throws IOException
Write a string.
Parameters:
str - the string to be output.

write

public void write(String str,
                  int off,
                  int len)
            throws IOException
Write a substring.
Parameters:
str - the whole string
off - the offset of the first character to be written.
len - the number of characters to write.

write

public void write(char[] cbuf)
            throws IOException
Write a character array.
Parameters:
cbuf - the character array to be output.

write

public void write(char[] cbuf,
                  int off,
                  int len)
            throws IOException
Write part of a character array.
Parameters:
cbuf - the character array to be output.
off - the index of the first character to be written.
len - the number of characters to write.

write

public void write(int c)
            throws IOException
Write a character.
Parameters:
c - the character to be output.