Package nzilbb.util
Class TempFileInputStream
- java.lang.Object
-
- java.io.InputStream
-
- nzilbb.util.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 Summary
Constructors Constructor Description TempFileInputStream(File file)Constructor.TempFileInputStream(File file, Long timeoutMS)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Overridden to pass through the call tofisvoidclose()Overridden to delete the file after closing the streamBooleangetDeleteOnClose()Getter forbDeleteOnClose: Whether to delete the file when close() is called or not.FilegetTempFile()Getter forfTempFile: The temporary file for deletion.LonggetTimeoutMS()Getter fortimeoutMS: Timeout after which, if the file still exists, it will be deleted.voidmark(int readlimit)Overridden to pass through the call tofisbooleanmarkSupported()Overridden to pass through the call tofisintread()Overridden to pass through the call tofisintread(byte[] b)Overridden to pass through the call tofisintread(byte[] b, int off, int len)Overridden to pass through the call tofisvoidreset()Overridden to pass through the call tofisTempFileInputStreamsetDeleteOnClose(Boolean bNewDeleteOnClose)Setter forbDeleteOnClose: Whether to delete the file when close() is called or not.TempFileInputStreamsetTempFile(File fNewTempFile)Setter forfTempFile: The temporary file for deletion.TempFileInputStreamsetTimeoutMS(Long newTimeoutMS)Setter fortimeoutMS: Timeout after which, if the file still exists, it will be deleted.longskip(long n)Overridden to pass through the call tofis-
Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
-
-
-
-
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 forfTempFile: The temporary file for deletion.- Returns:
- The temporary file for deletion.
-
setTempFile
public TempFileInputStream setTempFile(File fNewTempFile)
Setter forfTempFile: 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 forbDeleteOnClose: 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 forbDeleteOnClose: 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 fortimeoutMS: 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 fortimeoutMS: 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.
-
close
public void close() throws IOExceptionOverridden to delete the file after closing the stream- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- On IO error.
-
read
public int read() throws IOExceptionOverridden to pass through the call tofis- Specified by:
readin classInputStream- Throws:
IOException- On IO error.
-
read
public int read(byte[] b) throws IOExceptionOverridden to pass through the call tofis- Overrides:
readin classInputStream- Throws:
IOException- On IO error.
-
read
public int read(byte[] b, int off, int len) throws IOExceptionOverridden to pass through the call tofis- Overrides:
readin classInputStream- Throws:
IOException- On IO error.
-
skip
public long skip(long n) throws IOExceptionOverridden to pass through the call tofis- Overrides:
skipin classInputStream- Throws:
IOException- On IO error.
-
available
public int available() throws IOExceptionOverridden to pass through the call tofis- Overrides:
availablein classInputStream- Throws:
IOException- On IO error.
-
mark
public void mark(int readlimit)
Overridden to pass through the call tofis- Overrides:
markin classInputStream
-
reset
public void reset() throws IOExceptionOverridden to pass through the call tofis- Overrides:
resetin classInputStream- Throws:
IOException- On IO error.
-
markSupported
public boolean markSupported()
Overridden to pass through the call tofis- Overrides:
markSupportedin classInputStream
-
-