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 classIoFunctions.FileType -
Constructor Summary
Constructors Constructor Description IoFunctions() -
Method Summary
Modifier and Type Method Description static floatbyteArrayToFloat(byte[] b, java.nio.ByteOrder bo)static intbyteArrayToInt(byte[] b, java.nio.ByteOrder bo)static intbyteArrayToShort(byte[] b, java.nio.ByteOrder bo)static doublebyteArrayToVal(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 booleancopyDir(java.io.File dir, java.io.File dest)Copies the directorydirand all its contents to the directorydest.static booleancopyDir(java.io.File dir, java.io.File dest, boolean recurse)Copies the directorydirand all its contents to the directorydest.static booleancopyDir(java.io.File dir, java.io.File dest, boolean recurse, ProgressUpdater progress)Copies the directorydirand all its contents to the directorydest.static booleancopyDirContents(java.io.File dir, java.io.File dest)Copies the contents of directorydir(but not the directory itself) to the directorydest.static booleancopyDirContents(java.io.File dir, java.io.File dest, boolean recurse)Copies the contents of directorydir(but not the directory itself) to the directorydest.static booleancopyDirContents(java.io.File dir, java.io.File dest, boolean recurse, ProgressUpdater progress)Copies the contents of directorydir(but not the directory itself) to the directorydest.static booleancopyFile(java.io.File sourceFile, java.io.File destFile)CopiessourceFileto the path specified bydestFile.static booleancreateDirs(java.io.File file)Parsesfileand creates any non-existent directories in its pathstatic byte[]decompressGZipped(byte[] contentBytes)Decompresses a gzipped byte array.static byte[]decompressZipped(byte[] contentBytes)Decompresses a zipped byte array.static booleandeleteDir(java.io.File dir)Deletes this directory and all of its subdirectories.static booleanextractTar(java.io.File tar_file, java.io.File target_dir)Extracts the tar archivetar_fileinto the directorytarget_dir.static byte[]floatToBytes(float f)Converts afloatvalue to four bytes.static java.io.FilefullFile(java.io.File parent, java.lang.String path)Returns a file for the parent, path combination; i.e, appendingpathtoparent.static byte[]getBytes(double val, DataType type, java.nio.ByteOrder bo)static java.io.FilegetCurrentDir()static InterfaceIOTypegetDefaultIOType(InterfaceShape shape, int io_type)static java.lang.StringgetDirectory(java.lang.String path)Returns the lowest directory in this path;pathmust specify a directory.static java.io.FilenameFiltergetDirFilter()static javax.swing.filechooser.FileFiltergetDirFilter2()static java.util.ArrayList<java.lang.String>getDirs(java.io.File path)Returns all directories, in top-down order, found in pathstatic doublegetDoubleFromBytes(byte[] b, java.nio.ByteOrder bo)static java.io.FilenameFiltergetExtensionFilter(java.lang.String ext)static javax.swing.filechooser.FileFiltergetFileChooserFilter(java.util.ArrayList<java.lang.String> extensions, java.lang.String description)static intgetFileCount(java.io.File directory)Counts the number of files contained indirectory(recursive).static intgetFileCount(java.io.File directory, boolean recurse)Counts the number of files contained indirectory.static java.io.File[]getFiles(java.lang.String dir, java.lang.String filter)static floatgetFloatFromBytes(byte[] b, java.nio.ByteOrder bo)static java.lang.StringgetParentPath(java.lang.String path)Returns the path to the parent directory of the file or directory specified bypath.static java.io.FilenameFiltergetPatternFilter(java.lang.String pattern)Containspatternstatic java.io.FilenameFiltergetPatternFilter(java.lang.String pattern, java.lang.String ext)static java.io.FilenameFiltergetRegexDirFilter(java.lang.String pattern)Matchespatternas a regular expression, returning only directoriesstatic java.io.FilenameFiltergetRegexFilter(java.lang.String pattern)Matchespatternas a regular expressionstatic 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) astype.static java.io.FilegetSystemRoot()Returns the root directory of the current operating system.static java.io.FilegetTempDir()Returns this system's temporary directorystatic doublegetValue(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 directoryroot.static voidgunzipAndTarFiles(java.io.File archive, java.io.File target_dir)Decompresses the contents of the tar.gz archive in the filearchivetotarget_dir.static voidgzipAndTarFiles(java.util.List<java.io.File> zip_files, java.io.File file_out)Compressesfileusing tar/gzip compression.static voidgzipAndTarFiles(java.util.List<java.io.File> zip_files, java.io.File file_out, java.lang.String root_dir)Compressesfileusing tar/gzip compression.static voidgzipFile(java.io.File file_in, java.io.File file_out)Compressesfileusing 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 anintvalue to four bytes.static booleanmoveDir(java.io.File dir, java.io.File dest)Moves the directorydirand all its contents to the directorydest.static booleanmoveDir(java.io.File dir, java.io.File dest, boolean recurse)Move the directorydirand all its contents to the directorydest.static booleanmoveDir(java.io.File dir, java.io.File dest, boolean recurse, ProgressUpdater progress)Move the directorydirand all its contents to the directorydest.static booleanmoveDirContents(java.io.File dir, java.io.File dest)Move the contents of directorydir(but not the directory itself) to the directorydest.static booleanmoveDirContents(java.io.File dir, java.io.File dest, boolean recurse)Move the contents of directorydir(but not the directory itself) to the directorydest.static booleanmoveDirContents(java.io.File dir, java.io.File dest, boolean recurse, ProgressUpdater progress)Move the contents of directorydir(but not the directory itself) to the directorydest.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.FileunzipArchiveToFile(java.io.File archive)Unzips the contents ofarchiveto the current folder, and returns the path to the folder.static java.io.FileunzipArchiveToFile(java.io.File archive, java.io.File target)Unzips the contents ofarchiveto a new file, and returns the path to the file.static java.io.FileunzipFile(java.io.File archive, java.io.File target)Unzips the contents ofarchiveto 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 archivetar_fileinto the directorytarget_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;pathmust 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 bypath.- Parameters:
path-- Returns:
-
createDirs
public static boolean createDirs(java.io.File file) throws java.io.IOExceptionParsesfileand 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.IOExceptionUnzips the contents ofarchiveto the current folder, and returns the path to the folder.archivemust 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.IOExceptionUnzips the contents ofarchiveto a new file, and returns the path to the file.archivemust be either a zip or a gzip archive, as specified by its extension.- Parameters:
archive-target- The target directory; ifnull, 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.IOExceptionUnzips the contents ofarchiveto a new folder, and returns the path to the folder.archivemust be either a zip or a gzip archive, as specified by its extension.- Parameters:
archive-target- The target file; ifnull, 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.IOExceptionCompressesfileusing gzip compression.- Parameters:
file_in- File to compressfile_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.IOExceptionCompressesfileusing tar/gzip compression.- Parameters:
zip_files- List of paths for files to add to archivefile_out- File for outputroot_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.IOExceptionCompressesfileusing tar/gzip compression.- Parameters:
zip_files- List of paths for files to add to archivefile_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.IOExceptionDecompresses the contents of the tar.gz archive in the filearchivetotarget_dir.- Parameters:
archive-target_dir-- Throws:
java.io.IOException
-
decompressGZipped
public static byte[] decompressGZipped(byte[] contentBytes) throws java.io.IOExceptionDecompresses a gzipped byte array.- Parameters:
contentBytes-- Returns:
- Throws:
java.io.IOException
-
compressGZipped
public static byte[] compressGZipped(byte[] contentBytes) throws java.io.IOExceptionCompresses a gzipped byte array.- Parameters:
contentBytes-- Returns:
- Throws:
java.io.IOException
-
decompressZipped
public static byte[] decompressZipped(byte[] contentBytes) throws java.io.IOExceptionDecompresses a zipped byte array.- Parameters:
contentBytes-- Returns:
- Throws:
java.io.IOException
-
compressZipped
public static byte[] compressZipped(byte[] contentBytes) throws java.io.IOExceptionCompresses a byte array.- Parameters:
contentBytes-- Returns:
- Throws:
java.io.IOException
-
floatToBytes
public static byte[] floatToBytes(float f)Converts afloatvalue to four bytes.- Parameters:
f-- Returns:
-
intToBytes
public static byte[] intToBytes(int i)Converts anintvalue 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, appendingpathtoparent.- 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 indirectory(recursive).- Parameters:
directory-- Returns:
-
getFileCount
public static int getFileCount(java.io.File directory, boolean recurse)Counts the number of files contained indirectory.- Parameters:
directory-recurse- Iftrue, returns all files in subdirectories too- Returns:
-
copyDir
public static boolean copyDir(java.io.File dir, java.io.File dest) throws java.io.IOExceptionCopies the directorydirand all its contents to the directorydest. Both must exist and be directories, otherwise method returnsfalse. 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.IOExceptionCopies the directorydirand all its contents to the directorydest. Both must exist and be directories, otherwise method returnsfalse.- 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.IOExceptionCopies the directorydirand all its contents to the directorydest. Both must exist and be directories, otherwise method returnsfalse.- Parameters:
dir-dest-recurse-- Throws:
java.io.IOException
-
copyDirContents
public static boolean copyDirContents(java.io.File dir, java.io.File dest) throws java.io.IOExceptionCopies the contents of directorydir(but not the directory itself) to the directorydest. Both must exist and be directories, otherwise method returnsfalse. 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.IOExceptionCopies the contents of directorydir(but not the directory itself) to the directorydest. Both must exist and be directories, otherwise method returnsfalse.- 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.IOExceptionCopies the contents of directorydir(but not the directory itself) to the directorydest. Both must exist and be directories, otherwise method returnsfalse.- Parameters:
dir-dest-recurse-- Throws:
java.io.IOException
-
moveDir
public static boolean moveDir(java.io.File dir, java.io.File dest) throws java.io.IOExceptionMoves the directorydirand all its contents to the directorydest. Both must exist and be directories, otherwise method returnsfalse. 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.IOExceptionMove the directorydirand all its contents to the directorydest. Both must exist and be directories, otherwise method returnsfalse.- 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.IOExceptionMove the directorydirand all its contents to the directorydest. Both must exist and be directories, otherwise method returnsfalse.- Parameters:
dir-dest-recurse-- Throws:
java.io.IOException
-
moveDirContents
public static boolean moveDirContents(java.io.File dir, java.io.File dest) throws java.io.IOExceptionMove the contents of directorydir(but not the directory itself) to the directorydest. Both must exist and be directories, otherwise method returnsfalse. 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.IOExceptionMove the contents of directorydir(but not the directory itself) to the directorydest. Both must exist and be directories, otherwise method returnsfalse.- 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.IOExceptionMove the contents of directorydir(but not the directory itself) to the directorydest. Both must exist and be directories, otherwise method returnsfalse.- 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.IOExceptionCopiessourceFileto the path specified bydestFile.- 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)Matchespatternas a regular expression, returning only directories- Parameters:
pattern-- Returns:
-
getPatternFilter
public static java.io.FilenameFilter getPatternFilter(java.lang.String pattern)Containspattern- Parameters:
pattern-- Returns:
-
getRegexFilter
public static java.io.FilenameFilter getRegexFilter(java.lang.String pattern)Matchespatternas 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
-
nextBytes
-
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
Returns a list of IO types which are compatible (i.e., handle the same set of objects) astype.- Parameters:
type-- Returns:
-
getDefaultIOType
- Parameters:
shape- Shape for which to retrieve a typeio_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 directoryroot. Ifrootisnull, 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:
-