Package org.apache.tika.io
Class TemporaryResources
- java.lang.Object
-
- org.apache.tika.io.TemporaryResources
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class TemporaryResources extends java.lang.Object implements java.io.CloseableUtility class for tracking and ultimately closing or otherwise disposing a collection of temporary resources.Note that this class is not thread-safe.
- Since:
- Apache Tika 0.10
-
-
Constructor Summary
Constructors Constructor Description TemporaryResources()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddResource(java.io.Closeable resource)Adds a new resource to the set of tracked resources that will all be closed when theclose()method is called.voidclose()Closes all tracked resources.java.nio.file.PathcreateTempFile()Creates a temporary file that will automatically be deleted when theclose()method is called, returning its path.java.io.FilecreateTemporaryFile()Creates and returns a temporary file that will automatically be deleted when theclose()method is called.voiddispose()Calls theclose()method and wraps the potentialIOExceptioninto aTikaExceptionfor convenience when used within Tika.<T extends java.io.Closeable>
TgetResource(java.lang.Class<T> klass)Returns the latest of the tracked resources that implements or extends the given interface or class.voidsetTemporaryFileDirectory(java.io.File tempFileDir)Sets the directory to be used for the temporary files created by thecreateTempFile()method.voidsetTemporaryFileDirectory(java.nio.file.Path tempFileDir)Sets the directory to be used for the temporary files created by thecreateTempFile()method.
-
-
-
Method Detail
-
setTemporaryFileDirectory
public void setTemporaryFileDirectory(java.nio.file.Path tempFileDir)
Sets the directory to be used for the temporary files created by thecreateTempFile()method.- Parameters:
tempFileDir- temporary file directory, ornullfor the system default
-
setTemporaryFileDirectory
public void setTemporaryFileDirectory(java.io.File tempFileDir)
Sets the directory to be used for the temporary files created by thecreateTempFile()method.- Parameters:
tempFileDir- temporary file directory, ornullfor the system default- See Also:
setTemporaryFileDirectory(Path)
-
createTempFile
public java.nio.file.Path createTempFile() throws java.io.IOExceptionCreates a temporary file that will automatically be deleted when theclose()method is called, returning its path.- Returns:
- Path to created temporary file that will be deleted after closing
- Throws:
java.io.IOException
-
createTemporaryFile
public java.io.File createTemporaryFile() throws java.io.IOExceptionCreates and returns a temporary file that will automatically be deleted when theclose()method is called.- Returns:
- Created temporary file that'll be deleted after closing
- Throws:
java.io.IOException- See Also:
createTempFile()
-
addResource
public void addResource(java.io.Closeable resource)
Adds a new resource to the set of tracked resources that will all be closed when theclose()method is called.- Parameters:
resource- resource to be tracked
-
getResource
public <T extends java.io.Closeable> T getResource(java.lang.Class<T> klass)
Returns the latest of the tracked resources that implements or extends the given interface or class.- Parameters:
klass- interface or class- Returns:
- matching resource, or
nullif not found
-
close
public void close() throws java.io.IOExceptionCloses all tracked resources. The resources are closed in reverse order from how they were added.Any suppressed exceptions from managed resources are collected and then added to the first thrown exception, which is re-thrown once all the resources have been closed.
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException- if one or more of the tracked resources could not be closed
-
dispose
public void dispose() throws TikaExceptionCalls theclose()method and wraps the potentialIOExceptioninto aTikaExceptionfor convenience when used within Tika.- Throws:
TikaException- if one or more of the tracked resources could not be closed
-
-