NASA World Wind

gov.nasa.worldwind.util
Class WWIO

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

public class WWIO
extends java.lang.Object


Field Summary
static java.lang.String DELETE_ON_EXIT_PREFIX
           
static java.lang.String ILLEGAL_FILE_PATH_PART_CHARACTERS
           
 
Constructor Summary
WWIO()
           
 
Method Summary
static java.lang.String byteBufferToString(java.nio.ByteBuffer buffer, java.lang.String encoding)
          Create a String from a ByteBuffer.
static void closeStream(java.lang.Object stream, java.lang.String name)
          Close a stream and catch any IOException generated in the process.
static java.net.Proxy configureProxy()
           
static java.io.File convertURLToFile(java.net.URL url)
          Converts a specified URL as to a path in the local file system.
static void copyDirectory(java.io.File source, java.io.File destination, boolean copySubDirectories)
           
static void copyFile(java.io.File source, java.io.File destination)
           
static void deflateBufferToFile(java.nio.ByteBuffer buffer, java.io.File file)
           
static void deleteDirectory(java.io.File file)
           
static java.lang.String formPath(java.lang.String... pathParts)
           
static java.lang.String getFilename(java.lang.String filePath)
          Returns the name of the file or directory denoted by the specified path.
static java.lang.Object getFileOrResourceAsStream(java.lang.String path, java.lang.Class c)
           
static java.io.InputStream getInputStreamFromByteBuffer(java.nio.ByteBuffer buffer)
          Creates an InputStream for the contents of a ByteBuffer.
static java.io.InputStream getInputStreamFromString(java.lang.String string)
          Creates an InputStream for the contents of a String.
static java.io.InputStream getInputStreamFromString(java.lang.String string, java.lang.String encoding)
          Creates an InputStream for the contents of a String.
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.
static java.lang.String getSuffix(java.lang.String filePath)
           
static java.nio.ByteBuffer inflateFileToBuffer(java.io.File file)
           
static java.nio.ByteBuffer inflateStreamToBuffer(java.io.InputStream inputStream)
           
static boolean isAncestorOf(java.io.File file, java.io.File ancestor)
           
static boolean isFileOutOfDate(java.net.URL url, long expiryTime)
           
static java.lang.String makeSuffixForMimeType(java.lang.String mimeType)
           
static java.net.URL makeURL(java.lang.String path)
           
static java.nio.MappedByteBuffer mapFile(java.io.File file)
           
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.
static java.io.Reader openReader(java.lang.Object src)
          Open a reader on an input source.
