public class IOUtil
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DICT_FILE_EXTENSION |
static long |
FIVE_GBS |
static java.lang.String |
INTERVAL_LIST_FILE_EXTENSION |
static long |
ONE_GB |
static java.lang.String |
SAM_FILE_EXTENSION |
static int |
STANDARD_BUFFER_SIZE
Deprecated.
Use
Defaults.NON_ZERO_BUFFER_SIZE instead. |
static long |
TWO_GBS |
static java.lang.String[] |
VCF_EXTENSIONS
Possible extensions for VCF files and related formats.
|
Constructor and Description |
---|
IOUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
assertDirectoryIsReadable(java.io.File dir)
Checks that a directory is non-null, extent, readable and a directory
otherwise a runtime exception is thrown.
|
static void |
assertDirectoryIsWritable(java.io.File dir)
Checks that a directory is non-null, extent, writable and a directory
otherwise a runtime exception is thrown.
|
static void |
assertFileIsReadable(java.io.File file)
Checks that a file is non-null, exists, is not a directory and is readable.
|
static void |
assertFileIsReadable(java.nio.file.Path path)
Checks that a file is non-null, exists, is not a directory and is readable.
|
static void |
assertFileIsWritable(java.io.File file)
Checks that a file is non-null, and is either extent and writable, or non-existent but
that the parent directory exists and is writable.
|
static void |
assertFilesAreReadable(java.util.List<java.io.File> files)
Checks that each file is non-null, exists, is not a directory and is readable.
|
static void |
assertFilesAreWritable(java.util.List<java.io.File> files)
Checks that each file is non-null, and is either extent and writable, or non-existent but
that the parent directory exists and is writable.
|
static void |
assertFilesEqual(java.io.File f1,
java.io.File f2)
Checks that the two files are the same length, and have the same content, otherwise throws a runtime exception.
|
static void |
assertFileSizeNonZero(java.io.File file)
Checks that a file is of non-zero length
|
static void |
assertInputIsValid(java.lang.String input)
Checks that an input is is non-null, a URL or a file, exists,
and if its a file then it is not a directory and is readable.
|
static void |
assertInputsAreValid(java.util.List<java.lang.String> inputs)
Checks that each string is non-null, exists or is a URL,
and if it is a file then not a directory and is readable.
|
static java.lang.String |
basename(java.io.File f)
Returns the name of the file minus the extension (i.e.
|
static void |
copyDirectoryTree(java.io.File fileOrDirectory,
java.io.File destination)
Copies a directory tree (all subdirectories and files) recursively to a destination
|
static void |
copyFile(java.io.File input,
java.io.File output)
Copy input to output, overwriting output if it already exists.
|
static void |
copyStream(java.io.InputStream input,
java.io.OutputStream output)
Utility method to copy the contents of input to output.
|
static java.io.File |
createTempDir(java.lang.String prefix,
java.lang.String suffix)
Create a temporary subdirectory in the default temporary-file directory, using the given prefix and suffix to generate the name.
|
static boolean |
deleteDirectoryTree(java.io.File fileOrDirectory)
Delete the given file or directory.
|
static void |
deleteFiles(java.io.File... files)
Delete a list of files, and write a warning message if one could not be deleted.
|
static void |
deleteFiles(java.lang.Iterable<java.io.File> files) |
static java.lang.String |
fileSuffix(java.io.File f)
Returns the name of the file extension (i.e.
|
static int |
getCompressionLevel() |
static java.io.File |
getDefaultTmpDir()
Returns a default tmp directory.
|
static java.io.File[] |
getFilesMatchingRegexp(java.io.File directory,
java.util.regex.Pattern regexp) |
static java.io.File[] |
getFilesMatchingRegexp(java.io.File directory,
java.lang.String regexp) |
static java.lang.String |
getFullCanonicalPath(java.io.File file)
Returns the full path to the file with all symbolic links resolved
|
static boolean |
isRegularPath(java.io.File file) |
static boolean |
isUrl(java.lang.String input)
Returns true iff the string is a url.
|
static java.lang.String |
makeFileNameSafe(java.lang.String str)
Takes a string and replaces any characters that are not safe for filenames with an underscore
|
static SeekableStream |
maybeBufferedSeekableStream(java.io.File file) |
static SeekableStream |
maybeBufferedSeekableStream(SeekableStream stream) |
static SeekableStream |
maybeBufferedSeekableStream(SeekableStream stream,
int bufferSize) |
static SeekableStream |
maybeBufferedSeekableStream(java.net.URL url) |
static java.io.InputStream |
maybeBufferInputStream(java.io.InputStream is) |
static java.io.InputStream |
maybeBufferInputStream(java.io.InputStream is,
int bufferSize) |
static java.io.OutputStream |
maybeBufferOutputStream(java.io.OutputStream os) |
static java.io.OutputStream |
maybeBufferOutputStream(java.io.OutputStream os,
int bufferSize) |
static java.io.Reader |
maybeBufferReader(java.io.Reader reader) |
static java.io.Reader |
maybeBufferReader(java.io.Reader reader,
int bufferSize) |
static java.io.Writer |
maybeBufferWriter(java.io.Writer writer) |
static java.io.Writer |
maybeBufferWriter(java.io.Writer writer,
int bufferSize) |
static java.io.File |
newTempFile(java.lang.String prefix,
java.lang.String suffix,
java.io.File[] tmpDirs)
Creates a new tmp file on one of the potential filesystems that has at least 5GB free.
|
static java.io.File |
newTempFile(java.lang.String prefix,
java.lang.String suffix,
java.io.File[] tmpDirs,
long minBytesFree)
Creates a new tmp file on one of the available temp filesystems, registers it for deletion
on JVM exit and then returns it.
|
static java.io.BufferedReader |
openFileForBufferedReading(java.io.File file)
Checks that a file exists and is readable, and then returns a buffered reader for it.
|
static java.io.BufferedReader |
openFileForBufferedUtf8Reading(java.io.File file)
Opens a file for reading, decompressing it if necessary
|
static java.io.BufferedWriter |
openFileForBufferedUtf8Writing(java.io.File file)
Preferred over PrintStream and PrintWriter because an exception is thrown on I/O error
|
static java.io.BufferedWriter |
openFileForBufferedWriting(java.io.File file)
Preferred over PrintStream and PrintWriter because an exception is thrown on I/O error
|
static java.io.BufferedWriter |
openFileForBufferedWriting(java.io.File file,
boolean append)
Preferred over PrintStream and PrintWriter because an exception is thrown on I/O error
|
static java.io.OutputStream |
openFileForMd5CalculatingWriting(java.io.File file) |
static java.io.InputStream |
openFileForReading(java.io.File file)
Opens a file for reading, decompressing it if necessary
|
static java.io.InputStream |
openFileForReading(java.nio.file.Path path)
Opens a file for reading, decompressing it if necessary
|
static java.io.OutputStream |
openFileForWriting(java.io.File file)
Opens a file for writing, overwriting the file if it already exists
|
static java.io.OutputStream |
openFileForWriting(java.io.File file,
boolean append)
Opens a file for writing
|
static java.io.InputStream |
openGzipFileForReading(java.io.File file)
Opens a GZIP-encoded file for reading, decompressing it if necessary
|
static java.io.InputStream |
openGzipFileForReading(java.nio.file.Path path)
Opens a GZIP-encoded file for reading, decompressing it if necessary
|
static java.io.OutputStream |
openGzipFileForWriting(java.io.File file,
boolean append)
Opens a GZIP encoded file for writing
|
static java.lang.String |
readFully(java.io.InputStream in)
Reads everything from an input stream as characters and returns a single String.
|
static IterableOnceIterator<java.lang.String> |
readLines(java.io.File f)
Returns an iterator over the lines in a text file.
|
static void |
setCompressionLevel(int compressionLevel)
Sets the GZip compression level for subsequent GZIPOutputStream object creation.
|
static long |
sizeOfTree(java.io.File fileOrDirectory)
Returns the size (in bytes) of the file or directory and all it's children.
|
static java.lang.String |
slurp(java.io.File file)
Convenience overload for
slurp(java.io.InputStream, java.nio.charset.Charset) using the default charset Charset.defaultCharset() . |
static java.lang.String |
slurp(java.io.InputStream is)
Convenience overload for
slurp(java.io.InputStream, java.nio.charset.Charset) using the default charset Charset.defaultCharset() . |
static java.lang.String |
slurp(java.io.InputStream is,
java.nio.charset.Charset charSet)
Reads all of the stream into a String, decoding with the provided
Charset then closes the stream quietly. |
static java.util.List<java.lang.String> |
slurpLines(java.io.File file)
Returns all of the untrimmed lines in the provided file.
|
static java.util.List<java.lang.String> |
slurpLines(java.io.InputStream is) |
static java.io.BufferedInputStream |
toBufferedStream(java.io.InputStream stream)
Wrap the given stream in a BufferedInputStream, if it isn't already wrapper
|
static void |
transferByStream(java.io.InputStream in,
java.io.OutputStream out,
long bytes)
Transfers from the input stream to the output stream using stream operations and a buffer.
|
static java.util.List<java.io.File> |
unrollFiles(java.util.Collection<java.io.File> inputs,
java.lang.String... extensions)
Go through the files provided and if they have one of the provided file extensions pass the file into the output
otherwise assume that file is a list of filenames and unfold it into the output.
|
@Deprecated public static final int STANDARD_BUFFER_SIZE
Defaults.NON_ZERO_BUFFER_SIZE
instead.public static final long ONE_GB
public static final long TWO_GBS
public static final long FIVE_GBS
public static final java.lang.String[] VCF_EXTENSIONS
public static final java.lang.String INTERVAL_LIST_FILE_EXTENSION
public static final java.lang.String SAM_FILE_EXTENSION
public static final java.lang.String DICT_FILE_EXTENSION
public static void setCompressionLevel(int compressionLevel)
compressionLevel
- 0 <= compressionLevel <= 9public static int getCompressionLevel()
public static java.io.BufferedInputStream toBufferedStream(java.io.InputStream stream)
stream
- stream to be wrappedpublic static void transferByStream(java.io.InputStream in, java.io.OutputStream out, long bytes)
public static java.io.OutputStream maybeBufferOutputStream(java.io.OutputStream os)
public static java.io.OutputStream maybeBufferOutputStream(java.io.OutputStream os, int bufferSize)
public static SeekableStream maybeBufferedSeekableStream(SeekableStream stream, int bufferSize)
public static SeekableStream maybeBufferedSeekableStream(SeekableStream stream)
public static SeekableStream maybeBufferedSeekableStream(java.io.File file)
public static SeekableStream maybeBufferedSeekableStream(java.net.URL url)
public static java.io.InputStream maybeBufferInputStream(java.io.InputStream is)
public static java.io.InputStream maybeBufferInputStream(java.io.InputStream is, int bufferSize)
public static java.io.Reader maybeBufferReader(java.io.Reader reader, int bufferSize)
public static java.io.Reader maybeBufferReader(java.io.Reader reader)
public static java.io.Writer maybeBufferWriter(java.io.Writer writer, int bufferSize)
public static java.io.Writer maybeBufferWriter(java.io.Writer writer)
public static void deleteFiles(java.io.File... files)
files
- Files to be deleted.public static void deleteFiles(java.lang.Iterable<java.io.File> files)
public static boolean isRegularPath(java.io.File file)
public static java.io.File newTempFile(java.lang.String prefix, java.lang.String suffix, java.io.File[] tmpDirs, long minBytesFree) throws java.io.IOException
java.io.IOException
public static java.io.File newTempFile(java.lang.String prefix, java.lang.String suffix, java.io.File[] tmpDirs) throws java.io.IOException
java.io.IOException
public static java.io.File getDefaultTmpDir()
public static java.lang.String basename(java.io.File f)
public static void assertInputIsValid(java.lang.String input)
input
- the input to check for validitypublic static boolean isUrl(java.lang.String input)
public static void assertFileIsReadable(java.io.File file)
file
- the file to check for readabilitypublic static void assertFileIsReadable(java.nio.file.Path path)
path
- the file to check for readabilitypublic static void assertFilesAreReadable(java.util.List<java.io.File> files)
files
- the list of files to check for readabilitypublic static void assertInputsAreValid(java.util.List<java.lang.String> inputs)
files
- the list of files to check for readabilitypublic static void assertFileIsWritable(java.io.File file)
file
- the file to check for writabilitypublic static void assertFilesAreWritable(java.util.List<java.io.File> files)
files
- the list of files to check for writabilitypublic static void assertDirectoryIsWritable(java.io.File dir)
dir
- the dir to check for writabilitypublic static void assertDirectoryIsReadable(java.io.File dir)
dir
- the dir to check for writabilitypublic static void assertFilesEqual(java.io.File f1, java.io.File f2)
public static void assertFileSizeNonZero(java.io.File file)
public static java.io.InputStream openFileForReading(java.io.File file)
file
- The file to openpublic static java.io.InputStream openFileForReading(java.nio.file.Path path)
path
- The file to openpublic static java.io.InputStream openGzipFileForReading(java.io.File file)
file
- The file to openpublic static java.io.InputStream openGzipFileForReading(java.nio.file.Path path)
path
- The file to openpublic static java.io.OutputStream openFileForWriting(java.io.File file)
file
- the file to write topublic static java.io.OutputStream openFileForWriting(java.io.File file, boolean append)
file
- the file to write toappend
- whether to append to the file if it already exists (we overwrite it if false)public static java.io.BufferedWriter openFileForBufferedWriting(java.io.File file, boolean append)
public static java.io.BufferedWriter openFileForBufferedWriting(java.io.File file)
public static java.io.BufferedWriter openFileForBufferedUtf8Writing(java.io.File file)
public static java.io.BufferedReader openFileForBufferedUtf8Reading(java.io.File file)
file
- The file to openpublic static java.io.OutputStream openGzipFileForWriting(java.io.File file, boolean append)
file
- the file to write toappend
- whether to append to the file if it already exists (we overwrite it if false)public static java.io.OutputStream openFileForMd5CalculatingWriting(java.io.File file)
public static void copyStream(java.io.InputStream input, java.io.OutputStream output)
input
- contents to be copiedoutput
- destinationpublic static void copyFile(java.io.File input, java.io.File output)
public static java.io.File[] getFilesMatchingRegexp(java.io.File directory, java.lang.String regexp)
directory
- regexp
- public static java.io.File[] getFilesMatchingRegexp(java.io.File directory, java.util.regex.Pattern regexp)
public static boolean deleteDirectoryTree(java.io.File fileOrDirectory)
public static long sizeOfTree(java.io.File fileOrDirectory)
public static void copyDirectoryTree(java.io.File fileOrDirectory, java.io.File destination)
public static java.io.File createTempDir(java.lang.String prefix, java.lang.String suffix)
prefix
- The prefix string to be used in generating the file's name; must be at least three characters longsuffix
- The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be usedpublic static java.io.BufferedReader openFileForBufferedReading(java.io.File file)
public static java.lang.String makeFileNameSafe(java.lang.String str)
public static java.lang.String fileSuffix(java.io.File f)
public static java.lang.String getFullCanonicalPath(java.io.File file)
public static java.lang.String readFully(java.io.InputStream in)
public static IterableOnceIterator<java.lang.String> readLines(java.io.File f)
f
- a file that is to be read in as textpublic static java.util.List<java.lang.String> slurpLines(java.io.File file) throws java.io.FileNotFoundException
java.io.FileNotFoundException
public static java.util.List<java.lang.String> slurpLines(java.io.InputStream is) throws java.io.FileNotFoundException
java.io.FileNotFoundException
public static java.lang.String slurp(java.io.File file) throws java.io.FileNotFoundException
slurp(java.io.InputStream, java.nio.charset.Charset)
using the default charset Charset.defaultCharset()
.java.io.FileNotFoundException
public static java.lang.String slurp(java.io.InputStream is)
slurp(java.io.InputStream, java.nio.charset.Charset)
using the default charset Charset.defaultCharset()
.public static java.lang.String slurp(java.io.InputStream is, java.nio.charset.Charset charSet)
Charset
then closes the stream quietly.public static java.util.List<java.io.File> unrollFiles(java.util.Collection<java.io.File> inputs, java.lang.String... extensions)