Package com.globalmentor.io
Class TempFileInputStream
- java.lang.Object
-
- java.io.InputStream
-
- com.globalmentor.io.InputStreamDecorator<java.io.FileInputStream>
-
- com.globalmentor.io.TempFileInputStream
-
- All Implemented Interfaces:
Disposable
,java.io.Closeable
,java.lang.AutoCloseable
public class TempFileInputStream extends InputStreamDecorator<java.io.FileInputStream>
An input stream to a file that, when closed, deletes the file.It is very important to properly close this output stream when finished using it; otherwise, orphaned temporary files may remain.
- Author:
- Garret Wilson
-
-
Constructor Summary
Constructors Constructor Description TempFileInputStream(java.io.File tempFile)
File constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close(boolean closeDecoratedStream)
Closes this input stream and releases any system resources associated with the stream.void
dispose()
Uninitializes the object.-
Methods inherited from class com.globalmentor.io.InputStreamDecorator
afterClose, available, beforeClose, close, finalize, getInputStream, mark, markSupported, read, read, read, reset, skip
-
-
-
-
Constructor Detail
-
TempFileInputStream
public TempFileInputStream(java.io.File tempFile) throws java.io.FileNotFoundException
File constructor.- Parameters:
tempFile
- The temporary file to which an input stream should be created.- Throws:
java.lang.NullPointerException
- if the given file isnull
.java.io.FileNotFoundException
- if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
-
-
Method Detail
-
close
public void close(boolean closeDecoratedStream) throws java.io.IOException
Closes this input stream and releases any system resources associated with the stream. A closed stream cannot perform output operations and cannot be reopened. If auto-dispose is enabled,InputStreamDecorator.dispose()
will be called if closing is successful. This version does not allow closing without closing the decorated stream.- Overrides:
close
in classInputStreamDecorator<java.io.FileInputStream>
- Parameters:
closeDecoratedStream
- Whether the decorated stream should also be closed.- Throws:
java.lang.IllegalArgumentException
- if the close decorated stream flag isfalse
.java.io.IOException
- if an I/O error occurs.- See Also:
InputStreamDecorator.beforeClose()
,InputStreamDecorator.afterClose()
,InputStreamDecorator.dispose()
-
dispose
public void dispose()
Uninitializes the object. After calling this method, the object should not be used further. This method can be called multiple times without danger. All exceptions should be caught and dealt with in this method. Child classes must call the parent class version. This deletes the temporary file, if any. Because this method is called indirectly fromInputStreamDecorator.close()
, this results in the temporary file being deleted after the input stream is closed.- Specified by:
dispose
in interfaceDisposable
- Overrides:
dispose
in classInputStreamDecorator<java.io.FileInputStream>
-
-