Package nzilbb.util

Class TempFileInputStream

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class TempFileInputStream
    extends InputStream
    An InputStream for one-off reading of a temporary file. When the stream is closed or finalized, or when a configurable timeout lapses, the file is deleted. This implementation uses a FileInputStream member, rather than inheriting from FileInputStream, so that the stream is not opened until it's actually going to be read. This ensures that a huge collection of TempFileInputStream objects can be handled without running out of open file handles.
    Author:
    Robert Fromont robert@fromont.net.nz
    • Constructor Detail

      • TempFileInputStream

        public TempFileInputStream​(File file)
                            throws FileNotFoundException
        Constructor. Doesn't open the file; that doesn't happen until some data is wanted.
        Side-effect: deleteOnExit() is called on the file.
        Parameters:
        file - The file.
        Throws:
        FileNotFoundException - If the file doesn't exist.
      • TempFileInputStream

        public TempFileInputStream​(File file,
                                   Long timeoutMS)
                            throws FileNotFoundException
        Constructor. Doesn't open the file; that doesn't happen until some data is wanted.
        Side-effect: deleteOnExit() is called on the file.
        Parameters:
        file - The file.
        timeoutMS - Timeout after which, if the file still exists, it will be deleted.
        Throws:
        FileNotFoundException - If the file doesn't exist.
    • Method Detail

      • getTempFile

        public File getTempFile()
        Getter for fTempFile: The temporary file for deletion.
        Returns:
        The temporary file for deletion.
      • setTempFile

        public TempFileInputStream setTempFile​(File fNewTempFile)
        Setter for fTempFile: The temporary file for deletion.
        Side-effect: deleteOnExit() is called on the file.
        Parameters:
        fNewTempFile - The temporary file for deletion.
      • getDeleteOnClose

        public Boolean getDeleteOnClose()
        Getter for bDeleteOnClose: Whether to delete the file when close() is called or not. Default is TRUE.
        Returns:
        Whether to delete the file when close() is called or not. Default is TRUE.
      • setDeleteOnClose

        public TempFileInputStream setDeleteOnClose​(Boolean bNewDeleteOnClose)
        Setter for bDeleteOnClose: Whether to delete the file when close() is called or not. Default is TRUE.
        Parameters:
        bNewDeleteOnClose - Whether to delete the file when close() is called or not. Default is TRUE.
      • getTimeoutMS

        public Long getTimeoutMS()
        Getter for timeoutMS: Timeout after which, if the file still exists, it will be deleted.
        Returns:
        Timeout after which, if the file still exists, it will be deleted.
      • setTimeoutMS

        public TempFileInputStream setTimeoutMS​(Long newTimeoutMS)
        Setter for timeoutMS: Timeout after which, if the file still exists, it will be deleted.
        Parameters:
        newTimeoutMS - Timeout after which, if the file still exists, it will be deleted.
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws IOException
        Overridden to pass through the call to fis
        Overrides:
        read in class InputStream
        Throws:
        IOException - On IO error.
      • mark

        public void mark​(int readlimit)
        Overridden to pass through the call to fis
        Overrides:
        mark in class InputStream
      • markSupported

        public boolean markSupported()
        Overridden to pass through the call to fis
        Overrides:
        markSupported in class InputStream