gov.llnl.babel.backend.writers
Class ChangeWriter

java.lang.Object
  extended by java.io.Writer
      extended by gov.llnl.babel.backend.writers.ChangeWriter
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable

public class ChangeWriter
extends java.io.Writer

This class will write a file leaving its last time modified unchanged if the content is unchanged. When overwriting a file, this 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.


Field Summary
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
ChangeWriter(java.io.File destination, java.io.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 java.io.Writer createWriter(java.lang.String filename, java.lang.String directory)
          Create a 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(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.
 void write(java.lang.String str)
          Write a string.
 void write(java.lang.String str, int off, int len)
          Write a substring.
 
Methods inherited from class java.io.Writer
append, append, append, append, append, append
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ChangeWriter

public ChangeWriter(java.io.File destination,
                    java.io.File directory)
             throws java.io.IOException
Create a writer to write a new file or overwrite an old file. You may prefer to use createWriter(java.lang.String, java.lang.String) instead of this constructor; it uses the simpler 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).
Throws:
java.io.IOException - something went wrong creating the Writer.
Method Detail

createWriter

public static java.io.Writer createWriter(java.lang.String filename,
                                          java.lang.String directory)
                                   throws java.io.IOException
Create a 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, File.separatorChar.
Returns:
a writer that will write to (directory + filename). This is typically a ChangeWriter or a FileWriter. The final type shouldn't really matter to the caller.
Throws:
java.io.IOException - something went wrong creating the Writer.

write

public void write(java.lang.String str)
           throws java.io.IOException
Write a string.

Overrides:
write in class java.io.Writer
Parameters:
str - the string to be output.
Throws:
java.io.IOException - There was a problem writing to the output device. Writing to a closed ChangeWriter also produces an IOException.

write

public void write(char[] cbuf)
           throws java.io.IOException
Write a character array.

Overrides:
write in class java.io.Writer
Parameters:
cbuf - the character array to be output.
Throws:
java.io.IOException - There was a problem writing to the output device. Writing to a closed ChangeWriter also produces an IOException.

write

public void write(java.lang.String str,
                  int off,
                  int len)
           throws java.io.IOException
Write a substring.

Overrides:
write in class java.io.Writer
Parameters:
str - the whole string
off - the offset of the first character to be written.
len - the number of characters to write.
Throws:
java.io.IOException - There was a problem writing to the output device. Writing to a closed ChangeWriter also produces an IOException.

write

public void write(int c)
           throws java.io.IOException
Write a character.

Overrides:
write in class java.io.Writer
Parameters:
c - the character to be output.
Throws:
java.io.IOException - There was a problem writing to the output device. Writing to a closed ChangeWriter also produces an IOException.

write

public void write(char[] cbuf,
                  int off,
                  int len)
           throws java.io.IOException
Write part of a character array.

Specified by:
write in class java.io.Writer
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.
Throws:
java.io.IOException - There was a problem writing to the output device. Writing to a closed ChangeWriter also produces an IOException.

flush

public void flush()
           throws java.io.IOException
This will flush all the buffered output into the temporary file. Because all output goes to the temporary file first, flushing will not force an update to the final file. The decision on whether to update the final file or not is made when close() is called.

Specified by:
flush in interface java.io.Flushable
Specified by:
flush in class java.io.Writer
Throws:
java.io.IOException - There was a problem flushing the output device. Flushing a closed ChangeWriter also produces an IOException.

close

public void close()
           throws java.io.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.

Specified by:
close in interface java.io.Closeable
Specified by:
close in class java.io.Writer
Throws:
java.io.IOException - This method does a lot compared to many close methods. It must compare the temporary file, and then try to write the original file. There are many possible reasons for an exception.

finalize

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

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable