Package mgui.io.util

Class IoFunctions

java.lang.Object
mgui.interfaces.Utility
mgui.io.util.IoFunctions

public class IoFunctions
extends Utility
Utility class which provides functions for common I/O operations.
Since:
1.0
Version:
1.0
Author:
Andrew Reid
  • Nested Class Summary

    Nested Classes
    Modifier and Type Class Description
    static class  IoFunctions.FileType  
  • Constructor Summary

    Constructors
    Constructor Description
    IoFunctions()  
  • Method Summary

    Modifier and Type Method Description
    static float byteArrayToFloat​(byte[] b, java.nio.ByteOrder bo)  
    static int byteArrayToInt​(byte[] b, java.nio.ByteOrder bo)  
    static int byteArrayToShort​(byte[] b, java.nio.ByteOrder bo)  
    static double byteArrayToVal​(byte[] b, int type, java.nio.ByteOrder bo)  
    static byte[] compressGZipped​(byte[] contentBytes)
    Compresses a gzipped byte array.
    static byte[] compressZipped​(byte[] contentBytes)
    Compresses a byte array.
    static boolean copyDir​(java.io.File dir, java.io.File dest)
    Copies the directory dir and all its contents to the directory dest.
    static boolean copyDir​(java.io.File dir, java.io.File dest, boolean recurse)
    Copies the directory dir and all its contents to the directory dest.
    static boolean copyDir​(java.io.File dir, java.io.File dest, boolean recurse, ProgressUpdater progress)
    Copies the directory dir and all its contents to the directory dest.
    static boolean copyDirContents​(java.io.File dir, java.io.File dest)
    Copies the contents of directory dir (but not the directory itself) to the directory dest.
    static boolean copyDirContents​(java.io.File dir, java.io.File dest, boolean recurse)
    Copies the contents of directory dir (but not the directory itself) to the directory dest.
    static boolean copyDirContents​(java.io.File dir, java.io.File dest, boolean recurse, ProgressUpdater progress)
    Copies the contents of directory dir (but not the directory itself) to the directory dest.
    static boolean copyFile​(java.io.File sourceFile, java.io.File destFile)
    Copies sourceFile to the path specified by destFile.
    static boolean createDirs​(java.io.File file)
    Parses file and creates any non-existent directories in its path
    static byte[] decompressGZipped​(byte[] contentBytes)
    Decompresses a gzipped byte array.
    static byte[] decompressZipped​(byte[] contentBytes)
    Decompresses a zipped byte array.
    static boolean deleteDir​(java.io.File dir)
    Deletes this directory and all of its subdirectories.
    static boolean extractTar​(java.io.File tar_file, java.io.File target_dir)
    Extracts the tar archive tar_file into the directory target_dir.
    static byte[] floatToBytes​(float f)
    Converts a float value to four bytes.
    static java.io.File fullFile​(java.io.File parent, java.lang.String path)
    Returns a file for the parent, path combination; i.e, appending path to parent.
    static byte[] getBytes​(double val, DataType type, java.nio.ByteOrder bo)  
    static java.io.File getCurrentDir()  
    static InterfaceIOType getDefaultIOType​(InterfaceShape shape, int io_type)
    Returns the first instance in InterfaceEnvironment of an InterfaceIOType compatible with shape.
    static java.lang.String getDirectory​(java.lang.String path)
    Returns the lowest directory in this path; path must specify a directory.
    static java.io.FilenameFilter getDirFilter()  
    static javax.swing.filechooser.FileFilter getDirFilter2()  
    static java.util.ArrayList<java.lang.String> getDirs​(java.io.File path)
    Returns all directories, in top-down order, found in path
    static double getDoubleFromBytes​(byte[] b, java.nio.ByteOrder bo)  
    static java.io.FilenameFilter getExtensionFilter​(java.lang.String ext)  
    static javax.swing.filechooser.FileFilter getFileChooserFilter​(java.util.ArrayList<java.lang.String> extensions, java.lang.String description)  
    static int getFileCount​(java.io.File directory)
    Counts the number of files contained in directory (recursive).
    static int getFileCount​(java.io.File directory, boolean recurse)
    Counts the number of files contained in directory.
    static java.io.File[] getFiles​(java.lang.String dir, java.lang.String filter)  
    static float getFloatFromBytes​(byte[] b, java.nio.ByteOrder bo)  
    static java.lang.String getParentPath​(java.lang.String path)
    Returns the path to the parent directory of the file or directory specified by path.
    static java.io.FilenameFilter getPatternFilter​(java.lang.String pattern)
    Contains pattern
    static java.io.FilenameFilter getPatternFilter​(java.lang.String pattern, java.lang.String ext)  
    static java.io.FilenameFilter getRegexDirFilter​(java.lang.String pattern)
    Matches pattern as a regular expression, returning only directories
    static java.io.FilenameFilter getRegexFilter​(java.lang.String pattern)
    Matches pattern as a regular expression
    static java.util.ArrayList<java.lang.String> getSubdirs​(java.io.File dir, boolean recurse)  
    static java.util.ArrayList<java.lang.String> getSupportingTypes​(InterfaceObject object)
    Returns a list of IO types which are compatible (i.e., handle the same set of objects) as type.
    static java.io.File getSystemRoot()
    Returns the root directory of the current operating system.
    static java.io.File getTempDir()
    Returns this system's temporary directory
    static double getValue​(byte[] b, DataType type, java.nio.ByteOrder bo)  
    static java.util.ArrayList<java.io.File> getWildcardPathFiles​(java.io.File root, java.lang.String pattern)
    Matches a wildcard string to all paths, relative to the directory root.
    static void gunzipAndTarFiles​(java.io.File archive, java.io.File target_dir)
    Decompresses the contents of the tar.gz archive in the file archive to target_dir.
    static void gzipAndTarFiles​(java.util.List<java.io.File> zip_files, java.io.File file_out)
    Compresses file using tar/gzip compression.
    static void gzipAndTarFiles​(java.util.List<java.io.File> zip_files, java.io.File file_out, java.lang.String root_dir)
    Compresses file using tar/gzip compression.
    static void gzipFile​(java.io.File file_in, java.io.File file_out)
    Compresses file using gzip compression.
    static byte[] intToByteArray​(int i, java.nio.ByteOrder bo)
    Thanks to http://forum.java.sun.com/thread.jspa?threadID=609364&start=15&tstart=0 for this.
    static byte[] intToBytes​(int i)
    Converts an int value to four bytes.
    static boolean moveDir​(java.io.File dir, java.io.File dest)
    Moves the directory dir and all its contents to the directory dest.
    static boolean moveDir​(java.io.File dir, java.io.File dest, boolean recurse)
    Move the directory dir and all its contents to the directory dest.
    static boolean moveDir​(java.io.File dir, java.io.File dest, boolean recurse, ProgressUpdater progress)
    Move the directory dir and all its contents to the directory dest.
    static boolean moveDirContents​(java.io.File dir, java.io.File dest)
    Move the contents of directory dir (but not the directory itself) to the directory dest.
    static boolean moveDirContents​(java.io.File dir, java.io.File dest, boolean recurse)
    Move the contents of directory dir (but not the directory itself) to the directory dest.
    static boolean moveDirContents​(java.io.File dir, java.io.File dest, boolean recurse, ProgressUpdater progress)
    Move the contents of directory dir (but not the directory itself) to the directory dest.
    static byte[] nextBytes​(java.io.RandomAccessFile in, DataType type, boolean hasAlpha)  
    static byte[] shortToByteArray​(int i, java.nio.ByteOrder bo)  
    static byte[] switchOrder​(byte[] b, int size)  
    static java.io.File unzipArchiveToFile​(java.io.File archive)
    Unzips the contents of archive to the current folder, and returns the path to the folder.
    static java.io.File unzipArchiveToFile​(java.io.File archive, java.io.File target)
    Unzips the contents of archive to a new file, and returns the path to the file.
    static java.io.File unzipFile​(java.io.File archive, java.io.File target)
    Unzips the contents of archive to a new folder, and returns the path to the folder.
    static byte[] valToByteArray​(double val, int type, java.nio.ByteOrder bo)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • IoFunctions

      public IoFunctions()
  • Method Details

    • switchOrder

      public static byte[] switchOrder​(byte[] b, int size)
    • getTempDir

      public static java.io.File getTempDir()
      Returns this system's temporary directory
      Returns:
    • extractTar

      public static boolean extractTar​(java.io.File tar_file, java.io.File target_dir)
      Extracts the tar archive tar_file into the directory target_dir.
      Parameters:
      tar_file -
      target_dir -
      Returns:
    • getDirectory

      public static java.lang.String getDirectory​(java.lang.String path)
      Returns the lowest directory in this path; path must specify a directory.
      Parameters:
      path -
      Returns:
    • getParentPath

      public static java.lang.String getParentPath​(java.lang.String path)
      Returns the path to the parent directory of the file or directory specified by path.
      Parameters:
      path -
      Returns:
    • createDirs

      public static boolean createDirs​(java.io.File file) throws java.io.IOException
      Parses file and creates any non-existent directories in its path
      Parameters:
      file -
      Throws:
      java.io.IOException
    • getDirs

      public static java.util.ArrayList<java.lang.String> getDirs​(java.io.File path)
      Returns all directories, in top-down order, found in path
      Parameters:
      path -
      Returns:
    • unzipArchiveToFile

      public static java.io.File unzipArchiveToFile​(java.io.File archive) throws java.io.IOException
      Unzips the contents of archive to the current folder, and returns the path to the folder. archive must be either a zip or a gzip archive, as specified by its extension.
      Parameters:
      archive -
      Returns:
      the resulting unzipped archive (parent path or file, depending on zip type)
      Throws:
      java.io.IOException
    • unzipArchiveToFile

      public static java.io.File unzipArchiveToFile​(java.io.File archive, java.io.File target) throws java.io.IOException
      Unzips the contents of archive to a new file, and returns the path to the file. archive must be either a zip or a gzip archive, as specified by its extension.
      Parameters:
      archive -
      target - The target directory; if null, the archive directory is used.
      Returns:
      the resulting unzipped archive (parent path or file, depending on zip type)
      Throws:
      java.io.IOException
    • unzipFile

      public static java.io.File unzipFile​(java.io.File archive, java.io.File target) throws java.io.IOException
      Unzips the contents of archive to a new folder, and returns the path to the folder. archive must be either a zip or a gzip archive, as specified by its extension.
      Parameters:
      archive -
      target - The target file; if null, the archive name, minus ".gz", is used.
      Returns:
      the resulting unzipped archive (parent path or file, depending on zip type)
      Throws:
      java.io.IOException
    • gzipFile

      public static void gzipFile​(java.io.File file_in, java.io.File file_out) throws java.io.IOException
      Compresses file using gzip compression.
      Parameters:
      file_in - File to compress
      file_out - File for output
      Throws:
      java.io.IOException - If the compression operation failed
    • gzipAndTarFiles

      public static void gzipAndTarFiles​(java.util.List<java.io.File> zip_files, java.io.File file_out) throws java.io.IOException
      Compresses file using tar/gzip compression.
      Parameters:
      zip_files - List of paths for files to add to archive
      file_out - File for output
      root_dir - Root directory, will be removed from item names
      Throws:
      java.io.IOException - If the compression operation failed
    • gzipAndTarFiles

      public static void gzipAndTarFiles​(java.util.List<java.io.File> zip_files, java.io.File file_out, java.lang.String root_dir) throws java.io.IOException
      Compresses file using tar/gzip compression.
      Parameters:
      zip_files - List of paths for files to add to archive
      file_out - File for output
      Throws:
      java.io.IOException - If the compression operation failed
    • gunzipAndTarFiles

      public static void gunzipAndTarFiles​(java.io.File archive, java.io.File target_dir) throws java.io.IOException
      Decompresses the contents of the tar.gz archive in the file archive to target_dir.
      Parameters:
      archive -
      target_dir -
      Throws:
      java.io.IOException
    • decompressGZipped

      public static byte[] decompressGZipped​(byte[] contentBytes) throws java.io.IOException
      Decompresses a gzipped byte array.
      Parameters:
      contentBytes -
      Returns:
      Throws:
      java.io.IOException
    • compressGZipped

      public static byte[] compressGZipped​(byte[] contentBytes) throws java.io.IOException
      Compresses a gzipped byte array.
      Parameters:
      contentBytes -
      Returns:
      Throws:
      java.io.IOException
    • decompressZipped

      public static byte[] decompressZipped​(byte[] contentBytes) throws java.io.IOException
      Decompresses a zipped byte array.
      Parameters:
      contentBytes -
      Returns:
      Throws:
      java.io.IOException
    • compressZipped

      public static byte[] compressZipped​(byte[] contentBytes) throws java.io.IOException
      Compresses a byte array.
      Parameters:
      contentBytes -
      Returns:
      Throws:
      java.io.IOException
    • floatToBytes

      public static byte[] floatToBytes​(float f)
      Converts a float value to four bytes.
      Parameters:
      f -
      Returns:
    • intToBytes

      public static byte[] intToBytes​(int i)
      Converts an int value to four bytes.
      Parameters:
      f -
      Returns:
    • fullFile

      public static java.io.File fullFile​(java.io.File parent, java.lang.String path)
      Returns a file for the parent, path combination; i.e, appending path to parent.
      Parameters:
      parent -
      path -
      Returns:
    • getCurrentDir

      public static java.io.File getCurrentDir()
    • getSystemRoot

      public static java.io.File getSystemRoot()
      Returns the root directory of the current operating system. If the OS has multiple roots (drives), finds the one which is the ancestor of the user's current directory.
      Returns:
    • getFiles

      public static java.io.File[] getFiles​(java.lang.String dir, java.lang.String filter)
    • getFileCount

      public static int getFileCount​(java.io.File directory)
      Counts the number of files contained in directory (recursive).
      Parameters:
      directory -
      Returns:
    • getFileCount

      public static int getFileCount​(java.io.File directory, boolean recurse)
      Counts the number of files contained in directory.
      Parameters:
      directory -
      recurse - If true, returns all files in subdirectories too
      Returns:
    • copyDir

      public static boolean copyDir​(java.io.File dir, java.io.File dest) throws java.io.IOException
      Copies the directory dir and all its contents to the directory dest. Both must exist and be directories, otherwise method returns false. Method is recursive, meaning all subdirectories will also be copied.
      Parameters:
      dir -
      dest -
      Throws:
      java.io.IOException
    • copyDir

      public static boolean copyDir​(java.io.File dir, java.io.File dest, boolean recurse) throws java.io.IOException
      Copies the directory dir and all its contents to the directory dest. Both must exist and be directories, otherwise method returns false.
      Parameters:
      dir -
      dest -
      recurse -
      Throws:
      java.io.IOException
    • copyDir

      public static boolean copyDir​(java.io.File dir, java.io.File dest, boolean recurse, ProgressUpdater progress) throws java.io.IOException
      Copies the directory dir and all its contents to the directory dest. Both must exist and be directories, otherwise method returns false.
      Parameters:
      dir -
      dest -
      recurse -
      Throws:
      java.io.IOException
    • copyDirContents

      public static boolean copyDirContents​(java.io.File dir, java.io.File dest) throws java.io.IOException
      Copies the contents of directory dir (but not the directory itself) to the directory dest. Both must exist and be directories, otherwise method returns false. Method is recursive, meaning all subdirectories will also be copied.
      Parameters:
      dir -
      dest -
      Throws:
      java.io.IOException
    • copyDirContents

      public static boolean copyDirContents​(java.io.File dir, java.io.File dest, boolean recurse) throws java.io.IOException
      Copies the contents of directory dir (but not the directory itself) to the directory dest. Both must exist and be directories, otherwise method returns false.
      Parameters:
      dir -
      dest -
      recurse -
      Throws:
      java.io.IOException
    • copyDirContents

      public static boolean copyDirContents​(java.io.File dir, java.io.File dest, boolean recurse, ProgressUpdater progress) throws java.io.IOException
      Copies the contents of directory dir (but not the directory itself) to the directory dest. Both must exist and be directories, otherwise method returns false.
      Parameters:
      dir -
      dest -
      recurse -
      Throws:
      java.io.IOException
    • moveDir

      public static boolean moveDir​(java.io.File dir, java.io.File dest) throws java.io.IOException
      Moves the directory dir and all its contents to the directory dest. Both must exist and be directories, otherwise method returns false. Method is recursive, meaning all subdirectories will also be copied.
      Parameters:
      dir -
      dest -
      Throws:
      java.io.IOException
    • moveDir

      public static boolean moveDir​(java.io.File dir, java.io.File dest, boolean recurse) throws java.io.IOException
      Move the directory dir and all its contents to the directory dest. Both must exist and be directories, otherwise method returns false.
      Parameters:
      dir -
      dest -
      recurse -
      Throws:
      java.io.IOException
    • moveDir

      public static boolean moveDir​(java.io.File dir, java.io.File dest, boolean recurse, ProgressUpdater progress) throws java.io.IOException
      Move the directory dir and all its contents to the directory dest. Both must exist and be directories, otherwise method returns false.
      Parameters:
      dir -
      dest -
      recurse -
      Throws:
      java.io.IOException
    • moveDirContents

      public static boolean moveDirContents​(java.io.File dir, java.io.File dest) throws java.io.IOException
      Move the contents of directory dir (but not the directory itself) to the directory dest. Both must exist and be directories, otherwise method returns false. Method is recursive, meaning all subdirectories will also be copied.
      Parameters:
      dir -
      dest -
      Throws:
      java.io.IOException
    • moveDirContents

      public static boolean moveDirContents​(java.io.File dir, java.io.File dest, boolean recurse) throws java.io.IOException
      Move the contents of directory dir (but not the directory itself) to the directory dest. Both must exist and be directories, otherwise method returns false.
      Parameters:
      dir -
      dest -
      recurse -
      Throws:
      java.io.IOException
    • moveDirContents

      public static boolean moveDirContents​(java.io.File dir, java.io.File dest, boolean recurse, ProgressUpdater progress) throws java.io.IOException
      Move the contents of directory dir (but not the directory itself) to the directory dest. Both must exist and be directories, otherwise method returns false.
      Parameters:
      dir -
      dest -
      recurse -
      Throws:
      java.io.IOException
    • getSubdirs

      public static java.util.ArrayList<java.lang.String> getSubdirs​(java.io.File dir, boolean recurse)
    • getFileChooserFilter

      public static javax.swing.filechooser.FileFilter getFileChooserFilter​(java.util.ArrayList<java.lang.String> extensions, java.lang.String description)
    • deleteDir

      public static boolean deleteDir​(java.io.File dir)
      Deletes this directory and all of its subdirectories.
      Parameters:
      dir -
      Returns:
    • copyFile

      public static boolean copyFile​(java.io.File sourceFile, java.io.File destFile) throws java.io.IOException
      Copies sourceFile to the path specified by destFile.
      Parameters:
      sourceFile -
      destFile -
      Throws:
      java.io.IOException
    • getDirFilter

      public static java.io.FilenameFilter getDirFilter()
    • getDirFilter2

      public static javax.swing.filechooser.FileFilter getDirFilter2()
    • getRegexDirFilter

      public static java.io.FilenameFilter getRegexDirFilter​(java.lang.String pattern)
      Matches pattern as a regular expression, returning only directories
      Parameters:
      pattern -
      Returns:
    • getPatternFilter

      public static java.io.FilenameFilter getPatternFilter​(java.lang.String pattern)
      Contains pattern
      Parameters:
      pattern -
      Returns:
    • getRegexFilter

      public static java.io.FilenameFilter getRegexFilter​(java.lang.String pattern)
      Matches pattern as a regular expression
      Parameters:
      pattern -
      Returns:
    • getPatternFilter

      public static java.io.FilenameFilter getPatternFilter​(java.lang.String pattern, java.lang.String ext)
    • getExtensionFilter

      public static java.io.FilenameFilter getExtensionFilter​(java.lang.String ext)
    • getValue

      public static double getValue​(byte[] b, DataType type, java.nio.ByteOrder bo) throws java.io.IOException
      Throws:
      java.io.IOException
    • getDoubleFromBytes

      public static double getDoubleFromBytes​(byte[] b, java.nio.ByteOrder bo)
    • getFloatFromBytes

      public static float getFloatFromBytes​(byte[] b, java.nio.ByteOrder bo)
    • getBytes

      public static byte[] getBytes​(double val, DataType type, java.nio.ByteOrder bo)
    • nextBytes

      public static byte[] nextBytes​(java.io.RandomAccessFile in, DataType type, boolean hasAlpha)
    • intToByteArray

      public static byte[] intToByteArray​(int i, java.nio.ByteOrder bo)
      Thanks to http://forum.java.sun.com/thread.jspa?threadID=609364&start=15&tstart=0 for this.
      Parameters:
      i -
      bo -
      Returns:
    • shortToByteArray

      public static byte[] shortToByteArray​(int i, java.nio.ByteOrder bo)
    • byteArrayToInt

      public static int byteArrayToInt​(byte[] b, java.nio.ByteOrder bo)
    • byteArrayToShort

      public static int byteArrayToShort​(byte[] b, java.nio.ByteOrder bo)
    • byteArrayToVal

      public static double byteArrayToVal​(byte[] b, int type, java.nio.ByteOrder bo)
    • byteArrayToFloat

      public static float byteArrayToFloat​(byte[] b, java.nio.ByteOrder bo)
    • valToByteArray

      public static byte[] valToByteArray​(double val, int type, java.nio.ByteOrder bo)
    • getSupportingTypes

      public static java.util.ArrayList<java.lang.String> getSupportingTypes​(InterfaceObject object)
      Returns a list of IO types which are compatible (i.e., handle the same set of objects) as type.
      Parameters:
      type -
      Returns:
    • getDefaultIOType

      public static InterfaceIOType getDefaultIOType​(InterfaceShape shape, int io_type)
      Returns the first instance in InterfaceEnvironment of an InterfaceIOType compatible with shape.
      Parameters:
      shape - Shape for which to retrieve a type
      io_type - Operation type of IO type
      Returns:
      See Also:
      InterfaceIOType
    • getWildcardPathFiles

      public static java.util.ArrayList<java.io.File> getWildcardPathFiles​(java.io.File root, java.lang.String pattern)
      Matches a wildcard string to all paths, relative to the directory root. If root is null, paths must be absolute (relative to the system root). The wildcard pattern matching uses regular expressions (regex).

      Example:
      data/*/subdir/somedata_*.dat - Searchs "subdir" (if it exists) in all subdirectories of "data" for file names matching "somedata_*.dat"

      Parameters:
      root -
      pattern -
      Returns: