ucar.unidata.util
Class IOUtil

java.lang.Object
  extended by ucar.unidata.util.IOUtil

public class IOUtil
extends Object

A set of io related utilities

Version:
$Revision: 1.52 $
Author:
IDV development group.

Nested Class Summary
static interface IOUtil.FileViewer
          FileViewer is used to walk dir trees
 
Field Summary
static boolean debug
          debug flag
 
Constructor Summary
IOUtil()
          Default constructor; does nothing
 
Method Summary
static String cleanFileName(String name)
          Remove illegal characters in the given filename
static void copyFile(File from, File to)
          Copy from file to to file
static void deleteDirectory(File root)
          This will recursively delete all contents under the given directory.
static List getDirectories(File dir, boolean recurse)
          Recursively descend (if recurse is true) through the given directory and return a list of all subdirectories.
static List getDirectories(List dirs, boolean recurse)
          Recursively descend (if recurse is true) through the given directories and return a list of all subdirectories.
static String getFileExtension(String f)
          Return the file extension from the given file (including the ".").
static String getFileRoot(String f)
          Excise the filename from the given path and return the root.
static List getFiles(File dir, boolean recurse)
          Recursively descend (if recurse is true) through the given directory and return a list of all files
static List getFiles(List files, File dir, boolean recurse)
          Recursively descend (if recurse is true) through the given directory and return a list of all files
static List getFiles(List files, File dir, boolean recurse, PatternFileFilter filter)
          Recursively descend (if recurse is true) through the given directory and return a list of all files
static String getFileTail(String f)
          Gets the file name, removing any leading directory paths.
static InputStream getInputStream(String filename)
          Get an input stream for the filename
static InputStream getInputStream(String filename, Class origin)
          Get an input stream for the filename
static InputStream getInputStreamOLDWAY(String filename, Class origin)
          Get an input stream for the filename
static List<String> getListing(String path, Class c)
          Lists the contents in the given file directory or java resource directory
static File getMostRecentFile(File dir)
          Find the youngest file in the given directory.
static File getMostRecentFile(File dir, FileFilter filter)
          Find the youngest file in the given directory that matches the given FileFilter.
static File getMostRecentFile(File dir, FileFilter filter)
          Find the youngest file in the given directory that matches the given FileFilter.
static File[] getNormalFiles(File[] files)
          Return an array of the file Files.
static URL getURL(String filename, Class origin)
          Get an input stream for the filename
static boolean hasSuffix(String fileOrUrl, String suffix)
          Does the given file or url have the given suffix
static boolean isHtmlFile(String filenameOrUrl)
          Determine if the given filename is an html file.
static boolean isHtmlSuffix(String url)
          Does the given url end with an html suffix.
static boolean isHttpProtocol(String url)
          Is the given url an http protocol.
static boolean isImageFile(String filename)
          Determine if the given filename is an image file (e.g., ends with .gif, .jpg, .jpeg, .png)
static boolean isRelativePath(String path)
          Is the given path relative
static boolean isTextFile(String filename)
          Determine if the given filename is a text file.
static String joinDir(File f1, String filename)
          Return the String representation of the given filename joined to the given directory f1.
static String joinDir(String f1, String f2)
          Return the String representation of the given filename joined to the given directory.
static void main(String[] args)
          test main
static String makeDir(File f)
          If the directory defined in the given argument f does not exist then make it.
static String makeDir(String path)
          If the directory defined in the given argument path does not exist then make it.
static void makeDirRecursive(File f)
          Make the directory.
static void moveFile(File from, File to)
          Move the from file to the to file
static void pruneEmptyDirectories(File root)
          This will recursively prune empty subdirectories of the given root directory.
static void pruneIfEmpty(File root)
          This will recursively prune empty subdirectories of the given root directory.
static byte[] readBytes(InputStream is)
          Read in the bytes from the given InputStream Closes the InputStream argument.
static byte[] readBytes(InputStream is, Object loadId)
          Read in the bytes from the given InputStream Closes the InputStream argument.
static byte[] readBytes(InputStream is, Object loadId, boolean closeIt)
          Read the bytes in the given input stream.
static byte[] readBytesAndCache(String contentName, String cacheGroup)
          See if the content is in the perma-cache.
