Class SafeOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable, java.nio.channels.Channel, java.nio.channels.WritableByteChannel

    @NotThreadSafe
    public abstract class SafeOutputStream
    extends java.io.OutputStream
    implements java.nio.channels.WritableByteChannel, java.io.Closeable
    See Also:
    SafeFiles.createAtomicFile(Path)
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      abstract void close()
      If commit() as been called, this method is a NO-OP.
      abstract void commit()
      Commit causes the current atomic file writing operation to conclude and the current temp file is safely promoted to being the canonical file.
      abstract int write​(java.nio.ByteBuffer src)
      Writes a sequence of bytes to this channel from the given buffer.
      • Methods inherited from class java.io.OutputStream

        flush, nullOutputStream, write, write, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.nio.channels.Channel

        isOpen
    • Constructor Detail

      • SafeOutputStream

        public SafeOutputStream()
    • Method Detail

      • write

        public abstract int write​(@Nonnull
                                  java.nio.ByteBuffer src)
                           throws java.io.IOException
        Writes a sequence of bytes to this channel from the given buffer.

        Unlike the contract of WritableByteChannel, the ENTIRE buffer is written to the channel (behaving like writeFully).

        Specified by:
        write in interface java.nio.channels.WritableByteChannel
        Parameters:
        src - The buffer to flush.
        Returns:
        The number of bytes written to the output stream.
        Throws:
        java.io.IOException - in the event that the buffer could not be written.
      • commit

        public abstract void commit()
                             throws java.io.IOException
        Commit causes the current atomic file writing operation to conclude and the current temp file is safely promoted to being the canonical file.

        It is safe to call close()} after commit. It is NOT safe to call any of the variations on write(java.nio.ByteBuffer) or OutputStream.flush() however.

        Throws:
        java.io.IOException - in the event that the data could not be committed.
      • close

        public abstract void close()
                            throws java.io.IOException
        If commit() as been called, this method is a NO-OP. Otherwise...

        Close causes the current atomic file writing operation to abort and the current temp file to be erased. It is NOT safe to call any of the variations on write(java.nio.ByteBuffer) or OutputStream.flush() however.

        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.IOException - in the event that the output stream cannot be closed.