org.jcoderz.commons.util
Class LazyFileOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.jcoderz.commons.util.LazyFileOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class LazyFileOutputStream
extends OutputStream

Purpose of this class is to avoid writing a File if it already exists and hold the same content.

This class in not about saving time for faster processing nor does is perform anything quicker. It sole checks if the target file exists, and if its content is already the same than the content that should be written. If this is the case no write operation is done at all. Therefore also the file content to be written is held in memory until the file is closed OR it is detected that the content did change.

Instances of this class are NOT multi thread save.

Author:
Andreas Mandel

Constructor Summary
LazyFileOutputStream(File file)
           
LazyFileOutputStream(File file, boolean append)
           
LazyFileOutputStream(String name)
           
LazyFileOutputStream(String name, boolean append)
           
 
Method Summary
 void close()
          
protected  void finalize()
          Cleans up the connection to the file, and ensures that the close method of this file output stream is called when there are no more references to this stream.
 boolean isBuffered()
          Returns true if the file is still buffered.
 void write(int b)
          
 
Methods inherited from class java.io.OutputStream
flush, write, write
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LazyFileOutputStream

public LazyFileOutputStream(File file,
                            boolean append)
                     throws FileNotFoundException
Throws:
FileNotFoundException
See Also:
FileOutputStream.FileOutputStream(String, boolean)

LazyFileOutputStream

public LazyFileOutputStream(File file)
                     throws FileNotFoundException
Throws:
FileNotFoundException
See Also:
FileOutputStream.FileOutputStream(File)

LazyFileOutputStream

public LazyFileOutputStream(String name,
                            boolean append)
                     throws FileNotFoundException
Throws:
FileNotFoundException
See Also:
FileOutputStream.FileOutputStream(String, boolean)

LazyFileOutputStream

public LazyFileOutputStream(String name)
                     throws FileNotFoundException
Throws:
FileNotFoundException
See Also:
FileOutputStream.FileOutputStream(String)
Method Detail

isBuffered

public boolean isBuffered()
Returns true if the file is still buffered. The value might change from true to false until the stream is closed. After this the returned value will not change any more.

Returns:
true if the actual out file was not touched (yet).

write

public void write(int b)
           throws IOException

Specified by:
write in class OutputStream
Throws:
IOException

close

public void close()
           throws IOException

Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException

finalize

protected void finalize()
                 throws IOException,
                        Throwable
Cleans up the connection to the file, and ensures that the close method of this file output stream is called when there are no more references to this stream.

Overrides:
finalize in class Object
Throws:
IOException - if an I/O error occurs.
Throwable
See Also:
FileInputStream.close()


Copyright 2007 The jCoderZ Project.