Class LazyFileInputStream

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

    public class LazyFileInputStream
    extends java.io.InputStream
    An InputStream that uses a FileInputStream internally, which is created when the first acces is done. On InputStream.close() the internal FileInputStream will be closed and unset that next reading all will get a new stream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()
      void close()
      java.nio.channels.FileChannel getChannel()
      Initializes the delegate if not already done and delegates to FileInputStream.getChannel().
      java.io.FileDescriptor getFD()
      Initializes the delegate if not already done and delegates to FileInputStream.getFD().
      boolean isOpen()
      Tell if the delegate is initialized.
      int read()
      int read​(byte[] b)
      int read​(byte[] b, int off, int len)
      byte[] readAllBytes()
      int readNBytes​(byte[] b, int off, int len)
      long skip​(long n)
      long transferTo​(java.io.OutputStream out)
      • Methods inherited from class java.io.InputStream

        mark, markSupported, nullInputStream, readNBytes, reset
      • Methods inherited from class java.lang.Object

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

      • LazyFileInputStream

        public LazyFileInputStream​(java.io.File file)
        Create a LazyFileInputStream to be based on the given File.
        Parameters:
        file - the file to use in the delegate.
    • Method Detail

      • read

        public int read()
                 throws java.io.IOException

        This implementation initializes the delegate if not already done and delegates to FileInputStream.read().

        Specified by:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] b)
                 throws java.io.IOException

        This implementation initializes the delegate if not already done and delegates to FileInputStream.read(byte[]).

        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

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

        This implementation initializes the delegate if not already done and delegates to FileInputStream.read(byte[], int, int).

        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • readNBytes

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

        This implementation initializes the delegate if not already done and delegates to InputStream.readNBytes(byte[], int, int).

        Overrides:
        readNBytes in class java.io.InputStream
        Throws:
        java.io.IOException
      • readAllBytes

        public byte[] readAllBytes()
                            throws java.io.IOException

        This implementation initializes the delegate if not already done and delegates to InputStream.readAllBytes().

        Overrides:
        readAllBytes in class java.io.InputStream
        Throws:
        java.io.IOException
      • skip

        public long skip​(long n)
                  throws java.io.IOException

        This implementation initializes the delegate if not already done and delegates to FileInputStream.skip(long).

        Overrides:
        skip in class java.io.InputStream
        Throws:
        java.io.IOException
      • available

        public int available()
                      throws java.io.IOException

        This implementation initializes the delegate if not already done and delegates to FileInputStream.available().

        Overrides:
        available in class java.io.InputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException

        This implementation calls FileInputStream.close() on the delegate and unassignes the delegate.

        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.InputStream
        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 FileInputStream.getChannel().
        Returns:
        FileInputStream.getChannel()
        Throws:
        java.io.FileNotFoundException - see FileInputStream(File).
      • getFD

        public final java.io.FileDescriptor getFD()
                                           throws java.io.IOException
        Initializes the delegate if not already done and delegates to FileInputStream.getFD().
        Returns:
        FileInputStream.getFD()
        Throws:
        java.io.IOException - see FileInputStream.getFD()
      • transferTo

        public long transferTo​(java.io.OutputStream out)
                        throws java.io.IOException

        This implementation initializes the delegate if not already done and delegates to InputStream.transferTo(OutputStream).

        Overrides:
        transferTo in class java.io.InputStream
        Throws:
        java.io.IOException
      • isOpen

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