static byte[] readBytesAndCache(String contentName, String cacheGroup, boolean unzipIfNeeded)
          See if the content is in the perma-cache.
static String readContents(File file)
          Read the contents of a File.
static String readContents(InputStream is)
          Read in the bytes from the given InputStream and construct and return a String.
static String readContents(String contentName)
          Return the String contents of the specified contentName.
static String readContents(String contentName, Class origin)
          Return the String contents of the specified contentName.
static String readContents(String contentName, Class origin, String dflt)
          Return the String contents of the specified contentName.
static String readContents(String contentName, String dflt)
          Return the String contents of the specified contentName.
static File[] sortFilesOnAge(File[] files, boolean youngestFirst)
          Sort the given files
static File[] sortFilesOnAge(File directory, FileFilter filter, boolean youngestFirst)
          Sort the files contained by the given directory and that (if non-null) match the given filter.
static String stripExtension(String f)
          Remove any file extension from the given file name.
static File[] toFiles(List files)
          Convert the toString value of the objects in the given files list to an array of File-s
static void wait(File directory, String filePattern, double sleepSeconds)
          Wait until there are new files in the given directory.
static void wait(List files, double sleepSeconds)
          Wait until one or more of the files in the files list (File) has changed.
static boolean walkDirectory(File dir, IOUtil.FileViewer fileViewer)
          Walk the dir tree with the given file viewer
static boolean walkDirectory(File dir, IOUtil.FileViewer fileViewer, int level)
          Walk the dir tree with the given file viewer
static FileFilter wrapFilter(FileFilter filter)
          Create a javaio FileFilter from the filechooser package file filter.
static void writeBytes(File filename, byte[] contents)
          Write out a file to the File specified.
static void writeFile(File filename, String contents)
          Write out a file to the File specified.
static void writeFile(String filename, String contents)
          Write out a file to the filename specified.
static void writeJarFile(String filename, List files)
          Write out the list of files to the jar file specified by filename
static void writeJarFile(String filename, List files, String pathPrefix)
          Write out the list of files to the jar file specified by filename
static void writeJarFile(String filename, List files, String pathPrefix, boolean makeFilesUnique)
          Write out the list of files to the jar file specified by filename
static int writeTo(InputStream from, OutputStream to)
          Copy the input stream to the output stream
static int writeTo(InputStream from, OutputStream to, Object loadId, int length)
          Copy the input stream to the output stream
static List writeTo(List urls, String prefix, String suffix)
          Copy the files pointed to by the urls list to the directory/file pointed to by prefx.
static List writeTo(List urls, String prefix, String suffix, Object loadId)
          Copy the files pointed to by the urls list to the directory/file pointed to by prefx.
static int writeTo(URL from, File file, Object loadId)
          Write to the file from the URL stream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

debug

public static boolean debug
debug flag

Constructor Detail

IOUtil

public IOUtil()
Default constructor; does nothing

Method Detail

hasSuffix

public static boolean hasSuffix(String fileOrUrl,
                                String suffix)
Does the given file or url have the given suffix

Parameters:
fileOrUrl - The name of the file or url
suffix - The suffix
Returns:
Does the fileOrUrl have the suffix

getMostRecentFile

public static File getMostRecentFile(File dir)
Find the youngest file in the given directory.

Parameters:
dir - The directory to search in.
Returns:
The most recent file (or null if none found).

getMostRecentFile

public static File getMostRecentFile(File dir,
                                     FileFilter filter)
Find the youngest file in the given directory that matches the given FileFilter.

Parameters:
dir - The directory to search in.
filter - The FileFilter to be used to limit what files we look at (may be null).
Returns:
The most recent file (or null if none found).

sortFilesOnAge

public static File[] sortFilesOnAge(File directory,
                                    FileFilter filter,
                                    boolean youngestFirst)
Sort the files contained by the given directory and that (if non-null) match the given filter.

Parameters:
directory - The directory
filter - The filter
youngestFirst - Ascending or descending
Returns:
The sorted files

getNormalFiles

public static File[] getNormalFiles(File[] files)
Return an array of the file Files. That is, the ones where File.isFile is true

