Class LazyFileInputStream

  • All Implemented Interfaces:
    Closeable, 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 Detail

      • LazyFileInputStream

        public LazyFileInputStream​(File file)
                            throws 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:
        FileNotFoundException
      • LazyFileInputStream

        public LazyFileInputStream​(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​(String name)
                            throws FileNotFoundException
        Creates a new LazyFileInputStream for the given file. If the file is unreadable, a FileNotFoundException is thrown.
        Parameters:
        name -
        Throws:
        FileNotFoundException
    • Method Detail

      • close

        public void close()
                   throws 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 AutoCloseable
        Specified by:
        close in interface Closeable
        Overrides:
        close in class AutoCloseInputStream
        Throws:
        IOException - if the underlying input stream can not be closed
      • markSupported

        public boolean markSupported()
        Description copied from class: ProxyInputStream
        Invokes the delegate's markSupported() method.
        Overrides:
        markSupported in class ProxyInputStream
        Returns:
        true if mark is supported, otherwise false
      • mark

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

        public long skip​(long n)
                  throws IOException
        Description copied from class: ProxyInputStream
        Invokes the delegate's skip(long) method.
        Overrides:
        skip in class ProxyInputStream
        Parameters:
        n - the number of bytes to skip
        Returns:
        the actual number of bytes skipped
        Throws:
        IOException - if an I/O error occurs
      • read

        public int read​(byte[] b)
                 throws IOException
        Description copied from class: ProxyInputStream
        Invokes the delegate's 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 the end of stream
        Throws:
        IOException - if an I/O error occurs
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws IOException
        Description copied from class: ProxyInputStream
        Invokes the delegate's 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 -1 if the end of stream
        Throws:
        IOException - if an I/O error occurs