|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.terrier.utility.Files
public class Files
Utililty class for opening readers/writers and input/output streams to files. Handles gzipped and bzipped files
on the fly, ie if a file ends in ".gz" or ".GZ", then it will be opened using a GZipInputStream/GZipOutputStream. ".bz2"
files are handled in a similar fashion. All returned Streams, Readers, Writers etc are Buffered. If a charset encoding is not specified, then the system default is used.
New interfaces are used to descibe random data access.
FileSystem plugsin
Additional file systems can be plugged into this module, by calling the addFileSystemCapability() method. FileSystems have read and/or write capabilities, as specified using the FSCapability constants. Files using these external file systems should be denoted by scheme prefixes - eg ftp://, http:// etc. NB: file:// is the default scheme
Support for additional stream compression & decompression can be plugged in by calling addFilterInputStreamMapping().
Terrier can cache files which will see heavy IO activity. In particular, files mentioned in the files.to.cache property will be cached to the default temporary folder. There are also API method to populate the cache with files. For all methods, java.io.tmpdir is the default temporary directory. An IOException will occur if caching fails for some reason.
Nested Class Summary | |
---|---|
static interface |
Files.FSCapability
constants declaring which capabilites a file system has |
protected static class |
Files.PathTransformation
a search regex and a replacement for path transformations |
Field Summary | |
---|---|
protected static java.lang.String |
DEFAULT_SCHEME
default scheme |
protected static java.util.Map<java.lang.String,FileSystem> |
fileSystems
map of scheme to FileSystem implementation |
protected static java.util.List<Files.PathTransformation> |
pathTransformations
transformations to apply to a path |
Constructor Summary | |
---|---|
Files()
|
Method Summary | |
---|---|
static void |
addFileSystemCapability(FileSystem fs)
Add a file system to Terrier. |
static void |
addFilterInputStreamMapping(java.lang.String regex,
java.lang.Class<? extends java.io.InputStream> inputStreamClass,
java.lang.Class<? extends java.io.OutputStream> outputStreamClass)
Add a filter mapping to the Files layes. |
static void |
addPathTransormation(java.lang.String find,
java.lang.String replace)
add a static transformation to apply to a path. |
static void |
cacheFile(java.lang.String filename)
Cache to the temporary directory specified by java.io.tmpdir System property. |
static void |
cacheFile(java.lang.String filename,
java.lang.String temporaryFolder)
Cache file to specified temporary folder |
static boolean |
canRead(java.lang.String filename)
returns true iff path can be read |
static boolean |
canWrite(java.lang.String filename)
returns true iff path can be read |
static java.lang.Long |
copyFile(java.io.File srcFile,
java.io.File destFile)
Copy a file from srcFile to destFile. |
static java.lang.Long |
copyFile(java.io.InputStream in,
java.io.OutputStream out)
Copy all bytes from in to out |
static java.lang.Long |
copyFile(java.lang.String srcFilename,
java.lang.String destFilename)
Copy a file from srcFile to destFile. |
static java.lang.Long |
createChecksum(java.io.File file)
Returns the CRC checksum of denoted file |
static boolean |
delete(java.lang.String filename)
Delete the named file. |
static boolean |
deleteOnExit(java.lang.String path)
Mark the named path as to be deleted on exit. |
static boolean |
exists(java.lang.String path)
returns true iff the path is really a path |
protected static FileSystem |
getFileSystem(java.lang.String filename)
derive the file system to use that is associated with the scheme in the specified filename. |
static java.lang.String |
getFileSystemName(java.lang.String path)
Get the name of the file system that would be used to access a given file or directory. |
static java.lang.String |
getParent(java.lang.String path)
What is the parent path to the specified path? |
protected static void |
initialise_mappings()
initialise the default compression mappings |
protected static void |
initialise_static_cache()
we may have been specified some files to cache immediately |
protected static void |
intialise_transformations()
initialise the transformations from Application property |
static boolean |
isDirectory(java.lang.String path)
return true if path is a directory |
static long |
length(java.io.File f)
returns the length of file f |
static long |
length(java.lang.String filename)
returns the length of the file, or 0L if cannot be found etc |
static java.lang.String[] |
list(java.lang.String path)
List the contents of a directory |
static void |
main(java.lang.String[] args)
Check that the a specified file exists as per Terrier's file system abstraction layer |
static boolean |
mkdir(java.lang.String path)
returns true if the specificed path can be made as a directory |
protected static java.io.InputStream |
openFile(java.lang.String filename)
Opens an OutputStream to a file called Filename, processing all allowed writable file systems named in writeFileSystemPrefixes |
static RandomDataInput |
openFileRandom(java.io.File file)
Open a file for random access reading |
static RandomDataInput |
openFileRandom(java.lang.String filename)
Returns a RandomAccessFile implementation accessing the specificed file |
static java.io.BufferedReader |
openFileReader(java.io.File file)
Opens a reader to the file called file. |
static java.io.BufferedReader |
openFileReader(java.io.File file,
java.lang.String charset)
Opens a reader to the file called filename. |
static java.io.BufferedReader |
openFileReader(java.lang.String filename)
Opens a reader to the file called filename. |
static java.io.BufferedReader |
openFileReader(java.lang.String filename,
java.lang.String charset)
Opens a reader to the file called filename. |
static java.io.InputStream |
openFileStream(java.io.File file)
Opens an InputStream to a file called file. |
static java.io.InputStream |
openFileStream(java.lang.String filename)
Opens an InputStream to a file called filename. |
static boolean |
rename(java.lang.String sourceFilename,
java.lang.String destFilename)
rename a file or directory. |
protected static java.lang.String |
transform(java.lang.String filename)
apply any transformations to the specified filename |
protected static java.io.OutputStream |
writeFile(java.lang.String filename)
Opens an OutputStream to a file called filename, using the filesystem named in the scheme component of the filename. |
static RandomDataOutput |
writeFileRandom(java.io.File file)
Open a file for random access writing and reading |
static RandomDataOutput |
writeFileRandom(java.lang.String filename)
Returns a RandomAccessFile implementation accessing the specificed file |
static java.io.OutputStream |
writeFileStream(java.io.File file)
Opens an OutputStream to a file called file. |
static java.io.OutputStream |
writeFileStream(java.lang.String filename)
Opens an OutputStream to a file called filename. |
static java.io.Writer |
writeFileWriter(java.io.File file)
Opens an Writer to a file called file. |
static java.io.Writer |
writeFileWriter(java.io.File file,
java.lang.String charset)
Opens an Writer to a file called file. |
static java.io.Writer |
writeFileWriter(java.lang.String filename)
Opens an Writer to a file called file. |
static java.io.Writer |
writeFileWriter(java.lang.String filename,
java.lang.String charset)
Opens an Writer to a file called file. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected static final java.util.Map<java.lang.String,FileSystem> fileSystems
protected static final java.util.List<Files.PathTransformation> pathTransformations
protected static final java.lang.String DEFAULT_SCHEME
Constructor Detail |
---|
public Files()
Method Detail |
---|
public static void addFilterInputStreamMapping(java.lang.String regex, java.lang.Class<? extends java.io.InputStream> inputStreamClass, java.lang.Class<? extends java.io.OutputStream> outputStreamClass)
addFilterInputStreamMapping(".+\\.gz$", GZIPInputStream.class, GZIPOutputStream.class); addFilterInputStreamMapping(".+\\.GZ$", GZIPInputStream.class, GZIPOutputStream.class);
regex
- Regular expression that the filename must match to require the filter streaminputStreamClass
- Class extending InputStream that decompresses the fileoutputStreamClass
- Class extending OutputStream that compresses the fileprotected static void initialise_static_cache()
protected static void intialise_transformations()
protected static void initialise_mappings()
public static void cacheFile(java.lang.String filename) throws java.io.IOException
java.io.IOException
public static void cacheFile(java.lang.String filename, java.lang.String temporaryFolder) throws java.io.IOException
java.io.IOException
public static void addPathTransormation(java.lang.String find, java.lang.String replace)
public static void addFileSystemCapability(FileSystem fs)
protected static java.lang.String transform(java.lang.String filename)
protected static FileSystem getFileSystem(java.lang.String filename)
filename
- public static java.lang.String getFileSystemName(java.lang.String path)
path
-
protected static java.io.InputStream openFile(java.lang.String filename) throws java.io.IOException
filename
- Filename of file to open
java.io.IOException
protected static java.io.OutputStream writeFile(java.lang.String filename) throws java.io.IOException
filename
- Filename of file to open, optionally including scheme
java.io.IOException
public static RandomDataInput openFileRandom(java.lang.String filename) throws java.io.IOException
java.io.IOException
public static RandomDataOutput writeFileRandom(java.lang.String filename) throws java.io.IOException
java.io.IOException
public static boolean delete(java.lang.String filename)
filename
- path to file to deletepublic static boolean deleteOnExit(java.lang.String path)
public static boolean exists(java.lang.String path)
public static boolean canRead(java.lang.String filename)
public static boolean canWrite(java.lang.String filename)
public static boolean mkdir(java.lang.String path)
public static long length(java.lang.String filename)
public static boolean isDirectory(java.lang.String path)
public static boolean rename(java.lang.String sourceFilename, java.lang.String destFilename)
public static java.lang.String getParent(java.lang.String path)
public static java.lang.String[] list(java.lang.String path)
public static java.io.BufferedReader openFileReader(java.io.File file) throws java.io.IOException
file
- File to open.
java.io.IOException
public static java.io.BufferedReader openFileReader(java.io.File file, java.lang.String charset) throws java.io.IOException
file
- File to open.charset
- Character set encoding of file. null for system default.
java.io.IOException
public static java.io.BufferedReader openFileReader(java.lang.String filename) throws java.io.IOException
filename
- File to open.
java.io.IOException
public static java.io.BufferedReader openFileReader(java.lang.String filename, java.lang.String charset) throws java.io.IOException
filename
- File to open.charset
- Character set encoding of file. null for system default.
java.io.IOException
public static java.io.InputStream openFileStream(java.io.File file) throws java.io.IOException
file
- File to open.
java.io.IOException
public static RandomDataInput openFileRandom(java.io.File file) throws java.io.IOException
java.io.IOException
public static java.io.InputStream openFileStream(java.lang.String filename) throws java.io.IOException
filename
- File to open.
java.io.IOException
public static java.io.OutputStream writeFileStream(java.io.File file) throws java.io.IOException
file
- File to open.
java.io.IOException
public static RandomDataOutput writeFileRandom(java.io.File file) throws java.io.IOException
java.io.IOException
public static java.io.OutputStream writeFileStream(java.lang.String filename) throws java.io.IOException
filename
- File to open.
java.io.IOException
public static java.io.Writer writeFileWriter(java.io.File file) throws java.io.IOException
file
- File to open.
java.io.IOException
public static java.io.Writer writeFileWriter(java.io.File file, java.lang.String charset) throws java.io.IOException
file
- File to open.charset
- Character set encoding of file. null for system default.
java.io.IOException
public static java.io.Writer writeFileWriter(java.lang.String filename) throws java.io.IOException
filename
- File to open.
java.io.IOException
public static java.io.Writer writeFileWriter(java.lang.String filename, java.lang.String charset) throws java.io.IOException
filename
- File to open.charset
- Character set encoding of file. null for system default.
java.io.IOException
public static java.lang.Long copyFile(java.lang.String srcFilename, java.lang.String destFilename) throws java.io.IOException
java.io.IOException
- if there was a problem copyingpublic static java.lang.Long copyFile(java.io.File srcFile, java.io.File destFile) throws java.io.IOException
java.io.IOException
- if there was a problem copyingpublic static java.lang.Long copyFile(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
java.io.IOException
public static java.lang.Long createChecksum(java.io.File file) throws java.io.IOException
java.io.IOException
public static long length(java.io.File f)
public static void main(java.lang.String[] args)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |