Class WWIO
- java.lang.Object
-
- gov.nasa.worldwind.util.WWIO
-
public class WWIO extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.StringDEFAULT_CHARACTER_ENCODINGThe default character encoding used if none is specified.static java.lang.StringDELETE_ON_EXIT_PREFIXstatic java.lang.StringILLEGAL_FILE_PATH_PART_CHARACTERSstatic intMAX_FILE_PATH_LENGTHThe maximum number of characters allowed in a file path.protected static java.util.Map<java.lang.String,java.lang.String>mimeTypeToSuffixMapprotected static java.util.Map<java.lang.String,java.lang.String>suffixToMimeTypeMap
-
Constructor Summary
Constructors Constructor Description WWIO()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringappendPathPart(java.lang.String firstPart, java.lang.String secondPart)static java.lang.StringbyteBufferToString(java.nio.ByteBuffer buffer, int length, java.lang.String encoding)Create aStringof limited size from aByteBuffer.static java.lang.StringbyteBufferToString(java.nio.ByteBuffer buffer, java.lang.String encoding)Create aStringfrom aByteBuffer.static voidcloseStream(java.lang.Object stream, java.lang.String name)Close a stream and catch anyIOExceptiongenerated in the process.static java.net.ProxyconfigureProxy()static java.io.FileconvertURIToFile(java.net.URI uri)Converts a specified URI as to a path in the local file system.static java.io.FileconvertURLToFile(java.net.URL url)Converts a specified URL as to a path in the local file system.static voidcopyDirectory(java.io.File source, java.io.File destination, boolean copySubDirectories)static voidcopyFile(java.io.File source, java.io.File destination)static booleandeflateBufferToFile(java.nio.ByteBuffer buffer, java.io.File file)static voiddeleteDirectory(java.io.File file)static java.lang.StringformPath(java.lang.String... pathParts)static java.io.BufferedInputStreamgetBufferedInputStream(java.io.InputStream is)Returns a newBufferedInputStreamwhich wraps the specified InputStream.static java.io.FilegetFileForLocalAddress(java.lang.Object src)Converts several types of addresses to a local file to aFile.static java.lang.StringgetFilename(java.lang.String filePath)Returns the name of the file or directory denoted by the specified path.static java.lang.ObjectgetFileOrResourceAsStream(java.lang.String path, java.lang.Class c)static java.io.InputStreamgetInputStreamFromByteBuffer(java.nio.ByteBuffer buffer)Creates anInputStreamfor the contents of aByteBuffer.static java.io.InputStreamgetInputStreamFromString(java.lang.String string)Creates anInputStreamfor the contents of aString.static java.io.InputStreamgetInputStreamFromString(java.lang.String string, java.lang.String encoding)Creates anInputStreamfor the contents of aString.static java.lang.StringgetParentFilePath(java.lang.String filePath)Returns the file path's parent directory path, or null if the file path does not have a parent.static java.lang.StringgetSourcePath(java.lang.Object src)Returns the specified input source's abstract path, or null if the input source has no path.static java.lang.StringgetSuffix(java.lang.String filePath)static java.nio.ByteBufferinflateFileToBuffer(java.io.File file)static java.nio.ByteBufferinflateStreamToBuffer(java.io.InputStream inputStream)static booleanisAncestorOf(java.io.File file, java.io.File ancestor)static booleanisContentType(java.io.File file, java.lang.String... mimeTypes)Indicates whether aFilecontains content of a specified mime type.static booleanisFileOutOfDate(java.net.URL url, long expiryTime)static booleanisLocalJarAddress(java.net.URL jarUrl)Determines whether a jar URL is a reference to a local jar file or an entry in a local jar file.static java.lang.String[]listChildFilenames(java.io.File file, java.io.FileFilter filter)Returns an array of strings naming the files and directories in the directory denoted by the specified file, that satisfy the specified filter.static java.lang.String[]listDescendantFilenames(java.io.File file, java.io.FileFilter filter)Returns an array of relative file paths naming the files and directories in the directory tree rooted by the specified file, that satisfy the specified filter.static java.lang.String[]listDescendantFilenames(java.io.File file, java.io.FileFilter filter, boolean recurseAfterMatch)Returns an array of relative file paths naming the files and directories in the directory tree rooted by the specified file, that satisfy the specified filter.protected static voidlistDescendantFilenames(java.io.File parent, java.lang.String pathname, java.io.FileFilter filter, boolean recurseAfterMatch, java.util.Collection<java.lang.String> matches)static java.lang.String[]makeCachePathForURL(java.net.URL url)static java.lang.StringmakeDataTypeForMimeType(java.lang.String mimeType)Returns the data type constant corresponding to the specified mime type string.static java.lang.StringmakeMimeTypeForSuffix(java.lang.String suffix)Returns the mime type string corresponding to the specified file suffix string.static booleanmakeParentDirs(java.lang.String path)Ensure that all directories leading the element at the end of a file path exist.static java.lang.StringmakeSuffixForMimeType(java.lang.String mimeType)Returns the file suffix string corresponding to the specified mime type string.static java.io.FilemakeTempDir()Create a directory in the computer's temp directory.static java.net.URImakeURI(java.lang.Object path)Creates a URI from an object.static java.net.URLmakeURL(java.lang.Object path)Creates a URL from an object.static java.net.URLmakeURL(java.lang.Object path, java.lang.String defaultProtocol)Creates a URL from an object.static java.net.URLmakeURL(java.lang.String path)Converts a string to a URL.static java.nio.MappedByteBuffermapFile(java.io.File file)Maps the specified File's bytes directly into memory as aMappedByteBuffer.static java.nio.MappedByteBuffermapFile(java.io.File file, java.nio.channels.FileChannel.MapMode mode)Maps the specified File's bytes directly into memory as aMappedByteBufferaccording to the specified mode.static java.io.InputStreamopenFileOrResourceStream(java.lang.String fileName, java.lang.Class c)Opens a file located via an absolute path or a path relative to the classpath.static java.io.ReaderopenReader(java.lang.Object src)Open a reader on an input source.static java.io.InputStreamopenStream(java.lang.Object src)Open anInputStreamfrom a general source.static java.nio.ByteBufferreadChannelToBuffer(java.nio.channels.ReadableByteChannel channel, boolean allocateDirect)Reads all the available bytes from the specifiedReadableByteChannel, returning the bytes as aByteBufferwith the current JVM byte order.static java.nio.ByteBufferreadChannelToBuffer(java.nio.channels.ReadableByteChannel channel, java.nio.ByteBuffer buffer)Reads the available bytes from the specifiedReadableByteChannelup to the number of bytes remaining in the buffer.static java.lang.StringreadChannelToString(java.nio.channels.ReadableByteChannel channel, java.lang.String encoding)Reads all the available bytes from the specifiedReadableByteChannel, returning the bytes as a String.static java.lang.StringreadCharacterStreamToString(java.io.Reader reader)Reads all the character stream content from the specifiedReader, returning a the accumulated content as a String .static java.nio.ByteBufferreadFileToBuffer(java.io.File file)Reads all the bytes from the specifiedFile, returning the bytes as a non-directByteBufferwith the current JVM byte order.static java.nio.ByteBufferreadFileToBuffer(java.io.File file, boolean allocateDirect)Reads all the bytes from the specifiedFile, returning the bytes as aByteBufferwith the current JVM byte order.static java.nio.ByteBufferreadGZipFileToBuffer(java.io.File gzFile)static java.nio.ByteBufferreadStreamToBuffer(java.io.InputStream inputStream)Reads all the available bytes from the specifiedInputStream, returning the bytes as a non-directByteBufferwith the current JVM byte order.static java.nio.ByteBufferreadStreamToBuffer(java.io.InputStream inputStream, boolean allocateDirect)Reads all the available bytes from the specifiedInputStream, returning the bytes as aByteBufferwith the current JVM byte order.static java.lang.StringreadStreamToString(java.io.InputStream stream, java.lang.String encoding)Reads all the available bytes from the specifiedInputStream, returning the bytes as a String.static java.lang.StringreadTextFile(java.io.File file)Open and read a text file intoString.static java.nio.ByteBufferreadURLContentToBuffer(java.net.URL url)Reads all the bytes from the specifiedURL, returning the bytes as a non-directByteBufferwith the current JVM byte order.static java.nio.ByteBufferreadURLContentToBuffer(java.net.URL url, boolean allocateDirect)Reads all the bytes from the specifiedURL, returning the bytes as aByteBufferwith the current JVM byte order.static java.lang.StringreadURLContentToString(java.net.URL url, java.lang.String encoding)Reads all the bytes from the specified, returning the bytes as a String.URLstatic java.nio.ByteBufferreadZipEntryToBuffer(java.io.File zipFile, java.lang.String entryName)static java.lang.StringreplaceIllegalFileNameCharacters(java.lang.String s)Replaces any illegal filename characters in a specified string with an underscore, "_".static java.lang.StringreplaceSuffix(java.lang.String in, java.lang.String newSuffix)static voidreverseFloatArray(int pos, int count, float[] array)static booleansaveBuffer(java.nio.ByteBuffer buffer, java.io.File file)static booleansaveBuffer(java.nio.ByteBuffer buffer, java.io.File file, boolean forceFilesystemWrite)static booleansaveBufferToGZipFile(java.nio.ByteBuffer buffer, java.io.File file)static booleansaveBufferToStream(java.nio.ByteBuffer buffer, java.io.OutputStream fos)static java.io.FilesaveBufferToTempFile(java.nio.ByteBuffer buffer, java.lang.String suffix)static voidskipBytes(java.io.InputStream is, int numBytes)Skip over a specified number of bytes in an input stream.static java.nio.ByteBufferstringToByteBuffer(java.lang.String string, java.lang.String encoding)Create aByteBufferfrom aString.static java.lang.StringstripLeadingSeparator(java.lang.String s)static java.lang.StringstripLeadingZeros(java.lang.String s)static java.lang.StringstripTrailingSeparator(java.lang.String s)static voidwriteTextFile(java.lang.String text, java.io.File file)Save aStringto a text file.
-
-
-
Field Detail
-
DELETE_ON_EXIT_PREFIX
public static final java.lang.String DELETE_ON_EXIT_PREFIX
- See Also:
- Constant Field Values
-
ILLEGAL_FILE_PATH_PART_CHARACTERS
public static final java.lang.String ILLEGAL_FILE_PATH_PART_CHARACTERS
- See Also:
- Constant Field Values
-
DEFAULT_CHARACTER_ENCODING
protected static final java.lang.String DEFAULT_CHARACTER_ENCODING
The default character encoding used if none is specified.- See Also:
- Constant Field Values
-
MAX_FILE_PATH_LENGTH
public static final int MAX_FILE_PATH_LENGTH
The maximum number of characters allowed in a file path. Covers Windows, Linux and OS X.- See Also:
- Constant Field Values
-
mimeTypeToSuffixMap
protected static java.util.Map<java.lang.String,java.lang.String> mimeTypeToSuffixMap
-
suffixToMimeTypeMap
protected static java.util.Map<java.lang.String,java.lang.String> suffixToMimeTypeMap
-
-
Method Detail
-
formPath
public static java.lang.String formPath(java.lang.String... pathParts)
-
appendPathPart
public static java.lang.String appendPathPart(java.lang.String firstPart, java.lang.String secondPart)
-
replaceIllegalFileNameCharacters
public static java.lang.String replaceIllegalFileNameCharacters(java.lang.String s)
Replaces any illegal filename characters in a specified string with an underscore, "_".- Parameters:
s- the string to examine.- Returns:
- a new string with illegal filename characters replaced.
- Throws:
java.lang.IllegalArgumentException- if the specified string is null.
-
stripTrailingSeparator
public static java.lang.String stripTrailingSeparator(java.lang.String s)
-
stripLeadingSeparator
public static java.lang.String stripLeadingSeparator(java.lang.String s)
-
stripLeadingZeros
public static java.lang.String stripLeadingZeros(java.lang.String s)
-
getFileForLocalAddress
public static java.io.File getFileForLocalAddress(java.lang.Object src)
Converts several types of addresses to a local file to aFile. Returns null if the source cannot be converted to a file. The source type may be one of the following:URLURIFileStringcontaining a valid URL description, a valid URI description, or a valid path to a local file.
- Parameters:
src- the source to convert to local file path.- Returns:
- a local File path, or null if the source could not be converted.
- Throws:
java.lang.IllegalArgumentException- if the source is null or an empty string.
-
convertURIToFile
public static java.io.File convertURIToFile(java.net.URI uri)
Converts a specified URI as to a path in the local file system. If the URI cannot be converted to a file path for any reason, this returns null.- Parameters:
uri- the URI to convert to a local file path.- Returns:
- a local File path, or null if the URI could not be converted.
- Throws:
java.lang.IllegalArgumentException- if the url is null.
-
convertURLToFile
public static java.io.File convertURLToFile(java.net.URL url)
Converts a specified URL as to a path in the local file system. If the URL cannot be converted to a file path for any reason, this returns null.- Parameters:
url- the URL to convert to a local file path.- Returns:
- a local File path, or null if the URL could not be converted.
- Throws:
java.lang.IllegalArgumentException- if the url is null.
-
saveBuffer
public static boolean saveBuffer(java.nio.ByteBuffer buffer, java.io.File file, boolean forceFilesystemWrite) throws java.io.IOException- Throws:
java.io.IOException
-
saveBuffer
public static boolean saveBuffer(java.nio.ByteBuffer buffer, java.io.File file) throws java.io.IOException- Throws:
java.io.IOException
-
saveBufferToStream
public static boolean saveBufferToStream(java.nio.ByteBuffer buffer, java.io.OutputStream fos) throws java.io.IOException- Throws:
java.io.IOException
-
mapFile
public static java.nio.MappedByteBuffer mapFile(java.io.File file, java.nio.channels.FileChannel.MapMode mode) throws java.io.IOExceptionMaps the specified File's bytes directly into memory as aMappedByteBufferaccording to the specified mode.If the mode is
FileChannel.MapMode.READ_ONLY, the file is mapped in read-only mode, and any attempt to modify the contents of the returned MappedByteBuffer causes aReadOnlyBufferException.If the mode is
FileChannel.MapMode.READ_WRITE, the file is mapped in read-write mode. Changing the contents of the returned MappedByteBuffer to be eventually propagated to the file. The specified file must be avialable for both reading and writing.If the mode is
FileChannel.MapMode.PRIVATE, the file is mapped in copy-on-write mode. Changing the contents of the returned MappedByteBuffer causes private copies of portions of the buffer to be created. The specified file must be avialable for both reading and writing.- Parameters:
file- the file to map.mode- the mapping mode, one ofFileChannel.MapMode.READ_ONLY,FileChannel.MapMode.READ_WRITE, orFileChannel.MapMode.PRIVATE.- Returns:
- a MappedByteBuffer representing the File's bytes.
- Throws:
java.io.IOException- if the file cannot be mapped for any reason.
-
mapFile
public static java.nio.MappedByteBuffer mapFile(java.io.File file) throws java.io.IOExceptionMaps the specified File's bytes directly into memory as aMappedByteBuffer. The file is mapped in read-only mode; any attempt to modify the contents of the returned MappedByteBuffer causes aReadOnlyBufferException.- Parameters:
file- the file to map.- Returns:
- a read-only MappedByteBuffer representing the File's bytes.
- Throws:
java.io.IOException- if the file cannot be mapped for any reason.
-
readURLContentToBuffer
public static java.nio.ByteBuffer readURLContentToBuffer(java.net.URL url) throws java.io.IOExceptionReads all the bytes from the specifiedURL, returning the bytes as a non-directByteBufferwith the current JVM byte order. Non-direct buffers are backed by JVM heap memory.- Parameters:
url- the URL to read.- Returns:
- the bytes from the specified URL, with the current JVM byte order.
- Throws:
java.lang.IllegalArgumentException- if the URL is null.java.io.IOException- if an I/O error occurs.
-
readURLContentToBuffer
public static java.nio.ByteBuffer readURLContentToBuffer(java.net.URL url, boolean allocateDirect) throws java.io.IOExceptionReads all the bytes from the specifiedURL, returning the bytes as aByteBufferwith the current JVM byte order. This returns a direct ByteBuffer if allocateDirect is true, and returns a non-direct ByteBuffer otherwise. Direct buffers are backed by native memory, and may resite outside of the normal garbage-collected heap. Non-direct buffers are backed by JVM heap memory.- Parameters:
url- the URL to read.allocateDirect- true to allocate and return a direct buffer, false to allocate and return a non-direct buffer.- Returns:
- the bytes from the specified URL, with the current JVM byte order.
- Throws:
java.lang.IllegalArgumentException- if the URL is null.java.io.IOException- if an I/O error occurs.
-
readURLContentToString
public static java.lang.String readURLContentToString(java.net.URL url, java.lang.String encoding) throws java.io.IOExceptionReads all the bytes from the specified, returning the bytes as a String. The bytes are interpreted according to the specified encoding, or UTF-8 if no encoding is specified.URL- Parameters:
url- the URL to read.encoding- the encoding do use. Ifnullis specified then UTF-8 is used.- Returns:
- the string representation of the bytes at the
URLdecoded according to the specified encoding. - Throws:
java.lang.IllegalArgumentException- if theurlis null.java.io.IOException- if an I/O error occurs.java.nio.charset.IllegalCharsetNameException- if the specified encoding name is illegal.java.nio.charset.UnsupportedCharsetException- if no support for the named encoding is available.
-
readFileToBuffer
public static java.nio.ByteBuffer readFileToBuffer(java.io.File file) throws java.io.IOExceptionReads all the bytes from the specifiedFile, returning the bytes as a non-directByteBufferwith the current JVM byte order. Non-direct buffers are backed by JVM heap memory.- Parameters:
file- the file to read.- Returns:
- the bytes from the specified file, with the current JVM byte order.
- Throws:
java.lang.IllegalArgumentException- if the file is null.java.io.IOException- if an I/O error occurs.
-
readFileToBuffer
public static java.nio.ByteBuffer readFileToBuffer(java.io.File file, boolean allocateDirect) throws java.io.IOExceptionReads all the bytes from the specifiedFile, returning the bytes as aByteBufferwith the current JVM byte order. This returns a direct ByteBuffer if allocateDirect is true, and returns a non-direct ByteBuffer otherwise. Direct buffers are backed by native memory, and may reside outside of the normal garbage-collected heap. Non-direct buffers are backed by JVM heap memory.- Parameters:
file- the file to read.allocateDirect- true to allocate and return a direct buffer, false to allocate and return a non-direct buffer.- Returns:
- the bytes from the specified file, with the current JVM byte order.
- Throws:
java.lang.IllegalArgumentException- if the file is null.java.io.IOException- if an I/O error occurs.
-
inflateFileToBuffer
public static java.nio.ByteBuffer inflateFileToBuffer(java.io.File file) throws java.io.IOException- Throws:
java.io.IOException
-
saveBufferToGZipFile
public static boolean saveBufferToGZipFile(java.nio.ByteBuffer buffer, java.io.File file) throws java.io.IOException- Throws:
java.io.IOException
-
deflateBufferToFile
public static boolean deflateBufferToFile(java.nio.ByteBuffer buffer, java.io.File file) throws java.io.IOException- Throws:
java.io.IOException
-
readGZipFileToBuffer
public static java.nio.ByteBuffer readGZipFileToBuffer(java.io.File gzFile) throws java.lang.IllegalArgumentException, java.io.IOException- Throws:
java.lang.IllegalArgumentExceptionjava.io.IOException
-
readZipEntryToBuffer
public static java.nio.ByteBuffer readZipEntryToBuffer(java.io.File zipFile, java.lang.String entryName) throws java.io.IOException- Throws:
java.io.IOException
-
readStreamToBuffer
public static java.nio.ByteBuffer readStreamToBuffer(java.io.InputStream inputStream) throws java.io.IOExceptionReads all the available bytes from the specifiedInputStream, returning the bytes as a non-directByteBufferwith the current JVM byte order. Non-direct buffers are backed by JVM heap memory.- Parameters:
inputStream- the stream to read.- Returns:
- the bytes from the specified stream, with the current JVM byte order.
- Throws:
java.lang.IllegalArgumentException- if the stream is null.java.io.IOException- if an I/O error occurs.
-
readStreamToBuffer
public static java.nio.ByteBuffer readStreamToBuffer(java.io.InputStream inputStream, boolean allocateDirect) throws java.io.IOExceptionReads all the available bytes from the specifiedInputStream, returning the bytes as aByteBufferwith the current JVM byte order. This returns a direct ByteBuffer if allocateDirect is true, and returns a non-direct ByteBuffer otherwise. Direct buffers are backed by native memory, and may reside outside of the normal garbage-collected heap. Non-direct buffers are backed by JVM heap memory.- Parameters:
inputStream- the stream to read.allocateDirect- true to allocate and return a direct buffer, false to allocate and return a non-direct buffer.- Returns:
- the bytes from the specified stream, with the current JVM byte order.
- Throws:
java.lang.IllegalArgumentException- if the stream is null.java.io.IOException- if an I/O error occurs.
-
readStreamToString
public static java.lang.String readStreamToString(java.io.InputStream stream, java.lang.String encoding) throws java.io.IOExceptionReads all the available bytes from the specifiedInputStream, returning the bytes as a String. The bytes are interpreted according to the specified encoding, or UTF-8 if no encoding is specified.- Parameters:
stream- the stream to read.encoding- the encoding do use. If null is specified then UTF-8 is used.- Returns:
- the string representation of the bytes in the stream decoded according to the specified encoding.
- Throws:
java.lang.IllegalArgumentException- if thestreamis null.java.io.IOException- if an I/O error occurs.
-
readChannelToBuffer
public static java.nio.ByteBuffer readChannelToBuffer(java.nio.channels.ReadableByteChannel channel, boolean allocateDirect) throws java.io.IOExceptionReads all the available bytes from the specifiedReadableByteChannel, returning the bytes as aByteBufferwith the current JVM byte order. This returns a direct ByteBuffer if allocateDirect is true, and returns a non-direct ByteBuffer otherwise. Direct buffers are backed by native memory, and may reside outside of the normal garbage-collected heap. Non-direct buffers are backed by JVM heap memory.- Parameters:
channel- the channel to read.allocateDirect- true to allocate and return a direct buffer, false to allocate and return a non-direct buffer.- Returns:
- the bytes from the specified channel, with the current JVM byte order.
- Throws:
java.lang.IllegalArgumentException- if the channel is null.java.io.IOException- if an I/O error occurs.
-
readChannelToBuffer
public static java.nio.ByteBuffer readChannelToBuffer(java.nio.channels.ReadableByteChannel channel, java.nio.ByteBuffer buffer) throws java.io.IOExceptionReads the available bytes from the specifiedReadableByteChannelup to the number of bytes remaining in the buffer. Bytes read from the specified channel are copied to the specifiedByteBuffer. Upon returning the specified buffer's limit is set to the number of bytes read, and its position is set to zero.- Parameters:
channel- the channel to read bytes from.buffer- the buffer to receive the bytes.- Returns:
- the specified buffer.
- Throws:
java.lang.IllegalArgumentException- if the channel or the buffer is null.java.io.IOException- if an I/O error occurs.
-
readChannelToString
public static java.lang.String readChannelToString(java.nio.channels.ReadableByteChannel channel, java.lang.String encoding) throws java.io.IOExceptionReads all the available bytes from the specifiedReadableByteChannel, returning the bytes as a String. The bytes are interpreted according to the specified encoding, or UTF-8 if no encoding is specified.- Parameters:
channel- the channel to read.encoding- the encoding do use. If null is specified then UTF-8 is used.- Returns:
- the string representation of the bytes in the channel decoded according to the specified encoding.
- Throws:
java.lang.IllegalArgumentException- if thestreamis null.java.io.IOException- if an I/O error occurs.
-
readCharacterStreamToString
public static java.lang.String readCharacterStreamToString(java.io.Reader reader) throws java.io.IOExceptionReads all the character stream content from the specifiedReader, returning a the accumulated content as a String .- Parameters:
reader- the character stream to read.- Returns:
- the string representing the accumulated content from the character stream.
- Throws:
java.lang.IllegalArgumentException- if thereaderis null.java.io.IOException- if an I/O error occurs.
-
inflateStreamToBuffer
public static java.nio.ByteBuffer inflateStreamToBuffer(java.io.InputStream inputStream) throws java.io.IOException- Throws:
java.io.IOException
-
replaceSuffix
public static java.lang.String replaceSuffix(java.lang.String in, java.lang.String newSuffix)
-
getSuffix
public static java.lang.String getSuffix(java.lang.String filePath)
-
getFilename
public static java.lang.String getFilename(java.lang.String filePath)
Returns the name of the file or directory denoted by the specified path. This is the last file name in the path, or null if the path does not contain any file names.- Parameters:
filePath- a file path String.- Returns:
- the last name in the specified path, or null if the path does not contain a name.
- Throws:
java.lang.IllegalArgumentException- if the file path is null.
-
getParentFilePath
public static java.lang.String getParentFilePath(java.lang.String filePath)
Returns the file path's parent directory path, or null if the file path does not have a parent.- Parameters:
filePath- a file path String.- Returns:
- the file path's parent directory, or null if the path does not have a parent.
- Throws:
java.lang.IllegalArgumentException- if the file path is null.
-
makeParentDirs
public static boolean makeParentDirs(java.lang.String path)
Ensure that all directories leading the element at the end of a file path exist. Create any nonexistent directories in the path. A directory is not creared for the last element in the path; it's assumed to be a file name and is ignored.- Parameters:
path- the path whose directories are vefified to exist or be created. The last element of the path is ignored.- Returns:
- true if all the directories in the path exist or were created.
- Throws:
java.lang.IllegalArgumentException- if the path is null.
-
makeTempDir
public static java.io.File makeTempDir() throws java.io.IOExceptionCreate a directory in the computer's temp directory.- Returns:
- a file reference to the new directory, of null if a directory could not be created.
- Throws:
java.io.IOException- if a directory could not be created.java.lang.SecurityException- if a security manager exists and it does not allow directory creation.
-
saveBufferToTempFile
public static java.io.File saveBufferToTempFile(java.nio.ByteBuffer buffer, java.lang.String suffix) throws java.io.IOException- Throws:
java.io.IOException
-
isFileOutOfDate
public static boolean isFileOutOfDate(java.net.URL url, long expiryTime)
-
configureProxy
public static java.net.Proxy configureProxy()
-
isContentType
public static boolean isContentType(java.io.File file, java.lang.String... mimeTypes)Indicates whether aFilecontains content of a specified mime type.Only the filename suffix is consulted to determine the file's content type.
- Parameters:
file- the file to test.mimeTypes- the mime types to test for.- Returns:
- true if the file contains a specified content type, false if the file does not contain a specified content type, the specified file is null, or no content types are specified.
-
makeSuffixForMimeType
public static java.lang.String makeSuffixForMimeType(java.lang.String mimeType)
Returns the file suffix string corresponding to the specified mime type string. The returned suffix starts with the period character '.' followed by the mime type's subtype, as in: ".[subtype]".- Parameters:
mimeType- the mime type who's suffix is returned.- Returns:
- the file suffix for the specified mime type, with a leading ".".
- Throws:
java.lang.IllegalArgumentException- if the mime type is null or malformed.
-
makeMimeTypeForSuffix
public static java.lang.String makeMimeTypeForSuffix(java.lang.String suffix)
Returns the mime type string corresponding to the specified file suffix string.- Parameters:
suffix- the suffix who's mime type is returned.- Returns:
- the mime type for the specified file suffix.
- Throws:
java.lang.IllegalArgumentException- if the file suffix is null.
-
makeDataTypeForMimeType
public static java.lang.String makeDataTypeForMimeType(java.lang.String mimeType)
Returns the data type constant corresponding to the specified mime type string. Supported mime types are as mapped to data types as follows:Mapping Mime Type Data Type application/bil32 AVKey.FLOAT32application/bil16 AVKey.INT16application/bil AVKey.INT16image/bil AVKey.INT16- Parameters:
mimeType- the mime type who's data type is returned.- Returns:
- the data type for the specified mime type.
- Throws:
java.lang.IllegalArgumentException- if the mime type is null or malformed.
-
getFileOrResourceAsStream
public static java.lang.Object getFileOrResourceAsStream(java.lang.String path, java.lang.Class c)
-
getInputStreamFromString
public static java.io.InputStream getInputStreamFromString(java.lang.String string)
Creates anInputStreamfor the contents of aString. The method creates a copy of the string's contents and passes a steam reference to that copy.- Parameters:
string- the string to create a stream for, encoded in UTF-8.- Returns:
- an
InputStreamfor the string's contents. - Throws:
java.lang.IllegalArgumentException- ifstringis null.
-
getInputStreamFromString
public static java.io.InputStream getInputStreamFromString(java.lang.String string, java.lang.String encoding)Creates anInputStreamfor the contents of aString. The method creates a copy of the string's contents and passes a steam reference to that copy.- Parameters:
string- the string to create a stream for.encoding- the character encoding of the string. UTF-8 is used if null.- Returns:
- an
InputStreamfor the string's contents. - Throws:
java.lang.IllegalArgumentException- ifstringis null.
-
getInputStreamFromByteBuffer
public static java.io.InputStream getInputStreamFromByteBuffer(java.nio.ByteBuffer buffer)
Creates anInputStreamfor the contents of aByteBuffer. The method creates a copy of the buffer's contents and passes a steam reference to that copy.- Parameters:
buffer- the buffer to create a stream for.- Returns:
- an
InputStreamfor the buffer's contents. - Throws:
java.lang.IllegalArgumentException- ifbufferis null.
-
getBufferedInputStream
public static java.io.BufferedInputStream getBufferedInputStream(java.io.InputStream is)
Returns a newBufferedInputStreamwhich wraps the specified InputStream. If the specified InputStream is already a BufferedInputStream, this returns the original InputStream cast to a BufferedInputStream.- Parameters:
is- the InputStream to wrap with a new BufferedInputStream.- Returns:
- a new BufferedInputStream which wraps the specified InputStream.
-
isAncestorOf
public static boolean isAncestorOf(java.io.File file, java.io.File ancestor)
-
copyFile
public static void copyFile(java.io.File source, java.io.File destination) throws java.io.IOException- Throws:
java.io.IOException
-
copyDirectory
public static void copyDirectory(java.io.File source, java.io.File destination, boolean copySubDirectories) throws java.io.IOException- Throws:
java.io.IOException
-
deleteDirectory
public static void deleteDirectory(java.io.File file) throws java.io.IOException- Throws:
java.io.IOException
-
closeStream
public static void closeStream(java.lang.Object stream, java.lang.String name)Close a stream and catch anyIOExceptiongenerated in the process. This supports any object that implements theCloseableinterface.- Parameters:
stream- the stream to close. If null, this method does nothing.name- the name of the stream to place in the log message if an exception is encountered.
-
readTextFile
public static java.lang.String readTextFile(java.io.File file)
Open and read a text file intoString.- Parameters:
file- aFilereference to the file to open and read.- Returns:
- a
Stringcontaining the contents of the file. - Throws:
java.lang.IllegalArgumentException- if the file is null.
-
writeTextFile
public static void writeTextFile(java.lang.String text, java.io.File file)Save aStringto a text file.- Parameters:
text- theStringto write to the file.file- aFilereference to the file to create.- Throws:
java.lang.IllegalArgumentException- if the text string or file is null.
-
openFileOrResourceStream
public static java.io.InputStream openFileOrResourceStream(java.lang.String fileName, java.lang.Class c)Opens a file located via an absolute path or a path relative to the classpath.- Parameters:
fileName- the path of the file to open, either absolute or relative to the classpath.c- the class that will be used to find a path relative to the classpath.- Returns:
- an
InputStreamto the open file - Throws:
java.lang.IllegalArgumentException- if the file name is null.WWRuntimeException- if an exception occurs or the file can't be found. The causing exception is available via this exception'sThrowable.initCause(Throwable)method.
-
byteBufferToString
public static java.lang.String byteBufferToString(java.nio.ByteBuffer buffer, java.lang.String encoding)Create aStringfrom aByteBuffer.- Parameters:
buffer- the byte buffer to convert.encoding- the encoding do use. If null is specified then UTF-8 is used.- Returns:
- the string representation of the bytes in the buffer decoded according to the specified encoding.
- Throws:
java.lang.IllegalArgumentException- if the buffer is null.java.nio.charset.IllegalCharsetNameException- if the specified encoding name is illegal.java.nio.charset.UnsupportedCharsetException- if no support for the named encoding is available.
-
byteBufferToString
public static java.lang.String byteBufferToString(java.nio.ByteBuffer buffer, int length, java.lang.String encoding)Create aStringof limited size from aByteBuffer.- Parameters:
buffer- the byte buffer to convert.length- the maximum number of characters to read from the buffer. Must be greater than 0.encoding- the encoding do use. If null is specified then UTF-8 is used.- Returns:
- the string representation of the bytes in the buffer decoded according to the specified encoding.
- Throws:
java.lang.IllegalArgumentException- if the buffer is null or the length is less than 1.java.nio.charset.IllegalCharsetNameException- if the specified encoding name is illegal.java.nio.charset.UnsupportedCharsetException- if no support for the named encoding is available.
-
stringToByteBuffer
public static java.nio.ByteBuffer stringToByteBuffer(java.lang.String string, java.lang.String encoding) throws java.io.UnsupportedEncodingExceptionCreate aByteBufferfrom aString.- Parameters:
string- the string to convert.encoding- the encoding do use. If null is specified then UTF-8 is used.- Returns:
- the ByteBuffer representation of the string decoded according to the specified encoding.
- Throws:
java.io.UnsupportedEncodingException- if the specified encoding is not supported
-
openReader
public static java.io.Reader openReader(java.lang.Object src) throws java.io.IOExceptionOpen a reader on an input source. The source may be one of the following:ReaderInputStreamFileURLString
Readers are used to read character streams.
- Parameters:
src- the input source of one of the above types.- Returns:
- a reader for the input source.
- Throws:
java.io.IOException- if i/o or other errors occur trying to create the reader.
-
openStream
public static java.io.InputStream openStream(java.lang.Object src) throws java.lang.ExceptionOpen anInputStreamfrom a general source. The source type may be one of the following:InputStreamURL- absolute
URI FileStringcontaining a valid URL description or a file or resource name available on the classpath.
- Parameters:
src- the input source of one of the above types.- Returns:
- an InputStream for the input source.
- Throws:
java.lang.IllegalArgumentException- if the source is null, an empty string, or is not one of the above types.java.lang.Exception- if the source cannot be opened for any reason.
-
getSourcePath
public static java.lang.String getSourcePath(java.lang.Object src)
Returns the specified input source's abstract path, or null if the input source has no path. The input source may be one of the following:StringFileURLURI
- Parameters:
src- the input source of one of the above types.- Returns:
- the input source's abstract path, or null if none exists.
- Throws:
java.lang.IllegalArgumentException- if the source is null.
-
makeURL
public static java.net.URL makeURL(java.lang.String path)
Converts a string to a URL.- Parameters:
path- the string to convert to a URL.- Returns:
- a URL for the specified object, or null if a URL could not be created.
- See Also:
makeURL(Object),makeURL(Object, String)
-
makeURL
public static java.net.URL makeURL(java.lang.Object path)
Creates a URL from an object.- Parameters:
path- the object from which to create a URL, typically a string.- Returns:
- a URL for the specified object, or null if a URL could not be created.
- See Also:
makeURL(String),makeURL(Object, String)
-
makeURL
public static java.net.URL makeURL(java.lang.Object path, java.lang.String defaultProtocol)Creates a URL from an object. If the object does not already convert directly to a URL, a URL with a specified protocol is created.- Parameters:
path- the object from which to create a URL, typically a string.defaultProtocol- if non-null, a protocol to use if the specified path does not yet include a protocol.- Returns:
- a URL for the specified object, or null if a URL could not be created.
- See Also:
makeURL(String),makeURL(Object)
-
makeURI
public static java.net.URI makeURI(java.lang.Object path)
Creates a URI from an object.- Parameters:
path- the object from which to create a URI, typically a string.- Returns:
- a URI for the specified object, or null if a URI could not be created.
- See Also:
makeURL(String),makeURL(Object),makeURL(Object, String)
-
listChildFilenames
public static java.lang.String[] listChildFilenames(java.io.File file, java.io.FileFilter filter)Returns an array of strings naming the files and directories in the directory denoted by the specified file, that satisfy the specified filter. If the filter is null, then all files and directories are accepted. This returns null if the specified file is not a directory.- Parameters:
file- the directory who's contents to list.filter- a file filter.- Returns:
- an array of file names denoting the files and directories in the directory denoted by the specified file, or null if the specified file is not a directory.
- Throws:
java.lang.IllegalArgumentException- if the file is null.
-
listDescendantFilenames
public static java.lang.String[] listDescendantFilenames(java.io.File file, java.io.FileFilter filter)Returns an array of relative file paths naming the files and directories in the directory tree rooted by the specified file, that satisfy the specified filter. The returned paths are relative to the specified file. If the filter is null, then all files and directories are accepted. This returns null if the specified file is not a directory.- Parameters:
file- the directory tree who's contents to list.filter- a file filter.- Returns:
- an array of relative file paths naming the files and directories in the directory tree rooted by the specified file, or null if the specified file is not a directory.
- Throws:
java.lang.IllegalArgumentException- if the file is null.
-
listDescendantFilenames
public static java.lang.String[] listDescendantFilenames(java.io.File file, java.io.FileFilter filter, boolean recurseAfterMatch)Returns an array of relative file paths naming the files and directories in the directory tree rooted by the specified file, that satisfy the specified filter. If the parameterrecurseAfterMatchis false, then this ignores any directory branches beneath a matched file. This has the effect of listing the top matches in the directory tree. The returned paths are relative to the specified file. If the filter is null, then all files and directories are accepted. This returns null if the specified file is not a directory.- Parameters:
file- the directory tree who's contents to list.filter- a file filter.recurseAfterMatch- true to list the contents of directory branches beneath a match; false to ignore branches beneath a match.- Returns:
- an array of relative file paths naming the files and directories in the directory tree rooted by the specified file, or null if the specified file is not a directory.
- Throws:
java.lang.IllegalArgumentException- if the file is null.
-
listDescendantFilenames
protected static void listDescendantFilenames(java.io.File parent, java.lang.String pathname, java.io.FileFilter filter, boolean recurseAfterMatch, java.util.Collection<java.lang.String> matches)
-
skipBytes
public static void skipBytes(java.io.InputStream is, int numBytes) throws java.io.IOExceptionSkip over a specified number of bytes in an input stream.- Parameters:
is- the input stream.numBytes- the number of bytes to skip over.- Throws:
java.lang.IllegalArgumentException- if the specified input stream is null.java.io.IOException- is an exception occurs while skipping the bytes.
-
makeCachePathForURL
public static java.lang.String[] makeCachePathForURL(java.net.URL url)
-
reverseFloatArray
public static void reverseFloatArray(int pos, int count, float[] array)
-
isLocalJarAddress
public static boolean isLocalJarAddress(java.net.URL jarUrl)
Determines whether a jar URL is a reference to a local jar file or an entry in a local jar file. SeeJarURLConnectionfor a description of jar URLs.- Parameters:
jarUrl- the jar URL, in the form jar:[url]!{entry}. (Omit [, ], { and } in the actual URL- Returns:
- true if the URL refers to a local resource, otherwise false.
-
-