Parameters:
files - Array of files
Returns:
The files

sortFilesOnAge

public static File[] sortFilesOnAge(File[] files,
                                    boolean youngestFirst)
Sort the given files

Parameters:
files - The files
youngestFirst - Ascending or descending
Returns:
Just return the given array

toFiles

public static File[] toFiles(List files)
Convert the toString value of the objects in the given files list to an array of File-s

Parameters:
files - List of files
Returns:
array of files

wrapFilter

public static FileFilter wrapFilter(FileFilter filter)
Create a javaio FileFilter from the filechooser package file filter.

Parameters:
filter - The filechooser file filter
Returns:
The javaio FileFilter.

getMostRecentFile

public static File getMostRecentFile(File dir,
                                     FileFilter filter)
Find the youngest file in the given directory that matches the given FileFilter.

Parameters:
dir - The directory to search in.
filter - The filter to be used to limit what files we look at (may be null).
Returns:
The most recent file (or null if none found).

writeTo

public static int writeTo(InputStream from,
                          OutputStream to)
                   throws IOException
Copy the input stream to the output stream

Parameters:
from - input stream
to - output
Returns:
How may bytes were written
Throws:
IOException - On badness

writeTo

public static int writeTo(URL from,
                          File file,
                          Object loadId)
                   throws IOException
Write to the file from the URL stream

Parameters:
from - URL for input
file - file for output
loadId - A JobManager loadId that, if set, can be used to stop the load
Returns:
number of bytes written
Throws:
IOException - problem writing to file.

writeTo

public static int writeTo(InputStream from,
                          OutputStream to,
                          Object loadId,
                          int length)
                   throws IOException
Copy the input stream to the output stream

Parameters:
from - input stream
to - output
loadId - A JobManager loadId that, if set, can be used to stop the load
length - number of bytes to write
Returns:
How may bytes were written
Throws:
IOException - On badness

writeTo

public static List writeTo(List urls,
                           String prefix,
                           String suffix)
                    throws IOException
Copy the files pointed to by the urls list to the directory/file pointed to by prefx. We copy prefix0.suffix,prefix1.suffix, ..., prefixN.suffix

Parameters:
urls - List of urls to copy
prefix - file directory prefix
suffix - suffix
Returns:
List of new files
Throws:
IOException - On badness

writeTo

public static List writeTo(List urls,
                           String prefix,
                           String suffix,
                           Object loadId)
                    throws IOException
Copy the files pointed to by the urls list to the directory/file pointed to by prefx. We copy prefix0.suffix,prefix1.suffix, ..., prefixN.suffix

Parameters:
urls - List of urls to copy
prefix - file directory prefix
suffix - suffix
loadId - JobManager loadId
Returns:
List of new files
Throws:
IOException - On badness

getListing

public static List<String> getListing(String path,
                                      Class c)
Lists the contents in the given file directory or java resource directory

Parameters:
path - may be a file directory path or a java resource directory
c - base class to use if its java resources
Returns:
List of sub files and directories

getFileTail

public static String getFileTail(String f)
Gets the file name, removing any leading directory paths.

Parameters:
f - The file path.
Returns:
The file name.

getFileRoot

public static String getFileRoot(String f)
Excise the filename from the given path and return the root.

Parameters:
f - The file path.
Returns:
The file name.

stripExtension

public static String stripExtension(String f)
Remove any file extension from the given file name.

Parameters:
f - The file path.
Returns:
The file name without the extension.

cleanFileName

public static String cleanFileName(String name)
Remove illegal characters in the given filename

Parameters:
name - The filename to be cleaned up
Returns:
The cleaned up filename

getFileExtension

public static String getFileExtension(String f)
Return the file extension from the given file (including the ".").

Parameters:
f - The file path.
Returns:
The file extension or an empty string if none found.

writeFile

public static void writeFile(String filename,
                             String contents)
                      throws FileNotFoundException,
                             IOException
Write out a file to the filename specified.

Parameters:
filename - filename to write to
contents - file contents
Throws:
FileNotFoundException - if the file does not exist
IOException - if there is a problem writing

writeFile

public static void writeFile(File filename,
                             String contents)
                      throws FileNotFoundException,
                             IOException
