Class IOToolkit


  • public final class IOToolkit
    extends Object
    Common functionality you might want when you're working with I/O.
    • Method Detail

      • closeSilently

        public static void closeSilently​(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 InputStream openUncompressedStream​(File file)
                                                  throws IOException
        Get an input stream for a optionally compressed file. If the file is compressed using GZip, ZIP or LZ4, then an appropriate unpacking will be done.
        Parameters:
        file - file to read from
        Returns:
        input stream for the unpacked file content
        Throws:
        IOException - on I/O error
      • openUncompressedStream

        public static InputStream openUncompressedStream​(InputStream stream)
                                                  throws IOException
        Get an input stream for a optionally compressed input stream. If the file is compressed using GZip, ZIP or LZ4, then an appropriate unpacking will be done.
        Parameters:
        stream - input stream to read from
        Returns:
        input stream for the unpacked content
        Throws:
        IOException - on I/O error
      • hasMagic

        public static boolean hasMagic​(File file,
                                       int[] magic)
                                throws IOException
        Checks if a file begins with a specified array of bytes.
        Parameters:
        file - the file to examine
        magic - the magic data, an array with values between 0 and 255
        Returns:
        true if the file begins with the magic, false otherwise
        Throws:
        IOException - if an error occurred when trying to read from the file
      • hasMagic

        public static boolean hasMagic​(InputStream is,
                                       int[] magic)
                                throws IOException
        Checks 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 examine
        magic - the magic data, an array with values between 0 and 255
        Returns:
        true if the input stream begins with the magic, false otherwise
        Throws:
        IOException - if an error occurred when trying to read from the stream
      • isGZipFile

        public static boolean isGZipFile​(File file)
                                  throws IOException
        Returns true if the file is GZip file.
        Parameters:
        file - the file to examine
        Returns:
        true if it is a GZip file, false otherwise
        Throws:
        IOException - if an error occurred when trying to read from the file
      • isLZ4File

        public static boolean isLZ4File​(File file)
                                 throws IOException
        Returns true if the file is LZ4 compressed.
        Parameters:
        file - the file to examine
        Returns:
        true if it is an LZ4 compressed file, false otherwise
        Throws:
        IOException - if an error occurred when trying to read from the file
      • isZipFile

        public static boolean isZipFile​(File file)
                                 throws IOException
        Checks if the file is a ZIP archive.
        Parameters:
        file - the file to examine
        Returns:
        true if it's a ZIP archive, false otherwise
        Throws:
        IOException - if an error occurred when trying to read from the file
      • getGzipMagic

        public static int[] getGzipMagic()
        Returns the magic bytes for identifying Gzip. This is a defensive copy. It's up to the user to cache this to avoid excessive allocations.
        Returns:
        a copy of the magic bytes for Gzip.
      • getZipMagic

        public static int[] getZipMagic()
        Returns the magic bytes for identifying Zip. This is a defensive copy. It's up to the user to cache this to avoid excessive allocations.
        Returns:
        a copy of the magic bytes for Zip.
      • getLz4Magic

        public static int[] getLz4Magic()
        Returns the magic bytes for identifying LZ4. This is a defensive copy. It's up to the user to cache this to avoid excessive allocations.
        Returns:
        a copy of the magic bytes for LZ4.
      • isCompressedFile

        public static boolean isCompressedFile​(File file)
                                        throws IOException
        Checks if the file is compressed in a way compatible with openUncompressedStream(File).
        Parameters:
        file - the file to examine
        Returns:
        true if the file is compressed in a manner which can be uncompressed by openUncompressedStream(File), false otherwise
        Throws:
        IOException - if an error occurred when trying to read from the file
      • saveToFile

        public static void saveToFile​(File file,
                                      List<String> lines)
                               throws IOException
        Write lines to a text file. If the file already exists, it will be overwritten.
        Parameters:
        file - file to write lines to
        lines - a list of strings that will be written on one line each
        Throws:
        IOException - on I/O error
        See Also:
        loadFromFile(File)
      • write

        public static void write​(InputStream in,
                                 File toOutput,
                                 boolean append)
                          throws IOException
        Copy all data from an input stream to a file.
        Parameters:
        in - input stream to read from
        toOutput - file to write to
        append - true if the file should be appended to, false if it should be overwritten
        Throws:
        IOException - on I/O error
      • copy

        public static void copy​(InputStream is,
                                OutputStream os)
                         throws IOException
        Copy all data from an input stream to an output stream.
        Parameters:
        is - input stream to read from
        os - output stream to write to
        Throws:
        IOException - on I/O error
      • copy

        public static void copy​(InputStream is,
                                OutputStream os,
                                int bufferSize)
                         throws IOException
        Copy all data from an input stream to an output stream.
        Parameters:
        is - input stream to read from
        os - output stream to write to
        bufferSize - size of the buffer used when copying data
        Throws:
        IOException - on I/O error
      • copyFile

        public static void copyFile​(File srcFile,
                                    File targetFile)
                             throws IOException
        Copies 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 from
        targetFile - target file to copy data to
        Throws:
        IOException - if something goes wrong during the copy
      • calculateFileHash

        public static String calculateFileHash​(File file)
                                        throws IOException
        Calculates an MD5 hash on ten evenly distributed 1kB blocks from the file.
        Parameters:
        file - file to calculate hash for
        Returns:
        MD5 hash string
        Throws:
        IOException - if something goes wrong when reading file data