Class TempFileInputStream

  • All Implemented Interfaces:
    Disposable, java.io.Closeable, java.lang.AutoCloseable

    public class TempFileInputStream
    extends InputStreamDecorator<java.io.FileInputStream>
    An input stream to a file that, when closed, deletes the file.

    It is very important to properly close this output stream when finished using it; otherwise, orphaned temporary files may remain.

    Author:
    Garret Wilson
    • Constructor Detail

      • TempFileInputStream

        public TempFileInputStream​(java.io.File tempFile)
                            throws java.io.FileNotFoundException
        File constructor.
        Parameters:
        tempFile - The temporary file to which an input stream should be created.
        Throws:
        java.lang.NullPointerException - if the given file is null.
        java.io.FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
    • Method Detail

      • close

        public void close​(boolean closeDecoratedStream)
                   throws java.io.IOException
        Closes this input stream and releases any system resources associated with the stream. A closed stream cannot perform output operations and cannot be reopened. If auto-dispose is enabled, InputStreamDecorator.dispose() will be called if closing is successful. This version does not allow closing without closing the decorated stream.
        Overrides:
        close in class InputStreamDecorator<java.io.FileInputStream>
        Parameters:
        closeDecoratedStream - Whether the decorated stream should also be closed.
        Throws:
        java.lang.IllegalArgumentException - if the close decorated stream flag is false.
        java.io.IOException - if an I/O error occurs.
        See Also:
        InputStreamDecorator.beforeClose(), InputStreamDecorator.afterClose(), InputStreamDecorator.dispose()
      • dispose

        public void dispose()
        Uninitializes the object. After calling this method, the object should not be used further. This method can be called multiple times without danger. All exceptions should be caught and dealt with in this method. Child classes must call the parent class version. This deletes the temporary file, if any. Because this method is called indirectly from InputStreamDecorator.close(), this results in the temporary file being deleted after the input stream is closed.
        Specified by:
        dispose in interface Disposable
        Overrides:
        dispose in class InputStreamDecorator<java.io.FileInputStream>