Class LazyFileOutputStream

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

    public class LazyFileOutputStream
    extends java.io.OutputStream
    An OutputStream that uses a FileOutputStream internally, which is created when first access is done. On OutputStream.close() the internal FileOutputStream will be closed and unset that next writing will get a new stream.
    • Constructor Summary

      Constructors 
      Constructor Description
      LazyFileOutputStream​(java.io.File file)
      Create an instance based on the given File.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      void flush()
      java.nio.channels.FileChannel getChannel()
      Initializes the delegate if not already done and delegates to FileOutputStream.getChannel().
      java.io.FileDescriptor getFD()
      Initializes the delegate if not already done and delegates to FileOutputStream.getFD().
      boolean isOpen()
      Tell if the delegate is initialized.
      void write​(byte[] b)
      void write​(byte[] b, int off, int len)
      void write​(int b)
      • Methods inherited from class java.io.OutputStream

        nullOutputStream
      • Methods inherited from class java.lang.Object

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

      • LazyFileOutputStream

        public LazyFileOutputStream​(java.io.File file)
        Create an instance based on the given File.
        Parameters:
        file - the file to use in the internal delegate.
    • Method Detail

      • close

        public void close()
                   throws java.io.IOException

        This implementation calls FileOutputStream.close() on the delegate and unassigns the delegate.

        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException

        This implementation calls OutputStream.flush() on the delegate if it is initalized.

        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.OutputStream
        Throws:
        java.io.IOException
      • getChannel

        public java.nio.channels.FileChannel getChannel()
                                                 throws java.io.FileNotFoundException
        Initializes the delegate if not already done and delegates to FileOutputStream.getChannel().
        Returns:
        FileOutputStream.getChannel()
        Throws:
        java.io.FileNotFoundException - if initialization of delegate fails. see FileOutputStream(File).
      • getFD

        public java.io.FileDescriptor getFD()
                                     throws java.io.IOException
        Initializes the delegate if not already done and delegates to FileOutputStream.getFD().
        Returns:
        FileOutputStream.getFD()
        Throws:
        java.io.IOException - see FileOutputStream.getFD().
        java.io.FileNotFoundException - if initialization of delegate fails. see FileOutputStream(File).
      • write

        public void write​(byte[] b)
                   throws java.io.IOException

        This implementation innitializes the delegate if not already done and delegates to FileOutputStream.write(byte[]).

        Overrides:
        write in class java.io.OutputStream
        Throws:
        java.io.FileNotFoundException - if initialization of delegate fails. see FileOutputStream(File).
        java.io.IOException
      • write

        public void write​(byte[] b,
                          int off,
                          int len)
                   throws java.io.IOException

        This implementation innitializes the delegate if not already done and delegates to FileOutputStream.write(byte[], int, int).

        Overrides:
        write in class java.io.OutputStream
        Throws:
        java.io.FileNotFoundException - if initialization of delegate fails. see FileOutputStream(File).
        java.io.IOException
      • write

        public void write​(int b)
                   throws java.io.IOException

        This implementation innitializes the delegate if not already done and delegates to FileOutputStream.write(int).

        Specified by:
        write in class java.io.OutputStream
        Throws:
        java.io.FileNotFoundException - if initialization of delegate fails. see FileOutputStream(File).
        java.io.IOException
      • isOpen

        public boolean isOpen()
        Tell if the delegate is initialized.
        Returns:
        true if there is a delegate.