NASA World Wind

gov.nasa.worldwind.util
Class WWIO

java.lang.Object
  extended by gov.nasa.worldwind.util.WWIO

public class WWIO
extends Object


Field Summary
protected static String DEFAULT_CHARACTER_ENCODING
          The default character encoding used if none is specified.
static String DELETE_ON_EXIT_PREFIX
           
static String ILLEGAL_FILE_PATH_PART_CHARACTERS
           
static int MAX_FILE_PATH_LENGTH
          The maximum number of characters allowed in a file path.
protected static Map<String,String> mimeTypeToSuffixMap
           
protected static Map<String,String> suffixToMimeTypeMap
           
 
Constructor Summary
WWIO()
           
 
Method Summary
static String appendPathPart(String firstPart, String secondPart)
           
static String byteBufferToString(ByteBuffer buffer, int length, String encoding)
          Create a String of limited size from a ByteBuffer.
static String byteBufferToString(ByteBuffer buffer, String encoding)
          Create a String from a ByteBuffer.
static void closeStream(Object stream, String name)
          Close a stream and catch any IOException generated in the process.
static Proxy configureProxy()
           
static File convertURIToFile(URI uri)
          Converts a specified URI as to a path in the local file system.
static File convertURLToFile(URL url)
          Converts a specified URL as to a path in the local file system.
static void copyDirectory(File source, File destination, boolean copySubDirectories)
           
static void copyFile(File source, File destination)
           
static boolean deflateBufferToFile(ByteBuffer buffer, File file)
           
static void deleteDirectory(File file)
           
static String formPath(String... pathParts)
           
static BufferedInputStream getBufferedInputStream(InputStream is)
          Returns a new BufferedInputStream which wraps the specified InputStream.
static File getFileForLocalAddress(Object src)
          Converts several types of addresses to a local file to a File.
static String getFilename(String filePath)
          Returns the name of the file or directory denoted by the specified path.
static Object getFileOrResourceAsStream(String path, Class c)
           
static InputStream getInputStreamFromByteBuffer(ByteBuffer buffer)
          Creates an InputStream for the contents of a ByteBuffer.
static InputStream getInputStreamFromString(String string)
          Creates an InputStream for the contents of a String.
static InputStream getInputStreamFromString(String string, String encoding)
          Creates an InputStream for the contents of a String.
static String getParentFilePath(String filePath)
          Returns the file path's parent directory path, or null if the file path does not have a parent.
static String getSourcePath(Object src)
          Returns the specified input source's abstract path, or null if the input source has no path.
static String getSuffix(String filePath)
           
static ByteBuffer inflateFileToBuffer(File file)
           
static ByteBuffer inflateStreamToBuffer(InputStream inputStream)
           
static boolean isAncestorOf(File file, File ancestor)
           
static boolean isContentType(File file, String... mimeTypes)
          Indicates whether a File contains content of a specified mime type.
static boolean isFileOutOfDate(URL url, long expiryTime)
           
static boolean isLocalJarAddress(URL jarUrl)
          Determines whether a jar URL is a reference to a local jar file or an entry in a local jar file.
