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