com.continuent.tungsten.common.file
Class FileIO

java.lang.Object
  extended by com.continuent.tungsten.common.file.FileIO

public class FileIO
extends java.lang.Object

Implements simple utility methods for storing and retrieving files located under a base directory. This class is designed to support operations on both standard Linux file systems as well as Hadoop. For this reason we use the package-defined FileIOException as a covering exception for all underlying exception types instead of exceptions like IOException that only apply to on particular type of file system.


Nested Class Summary
 class FileIO.LocalFilenameFilter
          Internal filter class to select file names based on a prefix.
 
Constructor Summary
FileIO()
           
 
Method Summary
 boolean delete(FilePath path)
          Delete path.
 boolean delete(FilePath path, boolean recursive)
          Delete path and optionally any children.
 boolean exists(FilePath path)
          Returns true if path exists.
 boolean isDirectory(FilePath path)
          Returns true if path is a directory.
 boolean isFile(FilePath path)
          Returns true if path is an ordinary file.
 java.lang.String[] list(FilePath path)
          Return a list of the names of children of this path.
 java.lang.String[] list(FilePath path, java.lang.String prefix)
          Return a list of the names of children of this path that start with the given prefix.
 boolean mkdir(FilePath path)
          Create path as a new directory.
 boolean mkdirs(FilePath path)
          Create path as a new directory including any intervening directories in the path.
 java.lang.String read(FilePath path)
          Returns the value of the contents of a file as a string using UTF-8 as charset encoding.
 java.lang.String read(FilePath path, java.lang.String charset)
          Returns the value of the contents of a file as a string.
 boolean readable(FilePath path)
          Returns true if path is readable.
 boolean writable(FilePath path)
          Returns true if path is writable.
 void write(FilePath path, java.lang.String value)
          Write data to file system using UTF-8 charset for file encoding and with flush only.
 void write(FilePath path, java.lang.String value, java.lang.String charset)
          Write data to file system with flush only.
 void write(FilePath path, java.lang.String value, java.lang.String charset, boolean fsync)
          Writes a string into a file, replacing an existing contents.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileIO

public FileIO()
Method Detail

exists

public boolean exists(FilePath path)
Returns true if path exists.


isFile

public boolean isFile(FilePath path)
Returns true if path is an ordinary file.


isDirectory

public boolean isDirectory(FilePath path)
Returns true if path is a directory.


writable

public boolean writable(FilePath path)
Returns true if path is writable.


readable

public boolean readable(FilePath path)
Returns true if path is readable.


list

public java.lang.String[] list(FilePath path)
Return a list of the names of children of this path.

Parameters:
path - Path to search
Returns:
An array of path names, which will be empty if there are no children

list

public java.lang.String[] list(FilePath path,
                               java.lang.String prefix)
Return a list of the names of children of this path that start with the given prefix.

Parameters:
path - Path to search
prefix - Required file name prefix or null to return all children
Returns:
An array of path names, which will be empty if there are no children

mkdir

public boolean mkdir(FilePath path)
Create path as a new directory.

Parameters:
path - Path to create
Returns:
true if successful

mkdirs

public boolean mkdirs(FilePath path)
Create path as a new directory including any intervening directories in the path.

Parameters:
path - Path to create
Returns:
true if successful

delete

public boolean delete(FilePath path)
Delete path. This form ignored children.

Parameters:
path - Path to delete
Returns:
true if fully successful, otherwise false.

delete

public boolean delete(FilePath path,
                      boolean recursive)
Delete path and optionally any children. Recursive deletes fail if we cannot delete all children as well as the original path.

Parameters:
path - Path to delete
recursive - If true delete child files/directories as well
Returns:
true if fully successful, otherwise false.

write

public void write(FilePath path,
                  java.lang.String value)
           throws FileIOException
Write data to file system using UTF-8 charset for file encoding and with flush only.

Parameters:
path - The file path
value - The string to write in the file
Throws:
FileIOException - Thrown if file is not writable

write

public void write(FilePath path,
                  java.lang.String value,
                  java.lang.String charset)
           throws FileIOException
Write data to file system with flush only.

Parameters:
path - The file path
value - The string to write in the file
charset - Character set of file data (e.g., UTF-8)
Throws:
FileIOException - Thrown if file is not writable

write

public void write(FilePath path,
                  java.lang.String value,
                  java.lang.String charset,
                  boolean fsync)
           throws FileIOException
Writes a string into a file, replacing an existing contents. There are two durability options. If fsync is true, we issue the Java equivalent of fsync, which is generally sufficient to survive a file system crash. If fsync is false, we just flush, which will generally survive a process crash.

Parameters:
path - The file path
value - The string to write in the file
charset - Character set of file data (e.g., UTF-8)
fsync - If true issue an fsync, otherwise just flush
Throws:
FileIOException - Thrown if file is not writable

read

public java.lang.String read(FilePath path)
                      throws FileIOException
Returns the value of the contents of a file as a string using UTF-8 as charset encoding.

Parameters:
path - The file path
Returns:
Contents of the file, which is an empty string for a 0-length file
Throws:
FileIOException - Thrown if file is not readable

read

public java.lang.String read(FilePath path,
                             java.lang.String charset)
                      throws FileIOException
Returns the value of the contents of a file as a string.

Parameters:
path - The file path
charset - Character set of file data (e.g., UTF-8)
Returns:
Contents of the file, which is an empty string for a 0-length file
Throws:
FileIOException - Thrown if file is not readable