public class WWIO extends Object
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
WWIO() |
Modifier and Type | Method and Description |
---|---|
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
, 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. |
protected static final String DEFAULT_CHARACTER_ENCODING
public static final String DELETE_ON_EXIT_PREFIX
public static final String ILLEGAL_FILE_PATH_PART_CHARACTERS
public static final int MAX_FILE_PATH_LENGTH
public static String byteBufferToString(ByteBuffer buffer, int length, String encoding)
String
of limited size from a ByteBuffer
.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.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.public static String byteBufferToString(ByteBuffer buffer, String encoding)
String
from a ByteBuffer
.buffer
- the byte buffer to convert.encoding
- the encoding do use. If null is specified then UTF-8 is used.IllegalArgumentException
- if the buffer is null.IllegalCharsetNameException
- if the specified encoding name is illegal.UnsupportedCharsetException
- if no support for the named encoding is available.public static void closeStream(Object stream, String name)
IOException
generated in the process. This supports any object that
implements the Closeable
interface.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.public static Proxy configureProxy()
public static File convertURIToFile(URI uri)
uri
- the URI to convert to a local file path.IllegalArgumentException
- if the url is null.public static File convertURLToFile(URL url)
url
- the URL to convert to a local file path.IllegalArgumentException
- if the url is null.public static void copyDirectory(File source, File destination, boolean copySubDirectories) throws IOException
IOException
public static void copyFile(File source, File destination) throws IOException
IOException
public static boolean deflateBufferToFile(ByteBuffer buffer, File file) throws IOException
IOException
public static void deleteDirectory(File file) throws IOException
IOException
public static BufferedInputStream getBufferedInputStream(InputStream is)
BufferedInputStream
which wraps the specified InputStream. If the specified
InputStream is already a BufferedInputStream, this returns the original InputStream cast to a
BufferedInputStream.is
- the InputStream to wrap with a new BufferedInputStream.public static File getFileForLocalAddress(Object src)
File
. Returns null if the source cannot
be converted to a file. The source type may be one of the following: src
- the source to convert to local file path.IllegalArgumentException
- if the source is null or an empty string.public static String getFilename(String filePath)
filePath
- a file path String.IllegalArgumentException
- if the file path is null.public static InputStream getInputStreamFromByteBuffer(ByteBuffer buffer)
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.buffer
- the buffer to create a stream for.InputStream
for the buffer's contents.IllegalArgumentException
- if buffer
is null.public static InputStream getInputStreamFromString(String string)
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.string
- the string to create a stream for, encoded in UTF-8.InputStream
for the string's contents.IllegalArgumentException
- if string
is null.public static InputStream getInputStreamFromString(String string, String encoding)
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.string
- the string to create a stream for.encoding
- the character encoding of the string. UTF-8 is used if null.InputStream
for the string's contents.IllegalArgumentException
- if string
is null.public static String getParentFilePath(String filePath)
filePath
- a file path String.IllegalArgumentException
- if the file path is null.public static String getSourcePath(Object src)
src
- the input source of one of the above types.IllegalArgumentException
- if the source is null.public static ByteBuffer inflateFileToBuffer(File file) throws IOException
IOException
public static ByteBuffer inflateStreamToBuffer(InputStream inputStream) throws IOException
IOException
public static boolean isContentType(File file, String... mimeTypes)
File
contains content of a specified mime type.
Only the filename suffix is consulted to determine the file's content type.file
- the file to test.mimeTypes
- the mime types to test for.public static boolean isFileOutOfDate(URL url, long expiryTime)
public static boolean isLocalJarAddress(URL jarUrl)
JarURLConnection
for a description of jar URLs.jarUrl
- the jar URL, in the form jar:public static String[] listChildFilenames(File file, FileFilter filter)
file
- the directory who's contents to list.filter
- a file filter.IllegalArgumentException
- if the file is null.public static String[] listDescendantFilenames(File file, FileFilter filter)
file
- the directory tree who's contents to list.filter
- a file filter.IllegalArgumentException
- if the file is null.public static String[] listDescendantFilenames(File file, FileFilter filter, boolean recurseAfterMatch)
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.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.IllegalArgumentException
- if the file is null.protected static void listDescendantFilenames(File parent, String pathname, FileFilter filter, boolean recurseAfterMatch, Collection<String> matches)
public static String makeDataTypeForMimeType(String mimeType)
Mime Type | Data Type |
---|---|
application/bil32 | AVKey.FLOAT32 |
application/bil16 | AVKey.INT16 |
application/bil | AVKey.INT16 |
image/bil | AVKey.INT16 |
mimeType
- the mime type who's data type is returned.IllegalArgumentException
- if the mime type is null or malformed.public static String makeMimeTypeForSuffix(String suffix)
suffix
- the suffix who's mime type is returned.IllegalArgumentException
- if the file suffix is null.public static boolean makeParentDirs(String path)
path
- the path whose directories are vefified to exist or be created. The last element of the path is
ignored.IllegalArgumentException
- if the path is null.public static String makeSuffixForMimeType(String mimeType)
mimeType
- the mime type who's suffix is returned.IllegalArgumentException
- if the mime type is null or malformed.public static File makeTempDir() throws IOException
IOException
- if a directory could not be created.SecurityException
- if a security manager exists and it does not allow directory creation.public static URI makeURI(Object path)
path
- the object from which to create a URI, typically a string.makeURL(String)
,
makeURL(Object)
,
makeURL(Object, String)
public static URL makeURL(Object path)
path
- the object from which to create a URL, typically a string.makeURL(String)
,
makeURL(Object, String)
public static URL makeURL(Object path, String defaultProtocol)
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.makeURL(String)
,
makeURL(Object)
public static URL makeURL(String path)
path
- the string to convert to a URL.makeURL(Object)
,
makeURL(Object, String)
public static MappedByteBuffer mapFile(File file) throws IOException
MappedByteBuffer
. The file is mapped
in read-only mode; any attempt to modify the contents of the returned MappedByteBuffer causes a ReadOnlyBufferException
.file
- the file to map.IOException
- if the file cannot be mapped for any reason.public static MappedByteBuffer mapFile(File file, FileChannel.MapMode mode) throws IOException
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.file
- the file to map.mode
- the mapping mode, one of FileChannel.MapMode.READ_ONLY
, FileChannel.MapMode.READ_WRITE
, or FileChannel.MapMode.PRIVATE
.IOException
- if the file cannot be mapped for any reason.public static InputStream openFileOrResourceStream(String fileName, Class c)
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.InputStream
to the open fileIllegalArgumentException
- 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.public static Reader openReader(Object src) throws IOException
src
- the input source of one of the above types.IOException
- if i/o or other errors occur trying to create the reader.public static InputStream openStream(Object src) throws Exception
InputStream
from a general source. The source type may be one of the following: InputStream
URL
URI
File
String
containing a valid URL description or a file or resource name
available on the classpath.src
- the input source of one of the above types.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.public static ByteBuffer readChannelToBuffer(ReadableByteChannel channel, boolean allocateDirect) throws IOException
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.channel
- the channel to read.allocateDirect
- true to allocate and return a direct buffer, false to allocate and return a non-direct
buffer.IllegalArgumentException
- if the channel is null.IOException
- if an I/O error occurs.public static ByteBuffer readChannelToBuffer(ReadableByteChannel channel, ByteBuffer buffer) throws IOException
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.channel
- the channel to read bytes from.buffer
- the buffer to receive the bytes.IllegalArgumentException
- if the channel or the buffer is null.IOException
- if an I/O error occurs.public static String readChannelToString(ReadableByteChannel channel, String encoding) throws IOException
ReadableByteChannel
, returning the
bytes as a String. The bytes are interpreted according to the specified encoding, or UTF-8 if no encoding is
specified.channel
- the channel to read.encoding
- the encoding do use. If null is specified then UTF-8 is used.IllegalArgumentException
- if the stream
is null.IOException
- if an I/O error occurs.public static String readCharacterStreamToString(Reader reader) throws IOException
Reader
, returning a the accumulated
content as a String .reader
- the character stream to read.IllegalArgumentException
- if the reader
is null.IOException
- if an I/O error occurs.public static ByteBuffer readFileToBuffer(File file) throws IOException
File
, returning the bytes as a non-direct ByteBuffer
with
the current JVM byte order. Non-direct buffers are backed by JVM heap memory.file
- the file to read.IllegalArgumentException
- if the file is null.IOException
- if an I/O error occurs.public static ByteBuffer readFileToBuffer(File file, boolean allocateDirect) throws IOException
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.file
- the file to read.allocateDirect
- true to allocate and return a direct buffer, false to allocate and return a non-direct
buffer.IllegalArgumentException
- if the file is null.IOException
- if an I/O error occurs.public static ByteBuffer readGZipFileToBuffer(File gzFile) throws IllegalArgumentException, IOException
IllegalArgumentException
IOException
public static ByteBuffer readStreamToBuffer(InputStream inputStream) throws IOException
InputStream
, returning the bytes as a non-direct ByteBuffer
with the current JVM byte order. Non-direct buffers are backed by JVM heap memory.inputStream
- the stream to read.IllegalArgumentException
- if the stream is null.IOException
- if an I/O error occurs.public static ByteBuffer readStreamToBuffer(InputStream inputStream, boolean allocateDirect) throws IOException
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.inputStream
- the stream to read.allocateDirect
- true to allocate and return a direct buffer, false to allocate and return a non-direct
buffer.IllegalArgumentException
- if the stream is null.IOException
- if an I/O error occurs.public static String readStreamToString(InputStream stream, String encoding) throws IOException
InputStream
, returning the bytes as a String.
The bytes are interpreted according to the specified encoding, or UTF-8 if no encoding is specified.stream
- the stream to read.encoding
- the encoding do use. If null is specified then UTF-8 is used.IllegalArgumentException
- if the stream
is null.IOException
- if an I/O error occurs.public static String readTextFile(File file)
String
.file
- a File
reference to the file to open and read.String
containing the contents of the file.IllegalArgumentException
- if the file is null.public static ByteBuffer readURLContentToBuffer(URL url) throws IOException
URL
, returning the bytes as a non-direct ByteBuffer
with
the current JVM byte order. Non-direct buffers are backed by JVM heap memory.url
- the URL to read.IllegalArgumentException
- if the URL is null.IOException
- if an I/O error occurs.public static ByteBuffer readURLContentToBuffer(URL url, boolean allocateDirect) throws IOException
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.url
- the URL to read.allocateDirect
- true to allocate and return a direct buffer, false to allocate and return a non-direct
buffer.IllegalArgumentException
- if the URL is null.IOException
- if an I/O error occurs.public static String readURLContentToString(URL url, String encoding) throws IOException
URL
, returning the bytes as a String. The bytes are
interpreted according to the specified encoding, or UTF-8 if no encoding is specified.url
- the URL to read.encoding
- the encoding do use. If null
is specified then UTF-8 is used. decoded according to the specified
encoding.
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.public static ByteBuffer readZipEntryToBuffer(File zipFile, String entryName) throws IOException
IOException
public static String replaceIllegalFileNameCharacters(String s)
s
- the string to examine.IllegalArgumentException
- if the specified string is null.public static void reverseFloatArray(int pos, int count, float[] array)
public static boolean saveBuffer(ByteBuffer buffer, File file) throws IOException
IOException
public static boolean saveBuffer(ByteBuffer buffer, File file, boolean forceFilesystemWrite) throws IOException
IOException
public static boolean saveBufferToGZipFile(ByteBuffer buffer, File file) throws IOException
IOException
public static boolean saveBufferToStream(ByteBuffer buffer, OutputStream fos) throws IOException
IOException
public static File saveBufferToTempFile(ByteBuffer buffer, String suffix) throws IOException
IOException
public static void skipBytes(InputStream is, int numBytes) throws IOException
is
- the input stream.numBytes
- the number of bytes to skip over.IllegalArgumentException
- if the specified input stream is null.IOException
- is an exception occurs while skipping the bytes.public static ByteBuffer stringToByteBuffer(String string, String encoding) throws UnsupportedEncodingException
ByteBuffer
from a String
.string
- the string to convert.encoding
- the encoding do use. If null is specified then UTF-8 is used.UnsupportedEncodingException
- if the specified encoding is not supported