Package org.openjdk.jmc.common.io
Class IOToolkit
- java.lang.Object
-
- org.openjdk.jmc.common.io.IOToolkit
-
public final class IOToolkit extends java.lang.ObjectCommon functionality you might want when you're working with I/O.
-
-
Constructor Summary
Constructors Modifier Constructor Description privateIOToolkit()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringcalculateFileHash(java.io.File file)Calculates an MD5 hash on ten evenly distributed 1kB blocks from the file.static voidcloseSilently(java.io.Closeable closeable)Closes a closeable.static voidcopy(java.io.InputStream is, java.io.OutputStream os)Copy all data from an input stream to an output stream.static voidcopy(java.io.InputStream is, java.io.OutputStream os, int bufferSize)Copy all data from an input stream to an output stream.static voidcopyFile(java.io.File srcFile, java.io.File targetFile)Copies srcFile to targetFile.static booleanhasMagic(java.io.File file, int[] magic)Checks if a file begins with a specified array of bytes.static booleanhasMagic(java.io.InputStream is, int[] magic)Checks if an input stream begins with a specified array of bytes.static booleanisCompressedFile(java.io.File file)Checks if the file is compressed in a way compatible withopenUncompressedStream(File).static booleanisGZipFile(java.io.File file)Returns true if the file is GZip file.static booleanisZipFile(java.io.File file)Checks if the file is a ZIP archive.static java.util.List<java.lang.String>loadFromFile(java.io.File file)Read lines from a text file.private static java.util.List<java.lang.String>loadFromReader(java.io.Reader reader)static java.util.List<java.lang.String>loadFromStream(java.io.InputStream is)Read lines from an input stream.static java.io.InputStreamopenUncompressedStream(java.io.File file)Get an input stream for a optionally compressed file.static java.io.InputStreamopenUncompressedStream(java.io.InputStream stream)Get an input stream for a optionally compressed input stream.static voidsaveToFile(java.io.File file, java.util.List<java.lang.String> lines)Write lines to a text file.static voidwrite(java.io.InputStream in, java.io.File toOutput, boolean append)Copy all data from an input stream to a file.
-
-
-
Method Detail
-
closeSilently
public static void closeSilently(java.io.Closeable closeable)
Closes a closeable. Typically you call this in a final statement so the method also ignores if the closeable is null.- Parameters:
closeable- object to close, may be null
-
openUncompressedStream
public static java.io.InputStream openUncompressedStream(java.io.File file) throws java.io.IOExceptionGet an input stream for a optionally compressed file. If the file is compressed using either GZip or ZIP then an appropriate unpacking will be done.- Parameters:
file- file to read from- Returns:
- input stream for the unpacked file content
- Throws:
java.io.IOException- on I/O error
-
openUncompressedStream
public static java.io.InputStream openUncompressedStream(java.io.InputStream stream) throws java.io.IOExceptionGet an input stream for a optionally compressed input stream. If the input stream is compressed using either GZip or ZIP then an appropriate unpacking will be done.- Parameters:
stream- input stream to read from- Returns:
- input stream for the unpacked content
- Throws:
java.io.IOException- on I/O error
-
hasMagic
public static boolean hasMagic(java.io.File file, int[] magic) throws java.io.IOExceptionChecks if a file begins with a specified array of bytes.- Parameters:
file- the file to examinemagic- the magic data, an array with values between 0 and 255- Returns:
trueif the file begins with the magic,falseotherwise- Throws:
java.io.IOException- if an error occurred when trying to read from the file
-
hasMagic
public static boolean hasMagic(java.io.InputStream is, int[] magic) throws java.io.IOExceptionChecks if an input stream begins with a specified array of bytes. The input stream will be positioned at the first byte after the magic data after this call.- Parameters:
is- the input stream to examinemagic- the magic data, an array with values between 0 and 255- Returns:
trueif the input stream begins with the magic,falseotherwise- Throws:
java.io.IOException- if an error occurred when trying to read from the stream
-
isGZipFile
public static boolean isGZipFile(java.io.File file) throws java.io.IOExceptionReturns true if the file is GZip file.- Parameters:
file- the file to examine- Returns:
trueif it is a GZip file,falseotherwise- Throws:
java.io.IOException- if an error occurred when trying to read from the file
-
isZipFile
public static boolean isZipFile(java.io.File file) throws java.io.IOExceptionChecks if the file is a ZIP archive.- Parameters:
file- the file to examine- Returns:
trueif it's a ZIP archive,falseotherwise- Throws:
java.io.IOException- if an error occurred when trying to read from the file
-
isCompressedFile
public static boolean isCompressedFile(java.io.File file) throws java.io.IOExceptionChecks if the file is compressed in a way compatible withopenUncompressedStream(File).- Parameters:
file- the file to examine- Returns:
trueif the file is compressed in a manner which can be uncompressed byopenUncompressedStream(File),falseotherwise- Throws:
java.io.IOException- if an error occurred when trying to read from the file
-
loadFromFile
public static java.util.List<java.lang.String> loadFromFile(java.io.File file) throws java.io.IOExceptionRead lines from a text file.- Parameters:
file- file to read lines from- Returns:
- a list of strings, one for each line in the file
- Throws:
java.io.IOException- on I/O error- See Also:
saveToFile(File, List)
-
loadFromReader
private static java.util.List<java.lang.String> loadFromReader(java.io.Reader reader) throws java.io.IOException- Throws:
java.io.IOException
-
saveToFile
public static void saveToFile(java.io.File file, java.util.List<java.lang.String> lines) throws java.io.IOExceptionWrite lines to a text file. If the file already exists, it will be overwritten.- Parameters:
file- file to write lines tolines- a list of strings that will be written on one line each- Throws:
java.io.IOException- on I/O error- See Also:
loadFromFile(File)
-
loadFromStream
public static java.util.List<java.lang.String> loadFromStream(java.io.InputStream is) throws java.io.IOExceptionRead lines from an input stream.- Parameters:
is- input stream to read lines from- Returns:
- a list of strings, one for each line in the stream
- Throws:
java.io.IOException- on I/O error- See Also:
saveToFile(File, List)
-
write
public static void write(java.io.InputStream in, java.io.File toOutput, boolean append) throws java.io.IOExceptionCopy all data from an input stream to a file.- Parameters:
in- input stream to read fromtoOutput- file to write toappend-trueif the file should be appended to,falseif it should be overwritten- Throws:
java.io.IOException- on I/O error
-
copy
public static void copy(java.io.InputStream is, java.io.OutputStream os) throws java.io.IOExceptionCopy all data from an input stream to an output stream.- Parameters:
is- input stream to read fromos- output stream to write to- Throws:
java.io.IOException- on I/O error
-
copy
public static void copy(java.io.InputStream is, java.io.OutputStream os, int bufferSize) throws java.io.IOExceptionCopy all data from an input stream to an output stream.- Parameters:
is- input stream to read fromos- output stream to write tobufferSize- size of the buffer used when copying data- Throws:
java.io.IOException- on I/O error
-
copyFile
public static void copyFile(java.io.File srcFile, java.io.File targetFile) throws java.io.IOExceptionCopies srcFile to targetFile. Will do nothing if srcFile and targetFile are the same file. Will copy file attributes.- Parameters:
srcFile- source file to copy data fromtargetFile- target file to copy data to- Throws:
java.io.IOException- if something goes wrong during the copy
-
calculateFileHash
public static java.lang.String calculateFileHash(java.io.File file) throws java.io.IOExceptionCalculates an MD5 hash on ten evenly distributed 1kB blocks from the file.- Parameters:
file- file to calculate hash for- Returns:
- MD5 hash string
- Throws:
java.io.IOException- if something goes wrong when reading file data
-
-