Class LazyFileInputStream

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

    public class LazyFileInputStream
    extends AutoCloseInputStream
    This input stream delays opening the file until the first byte is read, and closes and discards the underlying stream as soon as the end of input has been reached or when the stream is explicitly closed.
    • Constructor Summary

      Constructors 
      Constructor Description
      LazyFileInputStream​(java.io.File file)
      Creates a new LazyFileInputStream for the given file.
      LazyFileInputStream​(java.io.FileDescriptor fd)
      Creates a new LazyFileInputStream for the given file descriptor.
      LazyFileInputStream​(java.lang.String name)
      Creates a new LazyFileInputStream for the given file.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()
      Invokes the delegate's InputStream.available() method.
      void close()
      Closes the underlying input stream and replaces the reference to it with a ClosedInputStream instance.
      void mark​(int readlimit)
      Invokes the delegate's InputStream.mark(int) method.
      boolean markSupported()
      Invokes the delegate's InputStream.markSupported() method.
      int read()
      Invokes the delegate's InputStream.read() method unless the stream is closed.
      int read​(byte[] b)
      Invokes the delegate's InputStream.read(byte[]) method.
      int read​(byte[] b, int off, int len)
      Invokes the delegate's InputStream.read(byte[], int, int) method.
      void reset()
      Invokes the delegate's InputStream.reset() method.
      long skip​(long n)
      Invokes the delegate's InputStream.skip(long) method.
      • Methods inherited from class java.io.InputStream

        nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
      • Methods inherited from class java.lang.Object

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

      • LazyFileInputStream

        public LazyFileInputStream​(java.io.File file)
                            throws java.io.FileNotFoundException
        Creates a new LazyFileInputStream for the given file. If the file is unreadable, a FileNotFoundException is thrown. The file is not opened until the first byte is read from the stream.
        Parameters:
        file - the file
        Throws:
        java.io.FileNotFoundException
      • LazyFileInputStream

        public LazyFileInputStream​(java.io.FileDescriptor fd)
        Creates a new LazyFileInputStream for the given file descriptor. The file is not opened until the first byte is read from the stream.
        Parameters:
        fd -
      • LazyFileInputStream

        public LazyFileInputStream​(java.lang.String name)
                            throws java.io.FileNotFoundException
        Creates a new LazyFileInputStream for the given file. If the file is unreadable, a FileNotFoundException is thrown.
        Parameters:
        name -
        Throws:
        java.io.FileNotFoundException
    • Method Detail

      • read

        public int read()
                 throws java.io.IOException
        Description copied from class: ProxyInputStream
        Invokes the delegate's InputStream.read() method unless the stream is closed.
        Overrides:
        read in class ProxyInputStream
        Returns:
        the byte read or EOF if we reached the end of stream.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • available

        public int available()
                      throws java.io.IOException
        Description copied from class: ProxyInputStream
        Invokes the delegate's InputStream.available() method.
        Overrides:
        available in class ProxyInputStream
        Returns:
        the number of available bytes, 0 if the stream is closed.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • close

        public void close()
                   throws java.io.IOException
        Description copied from class: AutoCloseInputStream
        Closes the underlying input stream and replaces the reference to it with a ClosedInputStream instance.

        This method is automatically called by the read methods when the end of input has been reached.

        Note that it is safe to call this method any number of times. The original underlying input stream is closed and discarded only once when this method is first called.

        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class AutoCloseInputStream
        Throws:
        java.io.IOException - if the underlying input stream cannot be closed
      • reset

        public void reset()
                   throws java.io.IOException
        Description copied from class: ProxyInputStream
        Invokes the delegate's InputStream.reset() method.
        Overrides:
        reset in class ProxyInputStream
        Throws:
        java.io.IOException - if this stream has not been marked or if the mark has been invalidated.
      • mark

        public void mark​(int readlimit)
        Description copied from class: ProxyInputStream
        Invokes the delegate's InputStream.mark(int) method.
        Overrides:
        mark in class ProxyInputStream
        Parameters:
        readlimit - read ahead limit.
      • skip

        public long skip​(long n)
                  throws java.io.IOException
        Description copied from class: ProxyInputStream
        Invokes the delegate's InputStream.skip(long) method.
        Overrides:
        skip in class ProxyInputStream
        Parameters:
        n - the number of bytes to skip.
        Returns:
        the actual number of bytes skipped.
        Throws:
        java.io.IOException - if the stream does not support seek, or if some other I/O error occurs.
      • read

        public int read​(byte[] b)
                 throws java.io.IOException
        Description copied from class: ProxyInputStream
        Invokes the delegate's InputStream.read(byte[]) method.
        Overrides:
        read in class ProxyInputStream
        Parameters:
        b - the buffer to read the bytes into.
        Returns:
        the number of bytes read or EOF if we reached the end of stream.
        Throws:
        java.io.IOException -
        • If the first byte cannot be read for any reason other than the end of the file,
        • if the input stream has been closed, or
        • if some other I/O error occurs.
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
        Description copied from class: ProxyInputStream
        Invokes the delegate's InputStream.read(byte[], int, int) method.
        Overrides:
        read in class ProxyInputStream
        Parameters:
        b - the buffer to read the bytes into.
        off - The start offset.
        len - The number of bytes to read.
        Returns:
        the number of bytes read or EOF if we reached the end of stream.
        Throws:
        java.io.IOException -
        • If the first byte cannot be read for any reason other than the end of the file,
        • if the input stream has been closed, or
        • if some other I/O error occurs.