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 directorydir
and all its contents to the directorydest
.static boolean
copyDir(java.io.File dir, java.io.File dest, boolean recurse)
Copies the directorydir
and all its contents to the directorydest
.static boolean
copyDir(java.io.File dir, java.io.File dest, boolean recurse, ProgressUpdater progress)
Copies the directorydir
and all its contents to the directorydest
.static boolean
copyDirContents(java.io.File dir, java.io.File dest)
Copies the contents of directorydir
(but not the directory itself) to the directorydest
.static boolean
copyDirContents(java.io.File dir, java.io.File dest, boolean recurse)
Copies the contents of directorydir
(but not the directory itself) to the directorydest
.static boolean
copyDirContents(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 boolean
copyFile(java.io.File sourceFile, java.io.File destFile)
CopiessourceFile
to the path specified bydestFile
.static boolean
createDirs(java.io.File file)
Parsesfile
and 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 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 archivetar_file
into the directorytarget_dir
.static byte[]
floatToBytes(float f)
Converts afloat
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, appendingpath
toparent
.static byte[]
getBytes(double val, DataType type, java.nio.ByteOrder bo)
static java.io.File
getCurrentDir()
static InterfaceIOType
getDefaultIOType(InterfaceShape shape, int io_type)
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 pathstatic 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 indirectory
(recursive).static int
getFileCount(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 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 bypath
.static java.io.FilenameFilter
getPatternFilter(java.lang.String pattern)
Containspattern
static java.io.FilenameFilter
getPatternFilter(java.lang.String pattern, java.lang.String ext)
static java.io.FilenameFilter
getRegexDirFilter(java.lang.String pattern)
Matchespattern
as a regular expression, returning only directoriesstatic java.io.FilenameFilter
getRegexFilter(java.lang.String pattern)
Matchespattern
as 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.File
getSystemRoot()
Returns the root directory of the current operating system.static java.io.File
getTempDir()
Returns this system's temporary directorystatic 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 directoryroot
.static void
gunzipAndTarFiles(java.io.File archive, java.io.File target_dir)
Decompresses the contents of the tar.gz archive in the filearchive
totarget_dir
.static void
gzipAndTarFiles(java.util.List<java.io.File> zip_files, java.io.File file_out)
Compressesfile
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)
Compressesfile
using tar/gzip compression.static void
gzipFile(java.io.File file_in, java.io.File file_out)
Compressesfile
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 anint
value to four bytes.static boolean
moveDir(java.io.File dir, java.io.File dest)
Moves the directorydir
and all its contents to the directorydest
.static boolean
moveDir(java.io.File dir, java.io.File dest, boolean recurse)
Move the directorydir
and all its contents to the directorydest
.static boolean
moveDir(java.io.File dir, java.io.File dest, boolean recurse, ProgressUpdater progress)
Move the directorydir
and all its contents to the directorydest
.static boolean
moveDirContents(java.io.File dir, java.io.File dest)
Move the contents of directorydir
(but not the directory itself) to the directorydest
.static boolean
moveDirContents(java.io.File dir, java.io.File dest, boolean recurse)
Move the contents of directorydir
(but not the directory itself) to the directorydest
.static boolean
moveDirContents(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.File
unzipArchiveToFile(java.io.File archive)
Unzips the contents ofarchive
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 ofarchive
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 ofarchive
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 archivetar_file
into 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;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 bypath
.- Parameters:
path
-- Returns:
-
createDirs
public static boolean createDirs(java.io.File file) throws java.io.IOExceptionParsesfile
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.IOExceptionUnzips the contents ofarchive
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.IOExceptionUnzips the contents ofarchive
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; 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 ofarchive
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; 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.IOExceptionCompressesfile
using 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.IOExceptionCompressesfile
using 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.IOExceptionCompressesfile
using 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 filearchive
totarget_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 afloat
value to four bytes.- Parameters:
f
-- Returns:
-
intToBytes
public static byte[] intToBytes(int i)Converts anint
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, appendingpath
toparent
.- 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 directorydir
and 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 directorydir
and 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 directorydir
and 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 directorydir
and 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 directorydir
and 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 directorydir
and 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.IOExceptionCopiessourceFile
to 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)Matchespattern
as 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)Matchespattern
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
-
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
. Ifroot
isnull
, 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:
-