static String[] listChildFilenames(File file, 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 String[] listDescendantFilenames(File file, 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 String[] listDescendantFilenames(File file, 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 void listDescendantFilenames(File parent, String pathname, FileFilter filter, boolean recurseAfterMatch, Collection<String> matches)
           
static String[] makeCachePathForURL(URL url)
           
static String makeDataTypeForMimeType(String mimeType)
          Returns the data type constant corresponding to the specified mime type string.
static String makeMimeTypeForSuffix(String suffix)
          Returns the mime type string corresponding to the specified file suffix string.
static boolean makeParentDirs(String path)
          Ensure that all directories leading the element at the end of a file path exist.
static String makeSuffixForMimeType(String mimeType)
          Returns the file suffix string corresponding to the specified mime type string.
static File makeTempDir()
          Create a directory in the computer's temp directory.
static URI makeURI(Object path)
          Creates a URI from an object.
static URL makeURL(Object path)
          Creates a URL from an object.
static URL makeURL(Object path, String defaultProtocol)
          Creates a URL from an object.
static URL makeURL(String path)
          Converts a string to a URL.
static MappedByteBuffer mapFile(File file)
          Maps the specified File's bytes directly into memory as a MappedByteBuffer.
static MappedByteBuffer mapFile(File file, FileChannel.MapMode mode)
          Maps the specified File's bytes directly into memory as a MappedByteBuffer according to the specified mode.
static InputStream openFileOrResourceStream(String fileName, Class c)
          Opens a file located via an absolute path or a path relative to the classpath.
static Reader openReader(Object src)
          Open a reader on an input source.
static InputStream openStream(Object src)
          Open an InputStream from a general source.
static ByteBuffer readChannelToBuffer(ReadableByteChannel channel, boolean allocateDirect)
          Reads all the available bytes from the specified ReadableByteChannel, returning the bytes as a ByteBuffer with the current JVM byte order.
static ByteBuffer readChannelToBuffer(ReadableByteChannel channel, ByteBuffer buffer)
          Reads the available bytes from the specified ReadableByteChannel up to the number of bytes remaining in the buffer.
static String readChannelToString(ReadableByteChannel channel, String encoding)
          Reads all the available bytes from the specified ReadableByteChannel, returning the bytes as a String.
static String readCharacterStreamToString(Reader reader)
          Reads all the character stream content from the specified Reader, returning a the accumulated content as a String .
static ByteBuffer readFileToBuffer(File file)
          Reads all the bytes from the specified File, returning the bytes as a non-direct ByteBuffer with the current JVM byte order.
static ByteBuffer readFileToBuffer(File file, boolean allocateDirect)
          Reads all the bytes from the specified File, returning the bytes as a ByteBuffer with the current JVM byte order.
static ByteBuffer readGZipFileToBuffer(File gzFile)
           
static ByteBuffer readStreamToBuffer(InputStream inputStream)
          Reads all the available bytes from the specified InputStream, returning the bytes as a non-direct ByteBuffer with the current JVM byte order.
static ByteBuffer readStreamToBuffer(InputStream inputStream, boolean allocateDirect)
          Reads all the available bytes from the specified InputStream, returning the bytes as a ByteBuffer with the current JVM byte order.
static String readStreamToString(InputStream stream, String encoding)
          Reads all the available bytes from the specified InputStream, returning the bytes as a String.
static String readTextFile(File file)
          Open and read a text file into String.
static ByteBuffer readURLContentToBuffer(URL url)
          Reads all the bytes from the specified URL, returning the bytes as a non-direct ByteBuffer with the current JVM byte order.
static ByteBuffer readURLContentToBuffer(URL url, boolean allocateDirect)
          Reads all the bytes from the specified URL, returning the bytes as a ByteBuffer with the current JVM byte order.
static String readURLContentToString(URL url, String encoding)
          Reads all the bytes from the specified URL, returning the bytes as a String.
static ByteBuffer readZipEntryToBuffer(File zipFile, String entryName)
           
static String replaceIllegalFileNameCharacters(String s)
          Replaces any illegal filename characters in a specified string with an underscore, "_".
static String replaceSuffix(String in, String newSuffix)
           
static void reverseFloatArray(int pos, int count, float[] array)
           
static boolean saveBuffer(ByteBuffer buffer, File file)
           
static boolean saveBuffer(ByteBuffer buffer, File file, boolean forceFilesystemWrite)
           
static boolean saveBufferToGZipFile(ByteBuffer buffer, File file)
           
static boolean saveBufferToStream(ByteBuffer buffer, OutputStream fos)
           
static File saveBufferToTempFile(ByteBuffer buffer, String suffix)
           
static void skipBytes(InputStream is, int numBytes)
          Skip over a specified number of bytes in an input stream.
static ByteBuffer stringToByteBuffer(String string, String encoding)
          Create a ByteBuffer from a String.
static String stripLeadingSeparator(String s)
           
static String stripLeadingZeros(String s)
           
static String stripTrailingSeparator(String s)
           
static void writeTextFile(String text, File file)
          Save a String to a text file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CHARACTER_ENCODING

protected static final String DEFAULT_CHARACTER_ENCODING
The default character encoding used if none is specified.

See Also:
Constant Field Values

DELETE_ON_EXIT_PREFIX

public static final String DELETE_ON_EXIT_PREFIX
See Also:
Constant Field Values

ILLEGAL_FILE_PATH_PART_CHARACTERS

public static final String ILLEGAL_FILE_PATH_PART_CHARACTERS
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 Map<String,String> mimeTypeToSuffixMap

suffixToMimeTypeMap

protected static Map<String,String> suffixToMimeTypeMap
Constructor Detail

WWIO

public WWIO()
Method Detail

appendPathPart

public static String appendPathPart(String firstPart,
                                    String secondPart)

byteBufferToString

public static String byteBufferToString(ByteBuffer buffer,
                                        int length,
                                        String encoding)
Create a String of limited size from a ByteBuffer.

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:
IllegalArgumentException - if the buffer is null or the length is less than 1.
IllegalCharsetNameException - if the specified encoding name is illegal.
UnsupportedCharsetException - if no support for the named encoding is available.

byteBufferToString

public static String byteBufferToString(ByteBuffer buffer,
                                        String encoding)
Create a String from a ByteBuffer.

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:
IllegalArgumentException - if the buffer is null.
IllegalCharsetNameException - if the specified encoding name is illegal.
UnsupportedCharsetException - if no support for the named encoding is available.

closeStream

public static void closeStream(Object stream,
                               String name)
Close a stream and catch any IOException generated in the process. This supports any object that implements the Closeable interface.

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.

configureProxy

public static Proxy configureProxy()

convertURIToFile

public static File convertURIToFile(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:
IllegalArgumentException - if the url is null.

convertURLToFile

public static File convertURLToFile(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:
IllegalArgumentException - if the url is null.

copyDirectory

public static void copyDirectory(File source,
                                 File destination,
                                 boolean copySubDirectories)
                          throws IOException
Throws:
IOException

copyFile

public static void copyFile(File source,
                            File destination)
                     throws IOException
Throws:
IOException

deflateBufferToFile

public static boolean deflateBufferToFile(ByteBuffer buffer,
                                          File file)
                                   throws IOException
Throws:
IOException

deleteDirectory

public static void deleteDirectory(File file)
                            throws IOException
Throws:
IOException

formPath

public static String formPath(String... pathParts)

getBufferedInputStream

public static BufferedInputStream getBufferedInputStream(InputStream is)
Returns a new BufferedInputStream which 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.

getFileForLocalAddress

public static File getFileForLocalAddress(Object src)
Converts several types of addresses to a local file to a File. Returns null if the source cannot be converted to a file. The source type may be one of the following:

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:
IllegalArgumentException - if the source is null or an empty string.

getFilename

public static String getFilename(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:
IllegalArgumentException - if the file path is null.

getFileOrResourceAsStream

public static Object getFileOrResourceAsStream(String path,
                                               Class c)

getInputStreamFromByteBuffer

public static InputStream getInputStreamFromByteBuffer(ByteBuffer buffer)
Creates an InputStream for the contents of a ByteBuffer. 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 InputStream for the buffer's contents.
Throws:
IllegalArgumentException - if buffer is null.

getInputStreamFromString

public static InputStream getInputStreamFromString(String string)
Creates an InputStream for the contents of a String. 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 InputStream for the string's contents.
Throws:
IllegalArgumentException - if string is null.

getInputStreamFromString

public static InputStream getInputStreamFromString(String string,
                                                   String encoding)
Creates an InputStream for the contents of a String. 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 InputStream for the string's contents.
Throws:
IllegalArgumentException - if string is null.

getParentFilePath

public static String getParentFilePath(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:
IllegalArgumentException - if the file path is null.

getSourcePath

public static String getSourcePath(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:

Parameters:
src - the input source of one of the above types.
Returns:
the input source's abstract path, or null if none exists.
Throws:
IllegalArgumentException - if the source is null.

getSuffix

public static String getSuffix(String filePath)

inflateFileToBuffer

public static ByteBuffer inflateFileToBuffer(File file)
                                      throws IOException
Throws:
IOException

inflateStreamToBuffer

public static ByteBuffer inflateStreamToBuffer(InputStream inputStream)
                                        throws IOException
Throws:
IOException

isAncestorOf

public static boolean isAncestorOf(File file,
                                   File ancestor)

isContentType

public static boolean isContentType(File file,
                                    String... mimeTypes)
Indicates whether a File contains 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.

isFileOutOfDate

public static boolean isFileOutOfDate(URL url,
                                      long expiryTime)

isLocalJarAddress

public static boolean isLocalJarAddress(URL jarUrl)
Determines whether a jar URL is a reference to a local jar file or an entry in a local jar file. See JarURLConnection for a description of jar URLs.

Parameters:
jarUrl - the jar URL, in the form jar:!{entry}. (Omit <, >, { and } in the actual URL}
Returns:
true if the URL refers to a local resource, otherwise false.

listChildFilenames

public static String[] listChildFilenames(File file,
                                          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:
IllegalArgumentException - if the file is null.

listDescendantFilenames

public static String[] listDescendantFilenames(File file,
                                               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:
IllegalArgumentException - if the file is null.

listDescendantFilenames

public static String[] listDescendantFilenames(File file,
                                               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 parameter recurseAfterMatch is 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:
IllegalArgumentException - if the file is null.

listDescendantFilenames

protected static void listDescendantFilenames(File parent,
                                              String pathname,
                                              FileFilter filter,
                                              boolean recurseAfterMatch,
                                              Collection<String> matches)

makeCachePathForURL

public static String[] makeCachePathForURL(URL url)

makeDataTypeForMimeType

public static String makeDataTypeForMimeType(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:
Mime TypeData Type
application/bil32AVKey.FLOAT32
application/bil16AVKey.INT16
application/bilAVKey.INT16
image/bilAVKey.INT16

Parameters:
mimeType - the mime type who's data type is returned.
Returns:
the data type for the specified mime type.
Throws:
IllegalArgumentException - if the mime type is null or malformed.

makeMimeTypeForSuffix

public static String makeMimeTypeForSuffix(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:
IllegalArgumentException - if the file suffix is null.

makeParentDirs

public static boolean makeParentDirs(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:
IllegalArgumentException - if the path is null.

makeSuffixForMimeType

public static String makeSuffixForMimeType(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:
IllegalArgumentException - if the mime type is null or malformed.

makeTempDir

public static File makeTempDir()
                        throws IOException
Create 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:
IOException - if a directory could not be created.
SecurityException - if a security manager exists and it does not allow directory creation.

makeURI

public static URI makeURI(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)

makeURL

public static URL makeURL(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 URL makeURL(Object path,
                          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)

makeURL

public static URL makeURL(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)

mapFile

public static MappedByteBuffer mapFile(File file)
                                throws IOException
Maps the specified File's bytes directly into memory as a MappedByteBuffer. The file is mapped in read-only mode; any attempt to modify the contents of the returned MappedByteBuffer causes a ReadOnlyBufferException.

Parameters:
file - the file to map.
Returns:
a read-only MappedByteBuffer representing the File's bytes.
Throws:
IOException - if the file cannot be mapped for any reason.

mapFile

public static MappedByteBuffer mapFile(File file,
                                       FileChannel.MapMode mode)
                                throws IOException
Maps the specified File's bytes directly into memory as a MappedByteBuffer according 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 a ReadOnlyBufferException.

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 of FileChannel.MapMode.READ_ONLY, FileChannel.MapMode.READ_WRITE, or FileChannel.MapMode.PRIVATE.
Returns:
a MappedByteBuffer representing the File's bytes.
Throws:
IOException - if the file cannot be mapped for any reason.

openFileOrResourceStream

public static InputStream openFileOrResourceStream(String fileName,
                                                   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 InputStream to the open file
Throws:
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's Throwable.initCause(Throwable) method.

openReader

public static Reader openReader(Object src)
                         throws IOException
Open a reader on an input source. The source may be one of the following:

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:
IOException - if i/o or other errors occur trying to create the reader.

openStream

public static InputStream openStream(Object src)
                              throws Exception
Open an InputStream from a general source. The source type may be one of the following:

Parameters:
src - the input source of one of the above types.
Returns:
an InputStream for the input source.
Throws:
IllegalArgumentException - if the source is null, an empty string, or is not one of the above types.
Exception - if the source cannot be opened for any reason.

readChannelToBuffer

public static ByteBuffer readChannelToBuffer(ReadableByteChannel channel,
                                             boolean allocateDirect)
                                      throws IOException
Reads all the available bytes from the specified ReadableByteChannel, returning the bytes as a ByteBuffer with 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:
IllegalArgumentException - if the channel is null.
IOException - if an I/O error occurs.

readChannelToBuffer

public static ByteBuffer readChannelToBuffer(ReadableByteChannel channel,
                                             ByteBuffer buffer)
                                      throws IOException
Reads the available bytes from the specified ReadableByteChannel up to the number of bytes remaining in the buffer. Bytes read from the specified channel are copied to the specified ByteBuffer. 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:
IllegalArgumentException - if the channel or the buffer is null.
IOException - if an I/O error occurs.

readChannelToString

public static String readChannelToString(ReadableByteChannel channel,
                                         String encoding)
                                  throws IOException
Reads all the available bytes from the specified ReadableByteChannel, 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:
IllegalArgumentException - if the stream is null.
IOException - if an I/O error occurs.

readCharacterStreamToString

public static String readCharacterStreamToString(Reader reader)
                                          throws IOException
Reads all the character stream content from the specified Reader, 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:
IllegalArgumentException - if the reader is null.
IOException - if an I/O error occurs.

readFileToBuffer

public static ByteBuffer readFileToBuffer(File file)
                                   throws IOException
Reads all the bytes from the specified File, returning the bytes as a non-direct ByteBuffer with 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:
IllegalArgumentException - if the file is null.
IOException - if an I/O error occurs.

readFileToBuffer

public static ByteBuffer readFileToBuffer(File file,
                                          boolean allocateDirect)
                                   throws IOException
Reads all the bytes from the specified File, returning the bytes as a ByteBuffer with 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:
IllegalArgumentException - if the file is null.
IOException - if an I/O error occurs.

readGZipFileToBuffer

public static ByteBuffer readGZipFileToBuffer(File gzFile)
                                       throws IllegalArgumentException,
                                              IOException
Throws:
IllegalArgumentException
IOException

readStreamToBuffer

public static ByteBuffer readStreamToBuffer(InputStream inputStream)
                                     throws IOException
Reads all the available bytes from the specified InputStream, returning the bytes as a non-direct ByteBuffer with 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:
IllegalArgumentException - if the stream is null.
IOException - if an I/O error occurs.

readStreamToBuffer

public static ByteBuffer readStreamToBuffer(InputStream inputStream,
                                            boolean allocateDirect)
                                     throws IOException
Reads all the available bytes from the specified InputStream, returning the bytes as a ByteBuffer with 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:
IllegalArgumentException - if the stream is null.
IOException - if an I/O error occurs.

readStreamToString

public static String readStreamToString(InputStream stream,
                                        String encoding)
                                 throws IOException
Reads all the available bytes from the specified InputStream, 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:
IllegalArgumentException - if the stream is null.
IOException - if an I/O error occurs.

readTextFile

public static String readTextFile(File file)
Open and read a text file into String.

Parameters:
file - a File reference to the file to open and read.
Returns:
a String containing the contents of the file.
Throws:
IllegalArgumentException - if the file is null.

readURLContentToBuffer

public static ByteBuffer readURLContentToBuffer(URL url)
                                         throws IOException
Reads all the bytes from the specified URL, returning the bytes as a non-direct ByteBuffer with 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:
IllegalArgumentException - if the URL is null.
IOException - if an I/O error occurs.

readURLContentToBuffer

public static ByteBuffer readURLContentToBuffer(URL url,
                                                boolean allocateDirect)
                                         throws IOException
Reads all the bytes from the specified URL, returning the bytes as a ByteBuffer with 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:
IllegalArgumentException - if the URL is null.
IOException - if an I/O error occurs.

readURLContentToString

public static String readURLContentToString(URL url,
                                            String encoding)
                                     throws IOException
Reads all the bytes from the specified URL, returning the bytes as a String. The bytes are interpreted according to the specified encoding, or UTF-8 if no encoding is specified.

Parameters:
url - the URL to read.
encoding - the encoding do use. If null is specified then UTF-8 is used.
Returns:
the string representation of the bytes at the decoded according to the specified encoding.
Throws:
IllegalArgumentException - if the url is null.
IOException - if an I/O error occurs.
IllegalCharsetNameException - if the specified encoding name is illegal.
UnsupportedCharsetException - if no support for the named encoding is available.

readZipEntryToBuffer

public static ByteBuffer readZipEntryToBuffer(File zipFile,
                                              String entryName)
                                       throws IOException
Throws:
IOException

replaceIllegalFileNameCharacters

public static String replaceIllegalFileNameCharacters(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:
IllegalArgumentException - if the specified string is null.

replaceSuffix

public static String replaceSuffix(String in,
                                   String newSuffix)

reverseFloatArray

public static void reverseFloatArray(int pos,
                                     int count,
                                     float[] array)

saveBuffer

public static boolean saveBuffer(ByteBuffer buffer,
                                 File file)
                          throws IOException
Throws:
IOException

saveBuffer

public static boolean saveBuffer(ByteBuffer buffer,
                                 File file,
                                 boolean forceFilesystemWrite)
                          throws IOException
Throws:
IOException

saveBufferToGZipFile

public static boolean saveBufferToGZipFile(ByteBuffer buffer,
                                           File file)
                                    throws IOException
Throws:
IOException

saveBufferToStream

public static boolean saveBufferToStream(ByteBuffer buffer,
                                         OutputStream fos)
                                  throws IOException
Throws:
IOException

saveBufferToTempFile

public static File saveBufferToTempFile(ByteBuffer buffer,
                                        String suffix)
                                 throws IOException
Throws:
IOException

skipBytes

public static void skipBytes(InputStream is,
                             int numBytes)
                      throws IOException
Skip over a specified number of bytes in an input stream.

Parameters:
is - the input stream.
numBytes - the number of bytes to skip over.
Throws:
IllegalArgumentException - if the specified input stream is null.
IOException - is an exception occurs while skipping the bytes.

stringToByteBuffer

public static ByteBuffer stringToByteBuffer(String string,
                                            String encoding)
                                     throws UnsupportedEncodingException
Create a ByteBuffer from a String.

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:
UnsupportedEncodingException - if the specified encoding is not supported

stripLeadingSeparator

public static String stripLeadingSeparator(String s)

stripLeadingZeros

public static String stripLeadingZeros(String s)

stripTrailingSeparator

public static String stripTrailingSeparator(String s)

writeTextFile

public static void writeTextFile(String text,
                                 File file)
Save a String to a text file.

Parameters:
text - the String to write to the file.
file - a File reference to the file to create.
Throws:
IllegalArgumentException - if the text string or file is null.

NASA World Wind