org.apache.avalon.fortress.tools
Class ChangedFileOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.apache.avalon.fortress.tools.ChangedFileOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public final class ChangedFileOutputStream
extends java.io.OutputStream

OutputStream which will only update an existing file if its contents actually change. Needed to keep Ant from rebuilding jars even when nothing has changed.

Version:
CVS $Revision: 1.1 $ $Date: 2004/04/02 08:29:44 $
Author:
The Avalon Team

Field Summary
private  java.io.ByteArrayOutputStream m_bos
          The output stream used to buffer data being writen.
private  java.io.File m_file
          The file to write to.
 
Constructor Summary
ChangedFileOutputStream(java.io.File file)
          Creates a new ChangedFileOutputStream.
 
Method Summary
 void close()
          Close the stream.
private  byte[] readBytes(java.io.File file)
          Reads the full contents of a file into a byte array.
 void write(int b)
          Writes the specified byte to this output stream.
private  void writeBytes(java.io.File file, byte[] bytes)
          Reads the full contents of a byte array out to a file.
 
Methods inherited from class java.io.OutputStream
flush, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_file

private java.io.File m_file
The file to write to.


m_bos

private java.io.ByteArrayOutputStream m_bos
The output stream used to buffer data being writen.

Constructor Detail

ChangedFileOutputStream

public ChangedFileOutputStream(java.io.File file)
Creates a new ChangedFileOutputStream.

Parameters:
file - The file to write to.
Method Detail

write

public void write(int b)
           throws java.io.IOException
Writes the specified byte to this output stream.

Specified by:
write in class java.io.OutputStream
Parameters:
b - Byte to write.
Throws:
java.io.IOException - If an I/O error occurs.

close

public void close()
           throws java.io.IOException
Close the stream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException - If an I/O error occurs.

readBytes

private byte[] readBytes(java.io.File file)
                  throws java.io.IOException
Reads the full contents of a file into a byte array. The file contents are treated as binary data.

Parameters:
file - File to read.
Returns:
The contents of the file as a byte array.
Throws:
java.io.IOException - If the file could not be read for any reason.

writeBytes

private void writeBytes(java.io.File file,
                        byte[] bytes)
                 throws java.io.IOException
Reads the full contents of a byte array out to a file.

Parameters:
file - File to write to.
bytes - The binary data to write.
Throws:
java.io.IOException - If the file could not be written to for any reason.