com.twitter.common.io
Class FileUtils.Temporary

java.lang.Object
  extended by com.twitter.common.io.FileUtils.Temporary
Enclosing class:
FileUtils

public static class FileUtils.Temporary
extends Object

A utility for creating and working with temporary files and directories.


Constructor Summary
FileUtils.Temporary(File basedir)
          Creates a new temporary utility that creates files and directories rooted at basedir.
 
Method Summary
 File createDir()
          Returns a new empty temporary directory.
 File createFile()
          Creates a new empty temporary file.
 File createFile(String suffix)
          Creates a new empty temporary file with the given filename suffix.
<E extends Exception>
void
doWithDir(ExceptionalClosure<File,E> work)
          Creates a new temporary directory and executes the unit of work against it ensuring the directory and its contents are removed after the work completes normally or abnormally.
<T,E extends Exception>
T
doWithDir(ExceptionalFunction<File,T,E> work)
          Creates a new temporary directory and executes the unit of work against it ensuring the directory and its contents are removed after the work completes normally or abnormally.
<E extends Exception>
void
doWithFile(ExceptionalClosure<File,E> work)
          Creates a new temporary file and executes the unit of work against it ensuring the file is removed after the work completes normally or abnormally.
<T,E extends Exception>
T
doWithFile(ExceptionalFunction<File,T,E> work)
          Creates a new temporary file and executes the unit of work against it ensuring the file is removed after the work completes normally or abnormally.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtils.Temporary

public FileUtils.Temporary(File basedir)
Creates a new temporary utility that creates files and directories rooted at basedir.

Parameters:
basedir - The base directory to generate temporary files and directories in.
Method Detail

createDir

public File createDir()
Returns a new empty temporary directory.

Returns:
a file representing the newly created directory.
Throws:
IllegalStateException - if a new temporary directory could not be created

createFile

public File createFile()
                throws IOException
Creates a new empty temporary file.

Returns:
a new empty temporary file
Throws:
IOException - if there was a problem creating a new temporary file

createFile

public File createFile(String suffix)
                throws IOException
Creates a new empty temporary file with the given filename suffix.

Parameters:
suffix - The suffix for the temporary file name
Returns:
a new empty temporary file
Throws:
IOException - if there was a problem creating a new temporary file

doWithDir

public <E extends Exception> void doWithDir(ExceptionalClosure<File,E> work)
               throws E extends Exception
Creates a new temporary directory and executes the unit of work against it ensuring the directory and its contents are removed after the work completes normally or abnormally.

Type Parameters:
E - The type of exception this unit of work can throw.
Parameters:
work - The unit of work to execute against the new temporary directory.
Throws:
E - bubbled transparently when the unit of work throws
E extends Exception

doWithDir

public <T,E extends Exception> T doWithDir(ExceptionalFunction<File,T,E> work)
            throws E extends Exception
Creates a new temporary directory and executes the unit of work against it ensuring the directory and its contents are removed after the work completes normally or abnormally.

Type Parameters:
T - The type of result this unit of work produces.
E - The type of exception this unit of work can throw.
Parameters:
work - The unit of work to execute against the new temporary directory.
Returns:
the result when the unit of work completes successfully
Throws:
E - bubbled transparently when the unit of work throws
E extends Exception

doWithFile

public <E extends Exception> void doWithFile(ExceptionalClosure<File,E> work)
                throws E extends Exception,
                       IOException
Creates a new temporary file and executes the unit of work against it ensuring the file is removed after the work completes normally or abnormally.

Type Parameters:
E - The type of exception this unit of work can throw.
Parameters:
work - The unit of work to execute against the new temporary file.
Throws:
E - bubbled transparently when the unit of work throws
IOException - if there was a problem creating a new temporary file
E extends Exception

doWithFile

public <T,E extends Exception> T doWithFile(ExceptionalFunction<File,T,E> work)
             throws E extends Exception,
                    IOException
Creates a new temporary file and executes the unit of work against it ensuring the file is removed after the work completes normally or abnormally.

Type Parameters:
T - The type of result this unit of work produces.
E - The type of exception this unit of work can throw.
Parameters:
work - The unit of work to execute against the new temporary file.
Returns:
the result when the unit of work completes successfully
Throws:
E - bubbled transparently when the unit of work throws
IOException - if there was a problem creating a new temporary file
E extends Exception