Package htsjdk.samtools.util
Class IOUtil
- java.lang.Object
-
- htsjdk.samtools.util.IOUtil
-
public class IOUtil extends Object
Miscellaneous stateless static IO-oriented methods. Also used for utility methods that wrap or aggregate functionality in Java IO.
-
-
Field Summary
Fields Modifier and Type Field Description static StringBCF_FILE_EXTENSIONDeprecated.since June 2019 UseFileExtensions.BCFinstead.static Set<String>BLOCK_COMPRESSED_EXTENSIONSDeprecated.Use since June 2019FileExtensions.BLOCK_COMPRESSEDinstead.static StringCOMPRESSED_VCF_FILE_EXTENSIONDeprecated.since June 2019 UseFileExtensions.COMPRESSED_VCFinstead.static StringCOMPRESSED_VCF_INDEX_EXTENSIONDeprecated.since June 2019 UseFileExtensions.COMPRESSED_VCF_INDEXinstead.static StringDICT_FILE_EXTENSIONDeprecated.since June 2019 UseFileExtensions.DICTinstead.static longFIVE_GBSstatic intGZIP_HEADER_READ_LENGTHnumber of bytes that will be read for the GZIP-header in the functionisGZIPInputStream(InputStream)static StringINTERVAL_LIST_FILE_EXTENSIONDeprecated.since June 2019 UseFileExtensions.INTERVAL_LISTinstead.static longONE_GBstatic StringSAM_FILE_EXTENSIONDeprecated.since June 2019 UseFileExtensions.SAMinstead.static intSTANDARD_BUFFER_SIZEDeprecated.UseDefaults.NON_ZERO_BUFFER_SIZEinstead.static longTWO_GBSstatic String[]VCF_EXTENSIONSDeprecated.since June 2019 UseFileExtensions.VCF_LISTinstead.static List<String>VCF_EXTENSIONS_LISTDeprecated.since June 2019 UseFileExtensions.VCF_LISTinstead.static StringVCF_FILE_EXTENSIONDeprecated.since June 2019 UseFileExtensions.VCFinstead.static StringVCF_INDEX_EXTENSIONDeprecated.since June 2019 UseFileExtensions.VCF_INDEXinstead.
-
Constructor Summary
Constructors Constructor Description IOUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static PathaddExtension(Path path, String extension)Adds the extension to the given path.static voidassertDirectoryIsReadable(File dir)Checks that a directory is non-null, extent, readable and a directory otherwise a runtime exception is thrown.static voidassertDirectoryIsWritable(File dir)Checks that a directory is non-null, extent, writable and a directory otherwise a runtime exception is thrown.static voidassertDirectoryIsWritable(Path dir)Checks that a directory is non-null, extent, writable and a directory otherwise a runtime exception is thrown.static voidassertFileIsReadable(File file)Checks that a file is non-null, exists, is not a directory and is readable.static voidassertFileIsReadable(Path path)Checks that a file is non-null, exists, is not a directory and is readable.static voidassertFileIsWritable(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 voidassertFilesAreReadable(List<File> files)Checks that each file is non-null, exists, is not a directory and is readable.static voidassertFilesAreWritable(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 voidassertFilesEqual(File f1, File f2)Checks that the two files are the same length, and have the same content, otherwise throws a runtime exception.static voidassertFileSizeNonZero(File file)Checks that a file is of non-zero lengthstatic voidassertInputIsValid(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 voidassertInputsAreValid(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 voidassertPathsAreReadable(List<Path> paths)Checks that each path is non-null, exists, is not a directory and is readable.static Stringbasename(File f)Returns the name of the file minus the extension (i.e.static voidcopyDirectoryTree(File fileOrDirectory, File destination)Copies a directory tree (all subdirectories and files) recursively to a destinationstatic voidcopyFile(File input, File output)Copy input to output, overwriting output if it already exists.static voidcopyStream(InputStream input, OutputStream output)Utility method to copy the contents of input to output.static FilecreateTempDir(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 booleandeleteDirectoryTree(File fileOrDirectory)Delete the given file or directory.static voiddeleteFiles(File... files)Delete a list of files, and write a warning message if one could not be deleted.static voiddeleteFiles(Iterable<File> files)static voiddeleteOnExit(Path path)Register aPathfor deletion on JVM exit.static voiddeletePath(Path path)Attempt to delete a single path and log an error if it is not deleted.static voiddeletePaths(Iterable<Path> paths)Iterate through Paths and delete each one.static voiddeletePaths(Path... paths)static List<Path>filesToPaths(Collection<File> files)Takes a list of Files and converts them to a list of Paths Runs .toPath() on the contents of the input.static StringfileSuffix(File f)Returns the name of the file extension (i.e.static intgetCompressionLevel()static FilegetDefaultTmpDir()Returns a default tmp directory.static PathgetDefaultTmpDirPath()Returns a default tmp directory as a Path.static File[]getFilesMatchingRegexp(File directory, String regexp)static File[]getFilesMatchingRegexp(File directory, Pattern regexp)static StringgetFullCanonicalPath(File file)Returns the full path to the file with all symbolic links resolvedstatic PathgetPath(String uriString)Converts the given URI to aPathobject.static List<Path>getPaths(List<String> uriStrings)static booleanhasBlockCompressedExtension(File file)Checks if a file ends in one of theFileExtensions.BLOCK_COMPRESSED.static booleanhasBlockCompressedExtension(String fileName)Checks if a file ends in one of theFileExtensions.BLOCK_COMPRESSED.static booleanhasBlockCompressedExtension(URI uri)Checks if a file ends in one of theFileExtensions.BLOCK_COMPRESSED.static booleanhasBlockCompressedExtension(Path path)Checks if a path ends in one of theFileExtensions.BLOCK_COMPRESSED.static booleanhasGzipFileExtension(Path path)check if the file name ends with .gz, .gzip, or .bfqstatic booleanhasScheme(String uriString)Check if the given URI has a scheme.static booleanisBlockCompressed(Path path)Checks if the provided path is block-compressed (including extension).static booleanisBlockCompressed(Path path, boolean checkExtension)Checks if the provided path is block-compressed.static booleanisGZIPInputStream(InputStream stream)Test whether a input stream looks like a GZIP input.static booleanisRegularPath(File file)static booleanisRegularPath(Path path)static booleanisUrl(String input)Returns true iff the string is a url.static StringmakeFileNameSafe(String str)Takes a string and replaces any characters that are not safe for filenames with an underscorestatic SeekableStreammaybeBufferedSeekableStream(SeekableStream stream)static SeekableStreammaybeBufferedSeekableStream(SeekableStream stream, int bufferSize)static SeekableStreammaybeBufferedSeekableStream(File file)static SeekableStreammaybeBufferedSeekableStream(URL url)static InputStreammaybeBufferInputStream(InputStream is)static InputStreammaybeBufferInputStream(InputStream is, int bufferSize)static OutputStreammaybeBufferOutputStream(OutputStream os)static OutputStreammaybeBufferOutputStream(OutputStream os, int bufferSize)static ReadermaybeBufferReader(Reader reader)static ReadermaybeBufferReader(Reader reader, int bufferSize)static WritermaybeBufferWriter(Writer writer)static WritermaybeBufferWriter(Writer writer, int bufferSize)static FilenewTempFile(String prefix, String suffix, File[] tmpDirs)Creates a new tmp file on one of the potential filesystems that has at least 5GB free.static FilenewTempFile(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 PathnewTempPath(String prefix, String suffix, Path[] tmpDirs)Creates a new tmp file on one of the potential filesystems that has at least 5GB free.static PathnewTempPath(String prefix, String suffix, Path[] tmpDirs, long minBytesFree)Creates a new tmp path on one of the available temp filesystems, registers it for deletion on JVM exit and then returns it.static BufferedReaderopenFileForBufferedReading(File file)Checks that a file exists and is readable, and then returns a buffered reader for it.static BufferedReaderopenFileForBufferedReading(Path path)Checks that a path exists and is readable, and then returns a buffered reader for it.static BufferedReaderopenFileForBufferedUtf8Reading(File file)Opens a file for reading, decompressing it if necessarystatic BufferedWriteropenFileForBufferedUtf8Writing(File file)Preferred over PrintStream and PrintWriter because an exception is thrown on I/O errorstatic BufferedWriteropenFileForBufferedUtf8Writing(Path path)Preferred over PrintStream and PrintWriter because an exception is thrown on I/O errorstatic BufferedWriteropenFileForBufferedWriting(File file)Preferred over PrintStream and PrintWriter because an exception is thrown on I/O errorstatic BufferedWriteropenFileForBufferedWriting(File file, boolean append)Preferred over PrintStream and PrintWriter because an exception is thrown on I/O errorstatic BufferedWriteropenFileForBufferedWriting(Path path, OpenOption... openOptions)Preferred over PrintStream and PrintWriter because an exception is thrown on I/O errorstatic OutputStreamopenFileForMd5CalculatingWriting(File file)static OutputStreamopenFileForMd5CalculatingWriting(Path file)static InputStreamopenFileForReading(File file)Opens a file for reading, decompressing it if necessarystatic InputStreamopenFileForReading(Path path)Opens a file for reading, decompressing it if necessarystatic OutputStreamopenFileForWriting(File file)Opens a file for writing, overwriting the file if it already existsstatic OutputStreamopenFileForWriting(File file, boolean append)Opens a file for writing, gzip it if it ends with ".gz" or "bfq"static OutputStreamopenFileForWriting(Path path, OpenOption... openOptions)Opens a file for writing, gzip it if it ends with ".gz" or "bfq"static InputStreamopenGzipFileForReading(File file)Opens a GZIP-encoded file for reading, decompressing it if necessarystatic InputStreamopenGzipFileForReading(Path path)Opens a GZIP-encoded file for reading, decompressing it if necessarystatic OutputStreamopenGzipFileForWriting(File file, boolean append)Opens a GZIP encoded file for writingstatic OutputStreamopenGzipFileForWriting(Path path, OpenOption... openOptions)Opens a GZIP encoded file for writingstatic StringreadFully(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 voidrecursiveDelete(Path directory)Delete a directory and all files in it.static voidsetCompressionLevel(int compressionLevel)Sets the GZip compression level for subsequent GZIPOutputStream object creation.static longsizeOfTree(File fileOrDirectory)Returns the size (in bytes) of the file or directory and all it's children.static Stringslurp(File file)Convenience overload forslurp(java.io.InputStream, java.nio.charset.Charset)using the default charsetCharset.defaultCharset().static Stringslurp(InputStream is)Convenience overload forslurp(java.io.InputStream, java.nio.charset.Charset)using the default charsetCharset.defaultCharset().static Stringslurp(InputStream is, Charset charSet)Reads all of the stream into a String, decoding with the providedCharsetthen 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 BufferedInputStreamtoBufferedStream(InputStream stream)Wrap the given stream in a BufferedInputStream, if it isn't already wrapperstatic PathtoPath(File fileOrNull)static voidtransferByStream(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.static List<Path>unrollPaths(Collection<Path> inputs, String... extensions)Go through the files provided and if they have one of the provided file extensions pass the file to the output otherwise assume that file is a list of filenames and unfold it into the output (recursively).
-
-
-
Field Detail
-
STANDARD_BUFFER_SIZE
@Deprecated public static final int STANDARD_BUFFER_SIZE
Deprecated.UseDefaults.NON_ZERO_BUFFER_SIZEinstead.
-
ONE_GB
public static final long ONE_GB
- See Also:
- Constant Field Values
-
TWO_GBS
public static final long TWO_GBS
- See Also:
- Constant Field Values
-
FIVE_GBS
public static final long FIVE_GBS
- See Also:
- Constant Field Values
-
VCF_FILE_EXTENSION
@Deprecated public static final String VCF_FILE_EXTENSION
Deprecated.since June 2019 UseFileExtensions.VCFinstead.- See Also:
- Constant Field Values
-
VCF_INDEX_EXTENSION
@Deprecated public static final String VCF_INDEX_EXTENSION
Deprecated.since June 2019 UseFileExtensions.VCF_INDEXinstead.- See Also:
- Constant Field Values
-
BCF_FILE_EXTENSION
@Deprecated public static final String BCF_FILE_EXTENSION
Deprecated.since June 2019 UseFileExtensions.BCFinstead.- See Also:
- Constant Field Values
-
COMPRESSED_VCF_FILE_EXTENSION
@Deprecated public static final String COMPRESSED_VCF_FILE_EXTENSION
Deprecated.since June 2019 UseFileExtensions.COMPRESSED_VCFinstead.- See Also:
- Constant Field Values
-
COMPRESSED_VCF_INDEX_EXTENSION
@Deprecated public static final String COMPRESSED_VCF_INDEX_EXTENSION
Deprecated.since June 2019 UseFileExtensions.COMPRESSED_VCF_INDEXinstead.- See Also:
- Constant Field Values
-
VCF_EXTENSIONS_LIST
@Deprecated public static final List<String> VCF_EXTENSIONS_LIST
Deprecated.since June 2019 UseFileExtensions.VCF_LISTinstead.
-
VCF_EXTENSIONS
@Deprecated public static final String[] VCF_EXTENSIONS
Deprecated.since June 2019 UseFileExtensions.VCF_LISTinstead.Possible extensions for VCF files and related formats.
-
INTERVAL_LIST_FILE_EXTENSION
@Deprecated public static final String INTERVAL_LIST_FILE_EXTENSION
Deprecated.since June 2019 UseFileExtensions.INTERVAL_LISTinstead.- See Also:
- Constant Field Values
-
SAM_FILE_EXTENSION
@Deprecated public static final String SAM_FILE_EXTENSION
Deprecated.since June 2019 UseFileExtensions.SAMinstead.- See Also:
- Constant Field Values
-
DICT_FILE_EXTENSION
@Deprecated public static final String DICT_FILE_EXTENSION
Deprecated.since June 2019 UseFileExtensions.DICTinstead.- See Also:
- Constant Field Values
-
BLOCK_COMPRESSED_EXTENSIONS
@Deprecated public static final Set<String> BLOCK_COMPRESSED_EXTENSIONS
Deprecated.Use since June 2019FileExtensions.BLOCK_COMPRESSEDinstead.
-
GZIP_HEADER_READ_LENGTH
public static final int GZIP_HEADER_READ_LENGTH
number of bytes that will be read for the GZIP-header in the functionisGZIPInputStream(InputStream)- See Also:
- Constant Field Values
-
-
Method Detail
-
setCompressionLevel
public static void setCompressionLevel(int compressionLevel)
Sets the GZip compression level for subsequent GZIPOutputStream object creation.- Parameters:
compressionLevel- 0 <= compressionLevel <= 9
-
getCompressionLevel
public static int getCompressionLevel()
-
toBufferedStream
public static BufferedInputStream toBufferedStream(InputStream stream)
Wrap the given stream in a BufferedInputStream, if it isn't already wrapper- Parameters:
stream- stream to be wrapped- Returns:
- A BufferedInputStream wrapping stream, or stream itself if stream instanceof BufferedInputStream.
-
transferByStream
public static void transferByStream(InputStream in, OutputStream out, long bytes)
Transfers from the input stream to the output stream using stream operations and a buffer.
-
maybeBufferOutputStream
public static OutputStream maybeBufferOutputStream(OutputStream os)
- Returns:
- If Defaults.BUFFER_SIZE > 0, wrap os in BufferedOutputStream, else return os itself.
-
maybeBufferOutputStream
public static OutputStream maybeBufferOutputStream(OutputStream os, int bufferSize)
- Returns:
- If bufferSize > 0, wrap os in BufferedOutputStream, else return os itself.
-
maybeBufferedSeekableStream
public static SeekableStream maybeBufferedSeekableStream(SeekableStream stream, int bufferSize)
-
maybeBufferedSeekableStream
public static SeekableStream maybeBufferedSeekableStream(SeekableStream stream)
-
maybeBufferedSeekableStream
public static SeekableStream maybeBufferedSeekableStream(File file)
-
maybeBufferedSeekableStream
public static SeekableStream maybeBufferedSeekableStream(URL url)
-
maybeBufferInputStream
public static InputStream maybeBufferInputStream(InputStream is)
- Returns:
- If Defaults.BUFFER_SIZE > 0, wrap is in BufferedInputStream, else return is itself.
-
maybeBufferInputStream
public static InputStream maybeBufferInputStream(InputStream is, int bufferSize)
- Returns:
- If bufferSize > 0, wrap is in BufferedInputStream, else return is itself.
-
deleteFiles
public static void deleteFiles(File... files)
Delete a list of files, and write a warning message if one could not be deleted.- Parameters:
files- Files to be deleted.
-
deletePaths
public static void deletePaths(Path... paths)
-
deletePaths
public static void deletePaths(Iterable<Path> paths)
Iterate through Paths and delete each one. Note: Path is itself an Iterable. This method special cases that and deletes the single Path rather than Iterating the Path for targets to delete. - Parameters:
paths- an iterable of Paths to delete
-
deletePath
public static void deletePath(Path path)
Attempt to delete a single path and log an error if it is not deleted.
-
isRegularPath
public static boolean isRegularPath(File file)
- Returns:
- true if the path is not a device (e.g. /dev/null or /dev/stdin), and is not an existing directory. I.e. is is a regular path that may correspond to an existing file, or a path that could be a regular output file.
-
isRegularPath
public static boolean isRegularPath(Path path)
- Returns:
- true if the path is not a device (e.g. /dev/null or /dev/stdin), and is not an existing directory. I.e. is is a regular path that may correspond to an existing file, or a path that could be a regular output file.
-
newTempFile
public static File newTempFile(String prefix, String suffix, File[] tmpDirs, long minBytesFree) throws IOException
Creates a new tmp file on one of the available temp filesystems, registers it for deletion on JVM exit and then returns it.- Throws:
IOException
-
newTempFile
public static File newTempFile(String prefix, String suffix, File[] tmpDirs) throws IOException
Creates a new tmp file on one of the potential filesystems that has at least 5GB free.- Throws:
IOException
-
getDefaultTmpDir
public static File getDefaultTmpDir()
Returns a default tmp directory.
-
newTempPath
public static Path newTempPath(String prefix, String suffix, Path[] tmpDirs, long minBytesFree) throws IOException
Creates a new tmp path on one of the available temp filesystems, registers it for deletion on JVM exit and then returns it.- Throws:
IOException
-
newTempPath
public static Path newTempPath(String prefix, String suffix, Path[] tmpDirs) throws IOException
Creates a new tmp file on one of the potential filesystems that has at least 5GB free.- Throws:
IOException
-
getDefaultTmpDirPath
public static Path getDefaultTmpDirPath()
Returns a default tmp directory as a Path.
-
deleteOnExit
public static void deleteOnExit(Path path)
Register aPathfor deletion on JVM exit.- See Also:
DeleteOnExitPathHook
-
basename
public static String basename(File f)
Returns the name of the file minus the extension (i.e. text after the last "." in the filename).
-
assertInputIsValid
public 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. If any condition is false then a runtime exception is thrown.- Parameters:
input- the input to check for validity
-
isUrl
public static boolean isUrl(String input)
Returns true iff the string is a url. Helps distinguish url inputs form file path inputs.
-
assertFileIsReadable
public static void assertFileIsReadable(File file)
Checks that a file is non-null, exists, is not a directory and is readable. If any condition is false then a runtime exception is thrown.- Parameters:
file- the file to check for readability
-
assertFileIsReadable
public static void assertFileIsReadable(Path path)
Checks that a file is non-null, exists, is not a directory and is readable. If any condition is false then a runtime exception is thrown.- Parameters:
path- the file to check for readability
-
assertFilesAreReadable
public static void assertFilesAreReadable(List<File> files)
Checks that each file is non-null, exists, is not a directory and is readable. If any condition is false then a runtime exception is thrown.- Parameters:
files- the list of files to check for readability
-
assertPathsAreReadable
public static void assertPathsAreReadable(List<Path> paths)
Checks that each path is non-null, exists, is not a directory and is readable. If any condition is false then a runtime exception is thrown.- Parameters:
paths- the list of paths to check for readability
-
assertInputsAreValid
public 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. If any condition is false then a runtime exception is thrown.- Parameters:
inputs- the list of files to check for readability
-
assertFileIsWritable
public 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. If any condition is false then a runtime exception is thrown.- Parameters:
file- the file to check for writability
-
assertFilesAreWritable
public 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. If any condition is false then a runtime exception is thrown.- Parameters:
files- the list of files to check for writability
-
assertDirectoryIsWritable
public static void assertDirectoryIsWritable(File dir)
Checks that a directory is non-null, extent, writable and a directory otherwise a runtime exception is thrown.- Parameters:
dir- the dir to check for writability
-
assertDirectoryIsWritable
public static void assertDirectoryIsWritable(Path dir)
Checks that a directory is non-null, extent, writable and a directory otherwise a runtime exception is thrown.- Parameters:
dir- the dir to check for writability
-
assertDirectoryIsReadable
public static void assertDirectoryIsReadable(File dir)
Checks that a directory is non-null, extent, readable and a directory otherwise a runtime exception is thrown.- Parameters:
dir- the dir to check for writability
-
assertFilesEqual
public 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.
-
assertFileSizeNonZero
public static void assertFileSizeNonZero(File file)
Checks that a file is of non-zero length
-
openFileForReading
public static InputStream openFileForReading(File file)
Opens a file for reading, decompressing it if necessary- Parameters:
file- The file to open- Returns:
- the input stream to read from
-
openFileForReading
public static InputStream openFileForReading(Path path)
Opens a file for reading, decompressing it if necessary- Parameters:
path- The file to open- Returns:
- the input stream to read from
-
openGzipFileForReading
public static InputStream openGzipFileForReading(File file)
Opens a GZIP-encoded file for reading, decompressing it if necessary- Parameters:
file- The file to open- Returns:
- the input stream to read from
-
openGzipFileForReading
public static InputStream openGzipFileForReading(Path path)
Opens a GZIP-encoded file for reading, decompressing it if necessary- Parameters:
path- The file to open- Returns:
- the input stream to read from
-
openFileForWriting
public static OutputStream openFileForWriting(File file)
Opens a file for writing, overwriting the file if it already exists- Parameters:
file- the file to write to- Returns:
- the output stream to write to
-
openFileForWriting
public static OutputStream openFileForWriting(File file, boolean append)
Opens a file for writing, gzip it if it ends with ".gz" or "bfq"- Parameters:
file- the file to write toappend- whether to append to the file if it already exists (we overwrite it if false)- Returns:
- the output stream to write to
-
openFileForWriting
public static OutputStream openFileForWriting(Path path, OpenOption... openOptions)
Opens a file for writing, gzip it if it ends with ".gz" or "bfq"- Parameters:
path- the file to write toopenOptions- options to use when opening the file- Returns:
- the output stream to write to
-
hasGzipFileExtension
public static boolean hasGzipFileExtension(Path path)
check if the file name ends with .gz, .gzip, or .bfq
-
openFileForBufferedWriting
public static BufferedWriter openFileForBufferedWriting(File file, boolean append)
Preferred over PrintStream and PrintWriter because an exception is thrown on I/O error
-
openFileForBufferedWriting
public static BufferedWriter openFileForBufferedWriting(Path path, OpenOption... openOptions)
Preferred over PrintStream and PrintWriter because an exception is thrown on I/O error
-
openFileForBufferedWriting
public static BufferedWriter openFileForBufferedWriting(File file)
Preferred over PrintStream and PrintWriter because an exception is thrown on I/O error
-
openFileForBufferedUtf8Writing
public static BufferedWriter openFileForBufferedUtf8Writing(File file)
Preferred over PrintStream and PrintWriter because an exception is thrown on I/O error
-
openFileForBufferedUtf8Writing
public static BufferedWriter openFileForBufferedUtf8Writing(Path path)
Preferred over PrintStream and PrintWriter because an exception is thrown on I/O error
-
openFileForBufferedUtf8Reading
public static BufferedReader openFileForBufferedUtf8Reading(File file)
Opens a file for reading, decompressing it if necessary- Parameters:
file- The file to open- Returns:
- the input stream to read from
-
openGzipFileForWriting
public static OutputStream openGzipFileForWriting(File file, boolean append)
Opens a GZIP encoded file for writing- Parameters:
file- the file to write toappend- whether to append to the file if it already exists (we overwrite it if false)- Returns:
- the output stream to write to
-
openGzipFileForWriting
public static OutputStream openGzipFileForWriting(Path path, OpenOption... openOptions)
Opens a GZIP encoded file for writing- Parameters:
path- the file to write toopenOptions- options to control how the file is opened- Returns:
- the output stream to write to
-
openFileForMd5CalculatingWriting
public static OutputStream openFileForMd5CalculatingWriting(File file)
-
openFileForMd5CalculatingWriting
public static OutputStream openFileForMd5CalculatingWriting(Path file)
-
copyStream
public static void copyStream(InputStream input, OutputStream output)
Utility method to copy the contents of input to output. The caller is responsible for opening and closing both streams.- Parameters:
input- contents to be copiedoutput- destination
-
copyFile
public static void copyFile(File input, File output)
Copy input to output, overwriting output if it already exists.
-
getFilesMatchingRegexp
public static File[] getFilesMatchingRegexp(File directory, String regexp)
- Parameters:
directory-regexp-- Returns:
- list of files matching regexp.
-
deleteDirectoryTree
public static boolean deleteDirectoryTree(File fileOrDirectory)
Delete the given file or directory. If a directory, all enclosing files and subdirs are also deleted.
-
sizeOfTree
public static long sizeOfTree(File fileOrDirectory)
Returns the size (in bytes) of the file or directory and all it's children.
-
copyDirectoryTree
public static void copyDirectoryTree(File fileOrDirectory, File destination)
Copies a directory tree (all subdirectories and files) recursively to a destination
-
createTempDir
public 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. Note that this method is not completely safe, because it create a temporary file, deletes it, and then creates a directory with the same name as the file. Should be good enough.- Parameters:
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 used- Returns:
- File object for new directory
-
openFileForBufferedReading
public static BufferedReader openFileForBufferedReading(File file)
Checks that a file exists and is readable, and then returns a buffered reader for it.
-
openFileForBufferedReading
public static BufferedReader openFileForBufferedReading(Path path)
Checks that a path exists and is readable, and then returns a buffered reader for it.
-
makeFileNameSafe
public static String makeFileNameSafe(String str)
Takes a string and replaces any characters that are not safe for filenames with an underscore
-
fileSuffix
public static String fileSuffix(File f)
Returns the name of the file extension (i.e. text after the last "." in the filename) including the .
-
getFullCanonicalPath
public static String getFullCanonicalPath(File file)
Returns the full path to the file with all symbolic links resolved
-
readFully
public static String readFully(InputStream in)
Reads everything from an input stream as characters and returns a single String.
-
readLines
public static IterableOnceIterator<String> readLines(File f)
Returns an iterator over the lines in a text file. The underlying resources are automatically closed when the iterator hits the end of the input, or manually by calling close().- Parameters:
f- a file that is to be read in as text- Returns:
- an iterator over the lines in the text file
-
slurpLines
public static List<String> slurpLines(File file) throws FileNotFoundException
Returns all of the untrimmed lines in the provided file.- Throws:
FileNotFoundException
-
slurpLines
public static List<String> slurpLines(InputStream is) throws FileNotFoundException
- Throws:
FileNotFoundException
-
slurp
public static String slurp(File file) throws FileNotFoundException
Convenience overload forslurp(java.io.InputStream, java.nio.charset.Charset)using the default charsetCharset.defaultCharset().- Throws:
FileNotFoundException
-
slurp
public static String slurp(InputStream is)
Convenience overload forslurp(java.io.InputStream, java.nio.charset.Charset)using the default charsetCharset.defaultCharset().
-
slurp
public static String slurp(InputStream is, Charset charSet)
Reads all of the stream into a String, decoding with the providedCharsetthen closes the stream quietly.
-
unrollFiles
public 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.
-
unrollPaths
public static List<Path> unrollPaths(Collection<Path> inputs, String... extensions)
Go through the files provided and if they have one of the provided file extensions pass the file to the output otherwise assume that file is a list of filenames and unfold it into the output (recursively).
-
hasScheme
public static boolean hasScheme(String uriString)
Check if the given URI has a scheme.- Parameters:
uriString- the URI to check- Returns:
trueif the given URI has a scheme,falseif not, or if the URI is malformed.
-
getPath
public static Path getPath(String uriString) throws IOException
Converts the given URI to aPathobject. If the filesystem cannot be found in the usual way, then attempt to load the filesystem provider using the thread context classloader. This is needed when the filesystem provider is loaded using a URL classloader (e.g. in spark-submit).- Parameters:
uriString- the URI to convert- Returns:
- the resulting
Path - Throws:
IOException- an I/O error occurs creating the file system
-
getPaths
public static List<Path> getPaths(List<String> uriStrings) throws RuntimeIOException
- Throws:
RuntimeIOException
-
filesToPaths
public static List<Path> filesToPaths(Collection<File> files)
Takes a list of Files and converts them to a list of Paths Runs .toPath() on the contents of the input.
-
isGZIPInputStream
public static boolean isGZIPInputStream(InputStream stream)
Test whether a input stream looks like a GZIP input. This identifies both gzip and bgzip streams as being GZIP.- Parameters:
stream- the input stream.- Returns:
- true if `stream` starts with a gzip signature.
- Throws:
IllegalArgumentException- if `stream` cannot mark or reset the stream
-
addExtension
public static Path addExtension(Path path, String extension)
Adds the extension to the given path.- Parameters:
path- the path to start from, eg. "/folder/file.jpg"extension- the extension to add, eg. ".bak"- Returns:
- "/folder/file.jpg.bak"
-
isBlockCompressed
public static boolean isBlockCompressed(Path path, boolean checkExtension) throws IOException
Checks if the provided path is block-compressed.Note that using
checkExtension=truewould avoid the cost of opening the file, but ifhasBlockCompressedExtension(String)returnsfalsethis would not detect block-compressed files such BAM.- Parameters:
path- file to check if it is block-compressed.checkExtension- iftrue, checks the extension before opening the file.- Returns:
trueif the file is block-compressed;falseotherwise.- Throws:
IOException- if there is an I/O error.
-
isBlockCompressed
public static boolean isBlockCompressed(Path path) throws IOException
Checks if the provided path is block-compressed (including extension).Note that block-compressed file extensions
FileExtensions.BLOCK_COMPRESSEDare not checked by this method.- Parameters:
path- file to check if it is block-compressed.- Returns:
trueif the file is block-compressed;falseotherwise.- Throws:
IOException- if there is an I/O error.
-
hasBlockCompressedExtension
public static boolean hasBlockCompressedExtension(String fileName)
Checks if a file ends in one of theFileExtensions.BLOCK_COMPRESSED.- Parameters:
fileName- string name for the file. May be an HTTP/S url.- Returns:
trueif the file has a block-compressed extension;falseotherwise.
-
hasBlockCompressedExtension
public static boolean hasBlockCompressedExtension(Path path)
Checks if a path ends in one of theFileExtensions.BLOCK_COMPRESSED.- Parameters:
path- object to extract the name from.- Returns:
trueif the path has a block-compressed extension;falseotherwise.
-
hasBlockCompressedExtension
public static boolean hasBlockCompressedExtension(File file)
Checks if a file ends in one of theFileExtensions.BLOCK_COMPRESSED.- Parameters:
file- object to extract the name from.- Returns:
trueif the file has a block-compressed extension;falseotherwise.
-
hasBlockCompressedExtension
public static boolean hasBlockCompressedExtension(URI uri)
Checks if a file ends in one of theFileExtensions.BLOCK_COMPRESSED.- Parameters:
uri- file as an URI.- Returns:
trueif the file has a block-compressed extension;falseotherwise.
-
recursiveDelete
public static void recursiveDelete(Path directory)
Delete a directory and all files in it.- Parameters:
directory- The directory to be deleted (along with its subdirectories)
-
-