com.microsoft.tfs.util.temp
Class TempStorageService

java.lang.Object
  extended by com.microsoft.tfs.util.temp.TempStorageService

public final class TempStorageService
extends java.lang.Object

Creates temporary files and directories with more flexibility than the File class. Temp items can be deleted immediately with cleanUpItem(File). By default items the service creates will be deleted when the JVM shuts down, but this behavior can be changed per-item with forgetItem(File).

This class is a singleton.

Thread-safety:
thread-safe
Since:
TEE-SDK-10.1

Field Summary
static java.lang.String DEFAULT_EXTENSION
          The default extension for files we create.
 
Method Summary
 void cleanUpAllItems()
          Cleans up (deletes) files and directories allocated by this service.
 void cleanUpItem(java.io.File item)
           Cleans up (deletes) a file or directory which was allocated by this service.
 java.io.File createTempDirectory()
          Creates a temporary directory (named after a new GUID) inside the system's default temporary directory and returns its full path.
 java.io.File createTempFile()
           Creates a new empty file in a new directory inside the system's default temporary directory.
 java.io.File createTempFile(java.io.File userDirectory, java.lang.String extension)
           Creates a new empty file with the specified extension in the specified directory.
 java.io.File createTempFile(java.lang.String extension)
           Creates a new empty file with the specified extension in a new directory inside the system's default temporary directory.
 void forgetItem(java.io.File item)
          Prevents the TempStorageService from cleaning up (deleting) this item automatically in the future.
static TempStorageService getInstance()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_EXTENSION

public static final java.lang.String DEFAULT_EXTENSION
The default extension for files we create.

See Also:
Constant Field Values
Constant Field Value:
".tmp"
Method Detail

getInstance

public static TempStorageService getInstance()
Returns:
the single instance of TempStorageService.

createTempFile

public java.io.File createTempFile()
                            throws java.io.IOException

Creates a new empty file in a new directory inside the system's default temporary directory. See createTempFile(File, String) for how to delete the file.

Returns:
the empty temp file that was created
Throws:
java.io.IOException - if a filesystem error occurred creating the temporary file

createTempFile

public java.io.File createTempFile(java.lang.String extension)
                            throws java.io.IOException

Creates a new empty file with the specified extension in a new directory inside the system's default temporary directory. See createTempFile(File, String) for how to delete the file.

Parameters:
extension - the extension (including the '.') to use for the temporary file's name (pass null or empty to use the default, DEFAULT_EXTENSION)
Returns:
the empty temp file that was created
Throws:
java.io.IOException - if a filesystem error occurred creating the temporary file

createTempFile

public java.io.File createTempFile(java.io.File userDirectory,
                                   java.lang.String extension)
                            throws java.io.IOException

Creates a new empty file with the specified extension in the specified directory.

When done using the file, do one of:

Parameters:
userDirectory - the directory in which to create this file (which must already exist). If null or empty, a new directory is created inside the system's temporary location and the file is created there and that directory is automatically cleaned up when the file is cleaned up
extension - the extension (including the '.') to use for the temporary file (pass null or empty to use the default).
Returns:
the temp file that was created
Throws:
java.io.IOException - if a filesystem error occurred creating the temporary file.

createTempDirectory

public java.io.File createTempDirectory()
                                 throws java.io.IOException
Creates a temporary directory (named after a new GUID) inside the system's default temporary directory and returns its full path. When you're done with the object, you may call cleanUpItem(File) to have it deleted.

If you do not call cleanUpItem(File) or forgetItem(File) in the future, any future call to cleanUpAllItems() will clean it up for you.

Returns:
the temp directory that was created
Throws:
java.io.IOException - if a filesystem error occurred creating the temporary directory.

forgetItem

public void forgetItem(java.io.File item)
Prevents the TempStorageService from cleaning up (deleting) this item automatically in the future.

Parameters:
item - the file or directory created by TempStorageService that should not be cleaned up (deleted) by TempStorageService in the future (must not be null)

cleanUpItem

public void cleanUpItem(java.io.File item)

Cleans up (deletes) a file or directory which was allocated by this service. If the item is a file in a directory which was also created by the service, that directory is cleaned up (recursively).

If the path was not allocated by this TempStorageService or was already cleaned up, no exception is thrown and the item is ignored.

Parameters:
item - an item that was created by createTempFile() or createTempDirectory() (must not be null)

cleanUpAllItems

public void cleanUpAllItems()
Cleans up (deletes) files and directories allocated by this service. Can be called multiple times on a single TempStorageService instance.



© 2012 Microsoft. All rights reserved.