public final class IOToolkit extends Object
Modifier and Type | Method and Description |
---|---|
static String |
calculateFileHash(File file)
Calculates an MD5 hash on ten evenly distributed 1kB blocks from the file.
|
static void |
closeSilently(Closeable closeable)
Closes a closeable.
|
static void |
copy(InputStream is,
OutputStream os)
Copy all data from an input stream to an output stream.
|
static void |
copy(InputStream is,
OutputStream os,
int bufferSize)
Copy all data from an input stream to an output stream.
|
static void |
copyFile(File srcFile,
File targetFile)
Copies srcFile to targetFile.
|
static boolean |
hasMagic(File file,
int[] magic)
Checks if a file begins with a specified array of bytes.
|
static boolean |
hasMagic(InputStream is,
int[] magic)
Checks if an input stream begins with a specified array of bytes.
|
static boolean |
isCompressedFile(File file)
Checks if the file is compressed in a way compatible with
openUncompressedStream(File) . |
static boolean |
isGZipFile(File file)
Returns true if the file is GZip file.
|
static boolean |
isZipFile(File file)
Checks if the file is a ZIP archive.
|
static List<String> |
loadFromFile(File file)
Read lines from a text file.
|
static List<String> |
loadFromStream(InputStream is)
Read lines from an input stream.
|
static InputStream |
openUncompressedStream(File file)
Get an input stream for a optionally compressed file.
|
static InputStream |
openUncompressedStream(InputStream stream)
Get an input stream for a optionally compressed input stream.
|
static void |
saveToFile(File file,
List<String> lines)
Write lines to a text file.
|
static void |
write(InputStream in,
File toOutput,
boolean append)
Copy all data from an input stream to a file.
|
public static void closeSilently(Closeable closeable)
closeable
- object to close, may be nullpublic static InputStream openUncompressedStream(File file) throws IOException
file
- file to read fromIOException
- on I/O errorpublic static InputStream openUncompressedStream(InputStream stream) throws IOException
stream
- input stream to read fromIOException
- on I/O errorpublic static boolean hasMagic(File file, int[] magic) throws IOException
file
- the file to examinemagic
- the magic data, an array with values between 0 and 255true
if the file begins with the magic, false
otherwiseIOException
- if an error occurred when trying to read from the filepublic static boolean hasMagic(InputStream is, int[] magic) throws IOException
is
- the input stream to examinemagic
- the magic data, an array with values between 0 and 255true
if the input stream begins with the magic, false
otherwiseIOException
- if an error occurred when trying to read from the streampublic static boolean isGZipFile(File file) throws IOException
file
- the file to examinetrue
if it is a GZip file, false
otherwiseIOException
- if an error occurred when trying to read from the filepublic static boolean isZipFile(File file) throws IOException
file
- the file to examinetrue
if it's a ZIP archive, false
otherwiseIOException
- if an error occurred when trying to read from the filepublic static boolean isCompressedFile(File file) throws IOException
openUncompressedStream(File)
.file
- the file to examinetrue
if the file is compressed in a manner which can be uncompressed by
openUncompressedStream(File)
, false
otherwiseIOException
- if an error occurred when trying to read from the filepublic static List<String> loadFromFile(File file) throws IOException
file
- file to read lines fromIOException
- on I/O errorsaveToFile(File, List)
public static void saveToFile(File file, List<String> lines) throws IOException
file
- file to write lines tolines
- a list of strings that will be written on one line eachIOException
- on I/O errorloadFromFile(File)
public static List<String> loadFromStream(InputStream is) throws IOException
is
- input stream to read lines fromIOException
- on I/O errorsaveToFile(File, List)
public static void write(InputStream in, File toOutput, boolean append) throws IOException
in
- input stream to read fromtoOutput
- file to write toappend
- true
if the file should be appended to, false
if it should be
overwrittenIOException
- on I/O errorpublic static void copy(InputStream is, OutputStream os) throws IOException
is
- input stream to read fromos
- output stream to write toIOException
- on I/O errorpublic static void copy(InputStream is, OutputStream os, int bufferSize) throws IOException
is
- input stream to read fromos
- output stream to write tobufferSize
- size of the buffer used when copying dataIOException
- on I/O errorpublic static void copyFile(File srcFile, File targetFile) throws IOException
srcFile
- source file to copy data fromtargetFile
- target file to copy data toIOException
- if something goes wrong during the copypublic static String calculateFileHash(File file) throws IOException
file
- file to calculate hash forIOException
- if something goes wrong when reading file dataCopyright © 2020. All rights reserved.