public final class IOUtils
extends java.lang.Object
Constructor and Description |
---|
IOUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
assertFileIsReadable(java.nio.file.Path path) |
static void |
canReadFile(java.io.File... files)
Checks that one or more user provided files are in fact regular (i.e.
|
static void |
createDirectory(java.lang.String pathString)
Creates a directory, in local FS, HDFS, or Google buckets to write individual files in.
|
static java.io.File |
createTempDir(java.lang.String prefix)
Creates a temp directory with the given prefix.
|
static java.io.File |
createTempFile(java.lang.String name,
java.lang.String extension)
Creates a temp file that will be deleted on exit
This will also mark the corresponding Tribble/Tabix/BAM indices matching the temp file for deletion.
|
static java.io.File |
createTempFileInDirectory(java.lang.String name,
java.lang.String extension,
java.io.File targetDir)
Creates a temp file in a target directory that will be deleted on exit
This will also mark the corresponding Tribble/Tabix/BAM indices matching the temp file for deletion.
|
static void |
deleteRecursivelyOnExit(java.io.File dir)
Schedule a file or directory to be deleted on jvm exit.
|
static java.nio.file.Path |
getPath(java.lang.String uriString)
Converts the given URI to a
Path object. |
static void |
gunzip(java.io.File input,
java.io.File output)
Un-gzips the input file to the output file.
|
static java.io.File |
gunzipToTempIfNeeded(java.io.File maybeGzipedFile)
Un-gzips the input file to a output file but only if the file's name ends with '.gz'.
|
static boolean |
isBamFileName(java.lang.String inputFileName)
Returns true if the file's extension is BAM.
|
static boolean |
isCramFile(java.io.File inputFile)
Returns true if the file's extension is CRAM.
|
static boolean |
isCramFile(java.nio.file.Path path)
Returns true if the file's extension is CRAM.
|
static boolean |
isCramFileName(java.lang.String inputFileName)
Returns true if the file's extension is CRAM.
|
static boolean |
isSpecialFile(java.io.File file)
Returns true if the file is a special file.
|
static java.io.PrintStream |
makePrintStreamMaybeGzipped(java.io.File file)
Makes a print stream for a file, gzipping on the fly if the file's name ends with '.gz'.
|
static java.io.Reader |
makeReaderMaybeGzipped(java.io.File file)
Makes a reader for a file, unzipping if the file's name ends with '.gz'.
|
static java.io.Reader |
makeReaderMaybeGzipped(java.io.InputStream in,
boolean zipped)
makes a reader for an inputStream wrapping it in an appropriate unzipper if necessary
|
static java.io.InputStream |
makeZippedInputStream(java.io.InputStream in)
creates an input stream from a zipped stream
|
static byte[] |
readFileIntoByteArray(java.io.File source)
Reads the entirety of the given file into a byte array.
|
static byte[] |
readFileIntoByteArray(java.io.File source,
int readBufferSize)
Reads the entirety of the given file into a byte array using the requested read buffer size.
|
static byte[] |
readStreamIntoByteArray(java.io.InputStream in,
int readBufferSize)
Reads all data from the given stream into a byte array using the requested read buffer size.
|
static java.io.File |
replaceExtension(java.io.File file,
java.lang.String extension) |
static java.lang.String |
replaceExtension(java.lang.String path,
java.lang.String extension) |
static boolean |
tryDelete(java.io.File file)
Tries to delete a file.
|
static java.lang.String |
urlDecode(java.lang.String string) |
static java.lang.String |
urlEncode(java.lang.String string) |
static void |
writeByteArrayToFile(byte[] bytes,
java.io.File destination)
Writes the given array of bytes to a file
|
static void |
writeByteArrayToStream(byte[] bytes,
java.io.OutputStream out)
Writes the given array of bytes to a stream
|
static void |
writeResource(Resource resource,
java.io.File file)
Writes the an embedded resource to a file.
|
static java.io.File |
writeTempFile(java.lang.String content,
java.lang.String prefix,
java.lang.String suffix)
Writes content to a temp file and returns the path to the temporary file.
|
static java.io.File |
writeTempFile(java.lang.String content,
java.lang.String prefix,
java.lang.String suffix,
java.io.File directory)
Writes content to a temp file and returns the path to the temporary file.
|
static java.io.File |
writeTempResource(Resource resource)
Writes an embedded resource to a temporary file.
|
static java.io.File |
writeTempResourceFromPath(java.lang.String resourcePath,
java.lang.Class<?> relativeClass)
Create a resource from a path and a relative class, and write it to a temporary file.
|
public static boolean isCramFile(java.io.File inputFile)
public static boolean isCramFile(java.nio.file.Path path)
public static boolean isCramFileName(java.lang.String inputFileName)
public static boolean isBamFileName(java.lang.String inputFileName)
public static java.io.File createTempDir(java.lang.String prefix)
prefix
- Prefix for the directory name.public static java.io.File writeTempFile(java.lang.String content, java.lang.String prefix, java.lang.String suffix)
content
- to write.prefix
- Prefix for the temp file.suffix
- Suffix for the temp file.public static java.io.File writeTempFile(java.lang.String content, java.lang.String prefix, java.lang.String suffix, java.io.File directory)
content
- to write.prefix
- Prefix for the temp file.suffix
- Suffix for the temp file.directory
- Directory for the temp file.public static boolean isSpecialFile(java.io.File file)
file
- File path to check.public static boolean tryDelete(java.io.File file)
file
- File to delete.public static java.io.File writeTempResource(Resource resource)
resource
- Embedded resource.public static java.io.File writeTempResourceFromPath(java.lang.String resourcePath, java.lang.Class<?> relativeClass)
resourcePath
- Relative or absolute path to the class.relativeClass
- Relative class to use as a class loader and for a relative package.public static void writeResource(Resource resource, java.io.File file)
resource
- Embedded resource.file
- File path to write.public static byte[] readFileIntoByteArray(java.io.File source)
source
- File to readpublic static byte[] readFileIntoByteArray(java.io.File source, int readBufferSize)
source
- File to readreadBufferSize
- Number of bytes to read in at one timepublic static byte[] readStreamIntoByteArray(java.io.InputStream in, int readBufferSize)
in
- Stream to read data fromreadBufferSize
- Number of bytes to read in at one timepublic static void writeByteArrayToFile(byte[] bytes, java.io.File destination)
bytes
- Data to writedestination
- File to write the data topublic static void writeByteArrayToStream(byte[] bytes, java.io.OutputStream out)
bytes
- Data to writeout
- Stream to write the data topublic static void gunzip(java.io.File input, java.io.File output)
public static java.io.File gunzipToTempIfNeeded(java.io.File maybeGzipedFile)
public static java.io.Reader makeReaderMaybeGzipped(java.io.File file) throws java.io.IOException
java.io.IOException
public static java.io.Reader makeReaderMaybeGzipped(java.io.InputStream in, boolean zipped) throws java.io.IOException
zipped
- is this stream zippedjava.io.IOException
public static java.io.InputStream makeZippedInputStream(java.io.InputStream in) throws java.io.IOException
java.util.zip.ZipException
- if !in.markSupported()java.io.IOException
public static java.io.PrintStream makePrintStreamMaybeGzipped(java.io.File file) throws java.io.IOException
java.io.IOException
public static java.io.File createTempFile(java.lang.String name, java.lang.String extension)
name
- Prefix of the file.extension
- Extension to concat to the end of the file.public static java.io.File createTempFileInDirectory(java.lang.String name, java.lang.String extension, java.io.File targetDir)
name
- Prefix of the file.extension
- Extension to concat to the end of the file name.targetDir
- Directory in which to create the temp file. If null, the default temp directory is used.public static java.lang.String replaceExtension(java.lang.String path, java.lang.String extension)
extension
- a file extension, may include 0 or more leading dots which will be replaced with a single dotpublic static java.io.File replaceExtension(java.io.File file, java.lang.String extension)
public static void deleteRecursivelyOnExit(java.io.File dir)
FileUtils.forceDeleteOnExit(java.io.File)
by deleting directories and files that did not exist at call time.dir
- to be deletedpublic static java.nio.file.Path getPath(java.lang.String uriString)
Path
object. 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).
Also makes an attempt to interpret the argument as a file name if it's not a URI.uriString
- the URI to convert.Path
UserException
- if an I/O error occurs when creating the file systempublic static void assertFileIsReadable(java.nio.file.Path path)
path
- Path to testUserException.CouldNotReadInputFile
- if the file isn't readable
and a regular filepublic static void canReadFile(java.io.File... files)
files
- the input files to test.java.lang.IllegalArgumentException
- if any input file file
is null
or files
is null
.UserException
- if any file
is not a regular file or it cannot be read.public static void createDirectory(java.lang.String pathString) throws java.io.IOException
java.io.IOException
public static final java.lang.String urlEncode(java.lang.String string)
public static final java.lang.String urlDecode(java.lang.String string)