static java.nio.ByteBuffer readFileToBuffer(java.io.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 java.nio.ByteBuffer readFileToBuffer(java.io.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 java.nio.ByteBuffer readStreamToBuffer(java.io.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 java.nio.ByteBuffer readStreamToBuffer(java.io.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 java.lang.String readTextFile(java.io.File file)
          Open and read a text file into String.
static java.nio.ByteBuffer readURLContentToBuffer(java.net.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 java.nio.ByteBuffer readURLContentToBuffer(java.net.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 java.nio.ByteBuffer readZipEntryToBuffer(java.io.File zipFile, java.lang.String entryName)
           
static java.lang.String replaceSuffix(java.lang.String in, java.lang.String newSuffix)
           
static boolean saveBuffer(java.nio.ByteBuffer buffer, java.io.File file)
           
static boolean saveBuffer(java.nio.ByteBuffer buffer, java.io.File file, boolean forceFilesystemWrite)
           
static boolean saveBufferToStream(java.nio.ByteBuffer buffer, java.io.OutputStream fos)
           
static java.io.File saveBufferToTempFile(java.nio.ByteBuffer buffer, java.lang.String suffix)
           
static java.nio.ByteBuffer stringToByteBuffer(java.lang.String string, java.lang.String encoding)
          Create a ByteBuffer from a String.
static java.lang.String stripIllegalFileNameCharacters(java.lang.String s)
           
static java.lang.String stripLeadingSeparator(java.lang.String s)
           
static java.lang.String stripLeadingZeros(java.lang.String s)
           
static java.lang.String stripTrailingSeparator(java.lang.String s)
           
static void writeTextFile(java.lang.String text, java.io.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

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
Constructor Detail

WWIO

public WWIO()
Method Detail

byteBufferToString

public static java.lang.String byteBufferToString(java.nio.ByteBuffer buffer,
                                                  java.lang.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.

closeStream

public static void closeStream(java.lang.Object stream,
                               java.lang.String name)
Close a stream and catch any IOException generated in the process. Stream types supported are InputStream, OutputStream, Reader and Writer.

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 java.net.Proxy configureProxy()

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.

copyDirectory

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

copyFile

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

deflateBufferToFile

public static void deflateBufferToFile(java.nio.ByteBuffer buffer,
                                       java.io.File file)
                                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

formPath

public static java.lang.String formPath(java.lang.String... pathParts)

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.

getFileOrResourceAsStream

public static java.lang.Object getFileOrResourceAsStream(java.lang.String path,
                                                         java.lang.Class c)

getInputStreamFromByteBuffer

public static java.io.InputStream getInputStreamFromByteBuffer(java.nio.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:
java.lang.IllegalArgumentException - if buffer is null.

getInputStreamFromString

public static java.io.InputStream getInputStreamFromString(java.lang.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:
java.lang.IllegalArgumentException - if string is null.

getInputStreamFromString

public static java.io.InputStream getInputStreamFromString(java.lang.String string,
                                                           java.lang.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:
java.lang.IllegalArgumentException - if string 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.

getSuffix

public static java.lang.String getSuffix(java.lang.String filePath)

inflateFileToBuffer

public static java.nio.ByteBuffer inflateFileToBuffer(java.io.File file)
                                               throws java.io.IOException
Throws:
java.io.IOException

inflateStreamToBuffer

public static java.nio.ByteBuffer inflateStreamToBuffer(java.io.InputStream inputStream)
                                                 throws java.io.IOException
Throws:
java.io.IOException

isAncestorOf

public static boolean isAncestorOf(java.io.File file,
                                   java.io.File ancestor)

isFileOutOfDate

public static boolean isFileOutOfDate(java.net.URL url,
                                      long expiryTime)

makeSuffixForMimeType

public static java.lang.String makeSuffixForMimeType(java.lang.String mimeType)

makeURL

public static java.net.URL makeURL(java.lang.String path)

mapFile

public static java.nio.MappedByteBuffer mapFile(java.io.File file)
                                         throws java.io.IOException
Throws:
java.io.IOException

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 InputStream to 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's Throwable.initCause(Throwable) method.

openReader

public static java.io.Reader openReader(java.lang.Object src)
                                 throws java.io.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:
java.io.IOException - if i/o or other errors occur trying to create the reader.

readFileToBuffer

public static java.nio.ByteBuffer readFileToBuffer(java.io.File file)
                                            throws java.io.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:
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.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:
java.lang.IllegalArgumentException - if the file is null.
java.io.IOException - if an I/O error occurs.

readStreamToBuffer

public static java.nio.ByteBuffer readStreamToBuffer(java.io.InputStream inputStream)
                                              throws java.io.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:
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.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:
java.lang.IllegalArgumentException - if the stream is null.
java.io.IOException - if an I/O error occurs.

readTextFile

public static java.lang.String readTextFile(java.io.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:
java.lang.IllegalArgumentException - if the file is null.

readURLContentToBuffer

public static java.nio.ByteBuffer readURLContentToBuffer(java.net.URL url)
                                                  throws java.io.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:
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.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:
java.lang.IllegalArgumentException - if the URL is null.
java.io.IOException - if an I/O error occurs.

readZipEntryToBuffer

public static java.nio.ByteBuffer readZipEntryToBuffer(java.io.File zipFile,
                                                       java.lang.String entryName)
                                                throws java.io.IOException
Throws:
java.io.IOException

replaceSuffix

public static java.lang.String replaceSuffix(java.lang.String in,
                                             java.lang.String newSuffix)

saveBuffer

public static boolean saveBuffer(java.nio.ByteBuffer buffer,
                                 java.io.File file)
                          throws java.io.IOException
Throws:
java.io.IOException

saveBuffer

public static boolean saveBuffer(java.nio.ByteBuffer buffer,
                                 java.io.File file,
                                 boolean forceFilesystemWrite)
                          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

saveBufferToTempFile

public static java.io.File saveBufferToTempFile(java.nio.ByteBuffer buffer,
                                                java.lang.String suffix)
                                         throws java.io.IOException
Throws:
java.io.IOException

stringToByteBuffer

public static java.nio.ByteBuffer stringToByteBuffer(java.lang.String string,
                                                     java.lang.String encoding)
                                              throws java.io.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:
java.io.UnsupportedEncodingException - if the specified encoding is not supported

stripIllegalFileNameCharacters

public static java.lang.String stripIllegalFileNameCharacters(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)

stripTrailingSeparator

public static java.lang.String stripTrailingSeparator(java.lang.String s)

writeTextFile

public static void writeTextFile(java.lang.String text,
                                 java.io.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:
java.lang.IllegalArgumentException - if the text string or file is null.

NASA World Wind