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 int
available()
Overridden to pass through the call tofis
void
close()
Overridden to delete the file after closing the streamBoolean
getDeleteOnClose()
Getter forbDeleteOnClose
: Whether to delete the file when close() is called or not.File
getTempFile()
Getter forfTempFile
: The temporary file for deletion.Long
getTimeoutMS()
Getter fortimeoutMS
: Timeout after which, if the file still exists, it will be deleted.void
mark(int readlimit)
Overridden to pass through the call tofis
boolean
markSupported()
Overridden to pass through the call tofis
int
read()
Overridden to pass through the call tofis
int
read(byte[] b)
Overridden to pass through the call tofis
int
read(byte[] b, int off, int len)
Overridden to pass through the call tofis
void
reset()
Overridden to pass through the call tofis
TempFileInputStream
setDeleteOnClose(Boolean bNewDeleteOnClose)
Setter forbDeleteOnClose
: Whether to delete the file when close() is called or not.TempFileInputStream
setTempFile(File fNewTempFile)
Setter forfTempFile
: The temporary file for deletion.TempFileInputStream
setTimeoutMS(Long newTimeoutMS)
Setter fortimeoutMS
: Timeout after which, if the file still exists, it will be deleted.long
skip(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 IOException
Overridden to delete the file after closing the stream- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
- On IO error.
-
read
public int read() throws IOException
Overridden to pass through the call tofis
- Specified by:
read
in classInputStream
- Throws:
IOException
- On IO error.
-
read
public int read(byte[] b) throws IOException
Overridden to pass through the call tofis
- Overrides:
read
in classInputStream
- Throws:
IOException
- On IO error.
-
read
public int read(byte[] b, int off, int len) throws IOException
Overridden to pass through the call tofis
- Overrides:
read
in classInputStream
- Throws:
IOException
- On IO error.
-
skip
public long skip(long n) throws IOException
Overridden to pass through the call tofis
- Overrides:
skip
in classInputStream
- Throws:
IOException
- On IO error.
-
available
public int available() throws IOException
Overridden to pass through the call tofis
- Overrides:
available
in classInputStream
- Throws:
IOException
- On IO error.
-
mark
public void mark(int readlimit)
Overridden to pass through the call tofis
- Overrides:
mark
in classInputStream
-
reset
public void reset() throws IOException
Overridden to pass through the call tofis
- Overrides:
reset
in classInputStream
- Throws:
IOException
- On IO error.
-
markSupported
public boolean markSupported()
Overridden to pass through the call tofis
- Overrides:
markSupported
in classInputStream
-
-