Write out a file to the File specified.

Parameters:
filename - File to write to
contents - file contents
Throws:
FileNotFoundException - if the file does not exist
IOException - if there is a problem writing

writeBytes

public static void writeBytes(File filename,
                              byte[] contents)
                       throws FileNotFoundException,
                              IOException
Write out a file to the File specified.

Parameters:
filename - File to write to
contents - file contents
Throws:
FileNotFoundException - if the file does not exist
IOException - if there is a problem writing

moveFile

public static void moveFile(File from,
                            File to)
                     throws FileNotFoundException,
                            IOException
Move the from file to the to file

Parameters:
from - File to move
to - The destination
Throws:
FileNotFoundException - When we cannot find the file
IOException - When something untoward happens

copyFile

public static void copyFile(File from,
                            File to)
                     throws FileNotFoundException,
                            IOException
Copy from file to to file

Parameters:
from - File to copy
to - The destination
Throws:
FileNotFoundException - When we cannot find the file
IOException - When something untoward happens

isTextFile

public static boolean isTextFile(String filename)
Determine if the given filename is a text file. i.e., it ends with .txt or .text

Parameters:
filename - The filename to check.
Returns:
Is the filename a text file.

isImageFile

public static boolean isImageFile(String filename)
Determine if the given filename is an image file (e.g., ends with .gif, .jpg, .jpeg, .png)

Parameters:
filename - The filename to check.
Returns:
Is the filename an image file.

isHtmlFile

public static boolean isHtmlFile(String filenameOrUrl)
Determine if the given filename is an html file. i.e., it ends with .htm or .html or if it does not have a file extension and begins with http:

Parameters:
filenameOrUrl - The filename to check.
Returns:
Is the filename an html file.

isHtmlSuffix

public static boolean isHtmlSuffix(String url)
Does the given url end with an html suffix.

Parameters:
url - The url
Returns:
Ends with html, htm, or shtml

isHttpProtocol

public static boolean isHttpProtocol(String url)
Is the given url an http protocol.

Parameters:
url - The url
Returns:
Starts with http or https

getInputStream

public static InputStream getInputStream(String filename)
                                  throws FileNotFoundException,
                                         IOException
Get an input stream for the filename

Parameters:
filename - name of file
Returns:
corresponding input stream
Throws:
FileNotFoundException - couldn't find the file
IOException - problem opening stream

getInputStream

public static InputStream getInputStream(String filename,
                                         Class origin)
                                  throws FileNotFoundException,
                                         IOException
Get an input stream for the filename

Parameters:
filename - name of file
origin - relative origin point for file location
Returns:
corresponding input stream
Throws:
FileNotFoundException - couldn't find the file
IOException - problem opening stream

getInputStreamOLDWAY

public static InputStream getInputStreamOLDWAY(String filename,
                                               Class origin)
                                        throws FileNotFoundException,
                                               IOException
Get an input stream for the filename

Parameters:
filename - name of file
origin - relative origin point for file location
Returns:
corresponding input stream
Throws:
FileNotFoundException - couldn't find the file
IOException - problem opening stream

getURL

public static URL getURL(String filename,
                         Class origin)
                  throws FileNotFoundException,
                         IOException
Get an input stream for the filename

Parameters:
filename - name of file
origin - relative origin point for file location
Returns:
corresponding input stream
Throws:
FileNotFoundException - couldn't find the file
IOException - problem opening stream

readContents

public static String readContents(File file)
                           throws FileNotFoundException,
                                  IOException
Read the contents of a File. Used for reading text type files (XML, HTML, etc)

Parameters:
file - file to read.
Returns:
contents as a String
Throws:
FileNotFoundException - couldn't find the file
IOException - problem opening stream

readContents

public static String readContents(String contentName,
                                  String dflt)
Return the String contents of the specified contentName. If the read fails (for whatever reason) then return the dflt parameter

Parameters:
contentName - URL or filename
dflt - default to return if a problem
Returns:
contents or default value

readContents

public static String readContents(String contentName,
                                  Class origin,
                                  String dflt)
Return the String contents of the specified contentName. If the read fails (for whatever reason) then return the dflt parameter

