Class IOToolkit


  • public final class IOToolkit
    extends java.lang.Object
    Common functionality you might want when you're working with I/O.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int[] MAGIC_GZ
      Magic bytes for recognizing GZip.
      private static int[] MAGIC_LZ4
      Magic bytes for recognizing LZ4.
      private static int[] MAGIC_ZIP
      Magic bytes for recognizing Zip.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private IOToolkit()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String calculateFileHash​(java.io.File file)
      Calculates an MD5 hash on ten evenly distributed 1kB blocks from the file.
      static void closeSilently​(java.io.Closeable closeable)
      Closes a closeable.
      static void copy​(java.io.InputStream is, java.io.OutputStream os)
      Copy all data from an input stream to an output stream.
      static void copy​(java.io.InputStream is, java.io.OutputStream os, int bufferSize)
      Copy all data from an input stream to an output stream.
      static void copyFile​(java.io.File srcFile, java.io.File targetFile)
      Copies srcFile to targetFile.
      static int[] getGzipMagic()
      Returns the magic bytes for identifying Gzip.
      static int[] getLz4Magic()
      Returns the magic bytes for identifying LZ4.
      static int[] getZipMagic()
      Returns the magic bytes for identifying Zip.
      static boolean hasMagic​(java.io.File file, int[] magic)
      Checks if a file begins with a specified array of bytes.
      static boolean hasMagic​(java.io.InputStream is, int[] magic)
      Checks if an input stream begins with a specified array of bytes.
      static boolean isCompressedFile​(java.io.File file)
      Checks if the file is compressed in a way compatible with openUncompressedStream(File).
      static boolean isGZipFile​(java.io.File file)
      Returns true if the file is GZip file.
      static boolean isLZ4File​(java.io.File file)
      Returns true if the file is LZ4 compressed.
      static boolean isZipFile​(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.InputStream openUncompressedStream​(java.io.File file)
      Get an input stream for a optionally compressed file.
      static java.io.InputStream openUncompressedStream​(java.io.InputStream stream)
      Get an input stream for a optionally compressed input stream.
      static void saveToFile​(java.io.File file, java.util.List<java.lang.String> lines)
      Write lines to a text file.
      static void write​(java.io.InputStream in, java.io.File toOutput, boolean append)
      Copy all data from an input stream to a file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MAGIC_ZIP

        private static final int[] MAGIC_ZIP
        Magic bytes for recognizing Zip.
      • MAGIC_GZ

        private static final int[] MAGIC_GZ
        Magic bytes for recognizing GZip.
      • MAGIC_LZ4

        private static final int[] MAGIC_LZ4
        Magic bytes for recognizing LZ4.
    • Constructor Detail

      • IOToolkit

        private IOToolkit()
    • 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.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:
        java.io.IOException - on I/O error
      • openUncompressedStream

        public static java.io.InputStream openUncompressedStream​(java.io.InputStream stream)
                                                          throws java.io.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:
        java.io.IOException - on I/O error
      • hasMagic

        public static boolean hasMagic​(java.io.File file,
                                       int[] magic)
                                throws java.io.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:
        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.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:
        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.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:
        java.io.IOException - if an error occurred when trying to read from the file
      • isLZ4File

        public static boolean isLZ4File​(java.io.File file)
                                 throws java.io.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:
        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.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:
        java.io.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​(java.io.File file)
                                        throws java.io.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:
        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.IOException
        Read 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.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:
        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.IOException
        Read 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.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:
        java.io.IOException - on I/O error
      • copy

        public static void copy​(java.io.InputStream is,
                                java.io.OutputStream os)
                         throws java.io.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:
        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.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:
        java.io.IOException - on I/O error
      • copyFile

        public static void copyFile​(java.io.File srcFile,
                                    java.io.File targetFile)
                             throws java.io.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:
        java.io.IOException - if something goes wrong during the copy
      • calculateFileHash

        public static java.lang.String calculateFileHash​(java.io.File file)
                                                  throws java.io.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:
        java.io.IOException - if something goes wrong when reading file data