Class AbstractOutputFile

java.lang.Object
dev.hardwood.AbstractOutputFile
All Implemented Interfaces:
OutputFile, Closeable, AutoCloseable
Direct Known Subclasses:
InMemoryOutputFile, S3OutputFile

@Experimental public abstract class AbstractOutputFile extends Object implements OutputFile

A base for implementing OutputFile that enforces its lifecycle.

The public methods are final: each checks the state the lifecycle table in OutputFile allows it in, throws IllegalStateException otherwise, and calls the protected do method that does the work. position() counts the bytes doWrite(ByteBuffer) accepted. A subclass implements doWrite and overrides the others it needs; they do nothing by default:

Method Called by, while the file is open Does
doCreate() create() acquires the destination's resources
doWrite(ByteBuffer) write appends the bytes
doClose() close() publishes the file at the destination
doDiscard() discard(), or close() when doClose() throws releases the resources and throws away what was written

Each do method other than doWrite is called at most once per file. When doCreate() throws, the file ends discarded without a call to doDiscard(), so doCreate() must leave nothing held when it fails. When doClose() throws, the file ends discarded: doDiscard() runs, a failure of it is attached to the doClose() failure as suppressed, and the doClose() failure is rethrown. The file is discarded before doDiscard() runs and is not discarded again, so doDiscard() must release what it can before it throws.

This API is Experimental: the shape may change in future releases.

  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final void
    Publishes an open file.
    final void
    Performs resource acquisition (e.g. opening a file channel).
    final void
    Discards an open file.
    protected void
    Publishes the file at the destination.
    protected void
    Acquires the destination's resources, such as opening a file channel.
    protected void
    Releases the resources and throws away what was written, leaving the destination as if nothing was written.
    protected abstract void
    Appends the remaining bytes of data, consuming the buffer.
    final long
    Returns the number of bytes written so far, which is the offset at which the next OutputFile.write(ByteBuffer) will begin.
    protected final void
    Throws unless the file is published, for a subclass that hands out the finished file.
    final void
    Appends the remaining bytes of data to the file.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • AbstractOutputFile

      protected AbstractOutputFile(String destination)
      Parameters:
      destination - the destination, as the IllegalStateExceptions name it, such as a path or a URI
  • Method Details

    • create

      public final void create() throws IOException
      Description copied from interface: OutputFile
      Performs resource acquisition (e.g. opening a file channel). Must be called before OutputFile.write(ByteBuffer) or OutputFile.position().
      Specified by:
      create in interface OutputFile
      Throws:
      IOException - if the resource cannot be acquired
    • write

      public final void write(ByteBuffer data) throws IOException
      Description copied from interface: OutputFile

      Appends the remaining bytes of data to the file.

      The buffer is consumed (its position advances to its limit).

      Specified by:
      write in interface OutputFile
      Parameters:
      data - the bytes to append
      Throws:
      IOException - if the write fails
    • position

      public final long position()
      Description copied from interface: OutputFile
      Returns the number of bytes written so far, which is the offset at which the next OutputFile.write(ByteBuffer) will begin.
      Specified by:
      position in interface OutputFile
      Returns:
      the running byte offset
    • close

      public final void close() throws IOException
      Publishes an open file. Does nothing on a file that is published or discarded, and ends an uncreated file as discarded without publishing anything.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface OutputFile
      Throws:
      IOException - if the file cannot be published
    • discard

      public final void discard() throws IOException
      Discards an open file. Does nothing on a file that is published or discarded, and ends an uncreated file as discarded.
      Specified by:
      discard in interface OutputFile
      Throws:
      IOException - if resources cannot be released
    • requireClosed

      protected final void requireClosed()
      Throws unless the file is published, for a subclass that hands out the finished file.
      Throws:
      IllegalStateException - if the file is not published
    • doCreate

      protected void doCreate() throws IOException
      Acquires the destination's resources, such as opening a file channel. Does nothing by default.
      Throws:
      IOException - if the resources cannot be acquired; the file then ends discarded
    • doWrite

      protected abstract void doWrite(ByteBuffer data) throws IOException
      Appends the remaining bytes of data, consuming the buffer. Returning with bytes left in data makes write throw IllegalStateException, since position() would no longer match the file.
      Parameters:
      data - the bytes to append
      Throws:
      IOException - if the write fails
    • doClose

      protected void doClose() throws IOException
      Publishes the file at the destination. May leave resources held when it fails, for doDiscard() to release. Does nothing by default.
      Throws:
      IOException - if the file cannot be published; the file then ends discarded
    • doDiscard

      protected void doDiscard() throws IOException
      Releases the resources and throws away what was written, leaving the destination as if nothing was written. Releases what it can before it throws, since it is not called again. Does nothing by default.
      Throws:
      IOException - if the resources cannot be released