com.mucommander.io
Class BackupOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by com.mucommander.io.BackupOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class BackupOutputStream
extends java.io.OutputStream

Saves file in as crash-safe a manner as possible.

In order to prevent system or muCommander failures to corrupt configuration files, the BackupOutputStream implements the following algorithm:

This way, if a crash was to happen while configuration files are being saved, either of the following will happen:

Files that have been saved by this class should be read with BackupInputStream in order to make sure that an uncorrupt version of them is loaded.

The BackupOutputStream monitors all of its own I/O operations. If an error occurs, then the backup operation will not be performed when close() is called. It's possible to force the backup operation by using the close(boolean) method.

Author:
Nicolas Rinaudo
See Also:
BackupInputStream

Field Summary
static char BACKUP_SUFFIX
          Character to add suffix file names with in order to mark them as backup.
 
Constructor Summary
BackupOutputStream(AbstractFile file)
          Opens a backup output stream on the specified file.
BackupOutputStream(java.io.File file)
          Opens a backup output stream on the specified file.
BackupOutputStream(java.lang.String file)
          Opens a backup output stream on the specified file.
 
Method Summary
 void close()
          Finishes the backup operation.
 void close(boolean backup)
          Closes the output stream.
 void flush()
          Flushes this output stream and forces any buffered output bytes to be written out to the stream.
 void write(byte[] b)
          Writes b.length bytes to this output stream.
 void write(byte[] b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this output stream.
 void write(int b)
          Writes the specified byte to this output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BACKUP_SUFFIX

public static final char BACKUP_SUFFIX
Character to add suffix file names with in order to mark them as backup.

See Also:
Constant Field Values
Constructor Detail

BackupOutputStream

public BackupOutputStream(java.io.File file)
                   throws java.io.IOException
Opens a backup output stream on the specified file.

Parameters:
file - file on which to open a backup output stream.
Throws:
java.io.IOException - thrown if any IO error occurs.

BackupOutputStream

public BackupOutputStream(java.lang.String file)
                   throws java.io.IOException
Opens a backup output stream on the specified file.

Parameters:
file - file on which to open a backup output stream.
Throws:
java.io.IOException - thrown if any IO error occurs.

BackupOutputStream

public BackupOutputStream(AbstractFile file)
                   throws java.io.IOException
Opens a backup output stream on the specified file.

Parameters:
file - file on which to open a backup output stream.
Throws:
java.io.IOException - thrown if any IO error occurs.
Method Detail

flush

public void flush()
           throws java.io.IOException
Flushes this output stream and forces any buffered output bytes to be written out to the stream.

This method calls the flush() method of its underlying output stream.

If an error occurs at this point, the close() method will not overwrite the target file. This can be forced through the close(boolean) method.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - if an I/O error occurs.

write

public void write(byte[] b)
           throws java.io.IOException
Writes b.length bytes to this output stream.

This method calls the write(byte[] b) method of its underlying output stream.

If an error occurs at this point, the close() method will not overwrite the target file. This can be forced through the close(boolean) method.

Overrides:
write in class java.io.OutputStream
Parameters:
b - the data to be written.
Throws:
java.io.IOException - if an I/O error occurs.

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Writes len bytes from the specified byte array starting at offset off to this output stream.

This method calls the write(byte[] b, int off, int len) method of its underlying output stream.

If an error occurs at this point, the close() method will not overwrite the target file. This can be forced through the close(boolean) method.

Overrides:
write in class java.io.OutputStream
Parameters:
b - the data to be written.
off - the start offset in the data.
len - the number of bytes to write.
Throws:
java.io.IOException - if an I/O error occurs.

write

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

This method calls the write(byte b) method of its underlying output stream.

If an error occurs at this point, the close() method will not overwrite the target file. This can be forced through the close(boolean) method.

Specified by:
write in class java.io.OutputStream
Parameters:
b - the data to be written.
Throws:
java.io.IOException - if an I/O error occurs.

close

public void close()
           throws java.io.IOException
Finishes the backup operation.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException - thrown if any IO related error occurs.

close

public void close(boolean backup)
           throws java.io.IOException
Closes the output stream.

The backup parameter is meant for those cases when an error happened while writing to the stream: if it did, we don't want to propagate to the target file, and thus should prevent the backup operation from being performed.

Parameters:
backup - whether or not to overwrite the target file by the backup one.
Throws:
java.io.IOException - thrown if any IO related error occurs.


This file is part of muCommander - Copyright (C) 2002-2008 Maxence Bernard