Parameters:
contentName - URL or filename
origin - origin class
dflt - default to return if a problem
Returns:
contents or default value

readContents

public static String readContents(String contentName)
                           throws FileNotFoundException,
                                  IOException
Return the String contents of the specified contentName.

Parameters:
contentName - can either be a URL, a filename or a resource.
Returns:
contents or null if there is a problem.
Throws:
FileNotFoundException - couldn't find the file
IOException - problem reading contents

isRelativePath

public static boolean isRelativePath(String path)
Is the given path relative

Parameters:
path - file path
Returns:
is relative

readContents

public static String readContents(String contentName,
                                  Class origin)
                           throws FileNotFoundException,
                                  IOException
Return the String contents of the specified contentName. contentName can either be a URL, a filename or a resource.

Parameters:
contentName - can either be a URL, a filename or a resource.
origin - relative origin for path to file
Returns:
contents or null if there is a problem.
Throws:
FileNotFoundException - couldn't find the file
IOException - problem reading contents

readBytesAndCache

public static byte[] readBytesAndCache(String contentName,
                                       String cacheGroup)
                                throws FileNotFoundException,
                                       IOException
See if the content is in the perma-cache. If it is then return it. Else read it (e.g., from a url) and cache it.

Parameters:
contentName - url or filename
cacheGroup - Cache group
Returns:
Bytes read
Throws:
FileNotFoundException - On badness
IOException - On badness

readBytesAndCache

public static byte[] readBytesAndCache(String contentName,
                                       String cacheGroup,
                                       boolean unzipIfNeeded)
                                throws FileNotFoundException,
                                       IOException
See if the content is in the perma-cache. If it is then return it. Else read it (e.g., from a url) and cache it.

Parameters:
contentName - url or filename
cacheGroup - Cache group
unzipIfNeeded - IF true and if the url is a zip file then unzip it
Returns:
Bytes read
Throws:
FileNotFoundException - On badness
IOException - On badness

readContents

public static String readContents(InputStream is)
                           throws IOException
Read in the bytes from the given InputStream and construct and return a String. Closes the InputStream argument.

Parameters:
is - InputStream to read from
Returns:
contents as a String
Throws:
IOException - problem reading contents

readBytes

public static byte[] readBytes(InputStream is)
                        throws IOException
Read in the bytes from the given InputStream Closes the InputStream argument.

Parameters:
is - InputStream to read from
Returns:
bytes read
Throws:
IOException - problem reading contents

readBytes

public static byte[] readBytes(InputStream is,
                               Object loadId)
                        throws IOException
Read in the bytes from the given InputStream Closes the InputStream argument. The globalTimestamp and myTimestamp parameters, if non-null, allow calling routines to abort the read. globalTimestamp, if non-null, is a array of size one that holds a virtual timestamp. If at anytime during the read of the bytes the value in globalTimestamp is different then the value given for myTimestamp then the read is aborted and null is returned.

Parameters:
is - InputStream to read from
loadId - Job manager load id
Returns:
bytes read
Throws:
IOException - problem reading contents

readBytes

public static byte[] readBytes(InputStream is,
                               Object loadId,
                               boolean closeIt)
                        throws IOException
Read the bytes in the given input stream.

Parameters:
is - The input stream
loadId - If non-null check with the JobManager if we should continue
closeIt - If true then close the input stream
Returns:
The bytes
Throws:
IOException - On badness

joinDir

public static String joinDir(String f1,
                             String f2)
Return the String representation of the given filename joined to the given directory.

Parameters:
f1 - directory path
f2 - filename
Returns:
concatenated String with the appropriate file separator

joinDir

public static String joinDir(File f1,
                             String filename)
Return the String representation of the given filename joined to the given directory f1.

Parameters:
f1 - directory path
filename - filename
Returns:
concatenated String with the appropriate file separator

makeDir

public static String makeDir(String path)
If the directory defined in the given argument path does not exist then make it.

Parameters:
path - directory to make
Returns:
the directory path

makeDir

public static String makeDir(File f)
If the directory defined in the given argument f does not exist then make it.

Parameters:
f - directory as a file
Returns:
directory path

makeDirRecursive

public static final void makeDirRecursive(File f)
Make the directory. This will recurse and make the directory tree if needed

Parameters:
f - directory to make

getFiles

public static List getFiles(File dir,
                            boolean recurse)
Recursively descend (if recurse is true) through the given directory and return a list of all files

Parameters:
dir - The directory to look at
recurse - Do we recurse
Returns:
List of files

getFiles

public static List getFiles(List files,
                            File dir,
                            boolean recurse)
Recursively descend (if recurse is true) through the given directory and return a list of all files

Parameters:
files - The list of files to add to
dir - The directory to look at
recurse - Do we recurse
Returns:
List of files

getFiles

public static List getFiles(List files,
                            File dir,
                            boolean recurse,
                            PatternFileFilter filter)
Recursively descend (if recurse is true) through the given directory and return a list of all files

Parameters:
files - The list of files to add to
dir - The directory to look at
recurse - Do we recurse
filter - If non-null then use this to find files
Returns:
List of files

walkDirectory

public static boolean walkDirectory(File dir,
                                    IOUtil.FileViewer fileViewer)
                             throws Exception
Walk the dir tree with the given file viewer

Parameters:
dir - dir
fileViewer - viewer
Returns:
should continue
Throws:
Exception - on badness_

walkDirectory

public static boolean walkDirectory(File dir,
                                    IOUtil.FileViewer fileViewer,
                                    int level)
                             throws Exception
Walk the dir tree with the given file viewer

Parameters:
dir - dir
fileViewer - viewer
level - tree depth
Returns:
should continue
Throws:
Exception - on badness_

getDirectories

public static List getDirectories(File dir,
                                  boolean recurse)
Recursively descend (if recurse is true) through the given directory and return a list of all subdirectories.

Parameters:
dir - The directory to look at
recurse - Do we recurse
Returns:
List of subdirs (File)

getDirectories

public static List getDirectories(List dirs,
                                  boolean recurse)
Recursively descend (if recurse is true) through the given directories and return a list of all subdirectories.

Parameters:
dirs - List of directories to look at
recurse - Do we recurse
Returns:
List of subdirs (File)

pruneIfEmpty

public static void pruneIfEmpty(File root)
This will recursively prune empty subdirectories of the given root directory. It will delete the given root directory if it is empty.

Parameters:
root - Directory to prune

pruneEmptyDirectories

public static void pruneEmptyDirectories(File root)
This will recursively prune empty subdirectories of the given root directory. It will NOT delete the given root directory if it is empty.

Parameters:
root - Directory to prune

deleteDirectory

public static void deleteDirectory(File root)
This will recursively delete all contents under the given directory.

Parameters:
root - Directory to delete

wait

public static void wait(File directory,
                        String filePattern,
                        double sleepSeconds)
Wait until there are new files in the given directory. If filePattern is non null then use that (regular expression) to match the files.

Parameters:
directory - The directory
filePattern - The pattern
sleepSeconds - Sleep this number of seconds between each check

wait

public static void wait(List files,
                        double sleepSeconds)
Wait until one or more of the files in the files list (File) has changed. If the files list is null or empty then just return.

Parameters:
files - List of File-s
sleepSeconds - Seconds to sleep between checks.

main

public static void main(String[] args)
                 throws Exception
test main

Parameters:
args - cmd line args
Throws:
Exception - On badness

writeJarFile

public static void writeJarFile(String filename,
                                List files)
                         throws IOException
Write out the list of files to the jar file specified by filename

Parameters:
filename - jar file name
files - list of files
Throws:
IOException - On badness

writeJarFile

public static void writeJarFile(String filename,
                                List files,
                                String pathPrefix)
                         throws IOException
Write out the list of files to the jar file specified by filename

Parameters:
filename - jar file name
files - list of files
pathPrefix - If not null this is the prefx we add to the jar entry
Throws:
IOException - On badness

writeJarFile

public static void writeJarFile(String filename,
                                List files,
                                String pathPrefix,
                                boolean makeFilesUnique)
                         throws IOException
Write out the list of files to the jar file specified by filename

Parameters:
filename - jar file name
files - list of files
pathPrefix - If not null this is the prefx we add to the jar entry
makeFilesUnique - If true then make all of the file names unique in case of conflict
Throws:
IOException - On badness