public class IOUtils
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
IOUtils.AudioAsset
An object for keeping a sound in memory.
|
static class |
IOUtils.InfoInputStream
Class to return out-of-band data with a buffered input stream.
|
static class |
IOUtils.LocatorType
Identify the scheme that generated an InfoInputStream.
|
static class |
IOUtils.StreamConsumer
Tool for consuming an inutstream in a separate thread.
|
static interface |
IOUtils.StreamToObject
Interface for parsers that read a file and produce an Object
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
CYGWIN_ROOT |
static int |
DATA |
static java.util.regex.Pattern |
DATA_PAT
A Pattern for parsing a data: URI
|
static java.util.HashMap<java.lang.String,java.lang.String> |
EXTENSION_TO_MIME |
protected static char[] |
HEX_ARRAY |
static int |
MIMEPARAMS |
static int |
MIMETYPE |
static int |
QUERY |
static int |
RELPATH |
static int |
SCHEME |
static java.util.regex.Pattern |
SCHEME_PAT
Pattern that matches a leading "scheme:".
|
static int |
SLASHES |
static java.util.regex.Pattern |
URI_PAT
A Pattern for parsing a URI string
|
protected static java.nio.file.Path |
workDir
A working directory notion for this application.
|
Constructor and Description |
---|
IOUtils() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
absolutize(java.lang.String locator)
Convert a locator to be absolute, if possible.
|
static java.lang.String |
absolutize(java.lang.String locator,
File dir)
Convert a locator to be absolute, if possible.
|
static java.lang.String |
absolutize(java.lang.String locator,
java.nio.file.Path dir)
Convert a locator to be absolute, if possible.
|
static java.lang.String |
basename(File f,
java.lang.String suffix)
parses a File name returning it with the path and suffix removed
|
static java.lang.String |
basename(java.lang.String fnm)
parses a File name returning it with the path removed
|
static java.lang.String |
basename(java.lang.String name,
java.lang.String suffix)
parses a String to remove path and suffix
|
static void |
browse(javax.swing.event.HyperlinkEvent e)
Open the user's default browser to the link in the event.
|
static void |
browse(java.lang.String dest)
Open the user's default browser to the given link.
|
static void |
browse(java.net.URL url)
Open the user's default browser to the URL.This method throws exceptions.For a generic message handler, use the verion of this method with a String argument.
|
static java.lang.String |
bytesToHex(byte[] bytes)
Convert an array of bytes to a mixed string.
|
static File |
canonicalize(java.lang.String loc,
File workDir,
java.lang.Class klass)
Convert a locator to be canonical.
|
static File |
capitalizeDriveLetter(File f)
Make the drive letter upper case.
|
static java.nio.charset.Charset |
checkCharset(java.lang.String name)
Checks if there is a charset for a given name.
|
static java.net.URL |
classURL(java.lang.Object obj)
Produce a jar: URL for a member's class.The jar format is
jar:{.jar file's URL}!/dir/dir/classname.class
where the /dir/dir elements are the names in the
class's binary name.
|
static File |
dirFromEnv(java.lang.String var) |
static java.lang.String |
dirname(java.lang.String name) |
static java.util.ArrayList<java.lang.String> |
exec(java.lang.String name,
long maxTime,
java.lang.String cmd)
Execute a shell command and return the output.Does not return until the command is completed.
|
static java.util.ArrayList<java.lang.String> |
exec(java.lang.String name,
long maxTime,
java.lang.String cmd,
java.util.function.Consumer<java.lang.String> err)
Execute a shell command and return the output.
|
static java.util.ArrayList<java.lang.String> |
exec(java.lang.String name,
java.lang.String cmd)
Execute a shell command and return the output.
|
static java.lang.String |
extension(File f)
extracts everything in f.getName() after the last dot.
|
static java.lang.String |
extension(java.lang.String name)
extracts everything in name after the last dot.
|
static java.net.URL |
f2U(File f)
Convert file name to URL.
|
static java.lang.Object |
fetchFromLocator(java.lang.Class<?> aClass,
java.lang.String src,
java.lang.Object sample)
Read from an URL with parameters and produce an appropriate Object:
String, JEditorPane, org.w3c.dom.Document, Image, byte[]
The parameters depend on type
|
static java.lang.String |
fileToUnix(File winF)
Convert a file name to Unix format.
|
static FilenameFilter |
getAcceptAllFilter()
Produce a FilenameFilter that accepts all files
|
static javax.swing.JComponent |
getComponentFor(java.lang.Object obj)
Get a viewer for one of the objects returned by fetchAsset
|
static File |
getDIR_HOME() |
static File |
getDIR_JAVAROOT() |
static File |
getDIR_PICTOOLS() |
static File |
getDIR_WEBROOT() |
static IOUtils.InfoInputStream |
getInputStream(java.lang.Class<?> aClass,
java.lang.String src)
Get a InputStream given a source specification.
|
static java.net.URL |
getJarURL(java.lang.Class<?> aClass)
Compute a URL that will access the jar holding a given class.
|
static java.lang.String |
getMimeForExt(java.lang.String ext) |
static IOUtils.StreamToObject |
getParserForMime(java.lang.String mime)
Given a mime type, returns an appropriate parser.
|
static File |
getPHYSPICS_INI_DIR() |
static FilenameFilter |
getPrefixFilter(java.lang.String nameStart)
Produce a FilenameFilter that checks that the name starts with a given prefix.
|
static Reader |
getReader(java.lang.Class<?> aClass,
IOUtils.InfoInputStream iis)
Get a Reader as described by a scheme.
|
static Reader |
getReader(java.lang.Class<?> aClass,
java.lang.String src)
Get a Reader as described by a scheme.
|
static java.lang.String |
getSibling(java.lang.String base,
java.lang.String sibling)
Given a filename string, compute the string name for a sibling.
|
static File |
getWorkDir() |
static File |
getWritableFile(java.lang.String name)
Given a name, return a File that is at that name and is writeable.
|
static File |
jarDir(java.lang.Class<?> aClass)
Get a File for the directory containing the jar.
|
static java.net.URL |
jarDirURL(java.lang.Class<?> aclass)
Get an URL for the directory containing a jar -- the jar from which
a given class was loaded.
|
static File |
jarFile(java.lang.Class<?> aClass)
Get a File object for a jar file.
|
static java.net.URL |
jarURLtoJarParent(java.net.URL jarURL)
Computes an URL that is parent to all siblings of the argument.
|
static javax.swing.ImageIcon |
loadIcon(java.lang.Class<?> app,
java.lang.String name)
Load an image and make an ImageIcon from it.
|
static javax.swing.Icon |
loadIconResource(java.lang.Class<?> app,
java.lang.String name)
Load a resource as an Icon (in fact, an ImageIcon).
|
static java.awt.image.BufferedImage |
loadImage(java.lang.Class<?> app,
java.lang.String name)
Load an Image as named by an IOUtils locator.
|
static java.awt.Image |
loadImageResource(java.lang.Class<?> app,
java.lang.String name)
Load a resource as an Image.
|
static File |
locatorFile(java.lang.Class<?> aClass,
java.lang.String src)
Parse a locator to get the corresponding file, if any.
|
static File |
nativize(File f)
Convert a File to a File for current platform.
|
static File |
nativize(java.lang.String fnm)
Convert a filename to a File for current platform.
|
static File |
newExtension(File f,
java.lang.String ext)
/** Change extension on filename.
|
static java.lang.String |
newExtension(java.lang.String path,
java.lang.String ext) |
static void |
printRootDirectories() |
static void |
putMimeForExt(java.lang.String ext,
java.lang.String mime) |
static void |
putParserForMime(java.lang.String mime,
IOUtils.StreamToObject parser) |
static java.lang.String |
readWholeFile(BufferedReader brdr)
Read the entire contents of a BufferedReader.
|
static java.lang.String |
readWholeFile(File f)
Read a File in its entirety.
|
static java.lang.String |
readWholeFile(InputStream is)
Read an InputStream in its entirety.
|
static java.lang.String |
readWholeFile(Reader rdr)
Read the entire contents of a Reader.
|
static java.lang.String |
readWholeFile(java.lang.String name)
Read a named file in its entirety.
|
static java.lang.String |
relativize(java.lang.String locator)
Convert a locator to a file relative to workDir, if possible.
|
static java.lang.String |
relativize(java.lang.String locator,
File dir)
Convert a locator to a value relative to the given dir, if possible.
|
static java.lang.String |
relativize(java.lang.String locator,
java.nio.file.Path path)
Convert a locator to a value relative to the given dir, if possible.
|
static File |
saveAnyOldVersion(File f)
If an old version of the file exists, move it to xxx-old.txt
|
static void |
setWorkDir(File f)
Set a working directory for the application.
|
static void |
showObject(java.lang.String title,
javax.swing.JComponent comp) |
static java.lang.String |
stemname(File f)
Parses a File name returning the name with the extension removed.
|
static java.lang.String |
stemname(File f,
java.lang.String suffix)
parses a file's String name returning it with a suffix removed
|
static java.lang.String |
stemname(java.lang.String fnm)
Parses a file's path name returning the name with the extension removed.
|
static java.lang.String |
stemname(java.lang.String fnm,
java.lang.String suffix)
parses a File name returning it with the suffix removed
|
static File |
valueForFinalDir(java.lang.String fnm)
Make a File for fnm and check its usability.
|
static void |
writeWholeString(File f,
java.lang.String s)
Write a String to be the contents of a file.
|
public static final java.lang.String CYGWIN_ROOT
protected static java.nio.file.Path workDir
public static final java.util.regex.Pattern SCHEME_PAT
public static final java.util.regex.Pattern URI_PAT
public static final int SCHEME
public static final int SLASHES
public static final int RELPATH
public static final int QUERY
public static final java.util.regex.Pattern DATA_PAT
public static final int MIMETYPE
public static final int MIMEPARAMS
public static final int DATA
public static final java.util.HashMap<java.lang.String,java.lang.String> EXTENSION_TO_MIME
protected static final char[] HEX_ARRAY
public static void setWorkDir(File f)
f
- The directory to set. No testing is performed.
The value is converted to a Path.public static File getWorkDir()
public static File getDIR_WEBROOT()
public static File getDIR_JAVAROOT()
public static File getDIR_PICTOOLS()
public static File getDIR_HOME()
public static File getPHYSPICS_INI_DIR()
public static void printRootDirectories()
public static void browse(javax.swing.event.HyperlinkEvent e)
e
- The Hyperlinkevent that occurred. If not of type ACTIVATED, it is ignored.public static void browse(java.lang.String dest)
dest
- String version of the destgination .public static void browse(java.net.URL url) throws java.net.URISyntaxException, IOException
url
- desired page to viewjava.net.URISyntaxException
- browse got invalid inputIOException
- browse got invalid inputpublic static File capitalizeDriveLetter(File f)
f
- File whose name is to be modified.public static File valueForFinalDir(java.lang.String fnm)
fnm
- The file namepublic static final File dirFromEnv(java.lang.String var)
var
- The name of a system property
whose value is the name of the desired value.public static java.lang.String extension(File f)
f
- The victim File whose getName() is parsedpublic static java.lang.String extension(java.lang.String name)
name
- A name to have its extension returnedpublic static java.lang.String newExtension(java.lang.String path, java.lang.String ext)
path
- A String whose name has or has not an extensionext
- the extension on the result String (if leading period is omitted, it will be supplied)public static File newExtension(File f, java.lang.String ext)
f
- A file whose name has or has not an extensionext
- the extension on the result File (if leading period is omitted, it will be supplied)public static java.lang.String stemname(File f)
f
- The File whose name to processpublic static java.lang.String stemname(java.lang.String fnm)
fnm
- The File whose name to processpublic static java.lang.String stemname(File f, java.lang.String suffix)
f
- The file path to processsuffix
- a trailing String to remove. Need not be solely an extension.
If a period in an extension is to be removed, it must appear in this argument.
If suffix is empty, remove a trailing extension, if any.public static java.lang.String stemname(java.lang.String fnm, java.lang.String suffix)
fnm
- The filename to processsuffix
- a trailing String to remove. Need not be solely an extension.
If a period in an extension is to be removed, it must appear in this argument.
If suffix is empty, remove a trailing extension, if any.public static java.lang.String basename(java.lang.String fnm)
fnm
- The filename to processpublic static java.lang.String basename(File f, java.lang.String suffix)
f
- The File whose name to processsuffix
- The suffix to remove, or null to remove any extension.
Note that the suffix is arbitrary and may or may not include a dot.
Example: "-paged.txt" will remove that entire string if found at the endpublic static java.lang.String basename(java.lang.String name, java.lang.String suffix)
name
- The string to act upon *suffix
- The suffix to remove, or null to just remove the extension.
Note that the suffix is arbitrary and may or may not include a dot.
Example: "-paged.txt" will remove that entire string if found at the end.
To remove the path and leave the extension alone, pass "" as the
value for suffix.public static java.lang.String dirname(java.lang.String name)
name
- A file name referring to a directorypublic static final java.net.URL f2U(File f)
f
- a Filepublic static java.lang.String relativize(java.lang.String locator)
locator
- The value to be revised.public static java.lang.String relativize(java.lang.String locator, File dir)
locator
- The value to be revised.dir
- the basis for the relativity.public static java.lang.String relativize(java.lang.String locator, java.nio.file.Path path)
locator
- The value to be revised.path
- the basis for the relativity.public static java.lang.String absolutize(java.lang.String locator)
locator
- A locator String.public static java.lang.String absolutize(java.lang.String locator, File dir)
locator
- A locator String.dir
- The basis absolute directory.public static java.lang.String absolutize(java.lang.String locator, java.nio.file.Path dir)
locator
- A locator String.dir
- The basis absolute directorypublic static File canonicalize(java.lang.String loc, File workDir, java.lang.Class klass)
loc
- the locator to be revisedworkDir
- Directory for absolutizing a relative nameklass
- A class in the jar where resource: should be foundpublic static File nativize(File f)
f
- the File to be convertednativize(java.lang.String)
public static File nativize(java.lang.String fnm)
fnm
- The file name.public static java.lang.String fileToUnix(File winF)
winF
- File with name to be converted (or retained) in Unix form.
The name will be requested with getAbsolutePath; the client must assure
that it is absolute with respect to the directory it is in.public static java.lang.String getSibling(java.lang.String base, java.lang.String sibling)
base
- A filename as a string.sibling
- The file to be found in the base's directory.
An initial slash is ignored, but internal slashes are kept.public static java.net.URL jarURLtoJarParent(java.net.URL jarURL)
jarURL
- The URL whose sibling directory to compute.public static File jarFile(java.lang.Class<?> aClass)
aClass
- The class whose jar is to be used.public static File jarDir(java.lang.Class<?> aClass)
aClass
- A member class of the desired jar.public static java.net.URL jarDirURL(java.lang.Class<?> aclass)
aclass
- The class whose jar is to be used.public static java.net.URL getJarURL(java.lang.Class<?> aClass)
To access a file -name- in the returned jarURL URLClassLoader urlLoad = new URLClassLoader(jarURL); urlLoad.getResource...(name); where name is slash-separated and lacks an initial slash. To access a sibling of the jar, let's say "sib.ext" URL jarDir = jarURLtoJarParent(jarURL); URL sibURL = jarDir.toURI().resolve("sib.ext").toURL(); ( if "sib.ext" can be known to have no characters that need quoting: URL sibURL = new URL(jarDir, "sib.ext"); )
aClass
- A class residing in the required jar.public static java.net.URL classURL(java.lang.Object obj)
obj
- the Class whose jar is to be found
or an object whose Class will be foundpublic static FilenameFilter getPrefixFilter(java.lang.String nameStart)
nameStart
- The prefix to look for.public static FilenameFilter getAcceptAllFilter()
public static File saveAnyOldVersion(File f)
f
- The file to be moved if it exists.public static File getWritableFile(java.lang.String name)
name
- file path. If relative, will be relative to current directory.public static void writeWholeString(File f, java.lang.String s) throws IOException
f
- the File to be written. An existing file will NOT be overwritten.s
- the String to be written to fIOException
- if it occurspublic static java.awt.Image loadImageResource(java.lang.Class<?> app, java.lang.String name)
app
- An class from the same jar as the image.name
- The name of the image.public static javax.swing.Icon loadIconResource(java.lang.Class<?> app, java.lang.String name)
app
- An class from the same jar as the icon's image.name
- The name of the icon image.public static java.awt.image.BufferedImage loadImage(java.lang.Class<?> app, java.lang.String name)
app
- An class from the same jar as the image.name
- An IOUtils locator for the image to be loaded.public static javax.swing.ImageIcon loadIcon(java.lang.Class<?> app, java.lang.String name)
app
- An class from the same jar as the icon's image.name
- An IOUtils locator for the image to be loaded.public static java.util.ArrayList<java.lang.String> exec(java.lang.String name, java.lang.String cmd)
name
- a name to appear in error messagescmd
- - the command to execute. NOTE: The shell
is platform dependent. On Windows it is 'cmd'; unix commands
will fail.public static java.util.ArrayList<java.lang.String> exec(java.lang.String name, long maxTime, java.lang.String cmd)
name
- a name to appear in error messagesmaxTime
- maximum time (in seconds) allowed for execution.
0 means no maximumcmd
- - the command to executepublic static java.util.ArrayList<java.lang.String> exec(java.lang.String name, long maxTime, java.lang.String cmd, java.util.function.Consumer<java.lang.String> err)
name
- a name to appear in error messagemaxTime
- maximum time (in seconds) allowed for execution.
0 means no maximumcmd
- the command to executeerr
- what to do with error messages. Example:
(s)->Messages.beep("something " + s)public static java.lang.String readWholeFile(java.lang.String name) throws IOException
name
- Name of file.IOException
- error reading filepublic static java.lang.String readWholeFile(File f) throws IOException
f
- The File to read,IOException
- error reading filepublic static java.lang.String readWholeFile(InputStream is) throws IOException
is
- The InputStream.IOException
- error reading filepublic static java.lang.String readWholeFile(Reader rdr) throws IOException
rdr
- the Reader for the input.IOException
- error reading filepublic static java.lang.String readWholeFile(BufferedReader brdr) throws IOException
brdr
- The BufferedReader to be read.
After reading, the reader is CLOSED.IOException
- error reading filepublic static IOUtils.InfoInputStream getInputStream(java.lang.Class<?> aClass, java.lang.String src)
Supported src String formats:
The string after userhome: jardir: or asset: is relative. An initial slash is ignored.
Assets
.
The package name is the dotted class name of the class
where the asset is defined. The default packagename
is that of the aClass parameter.
Asset returns an InfoInputStream set appropriately
src
- Location of data, as described above.aClass
- A Class object for a class in the application.
This value is needed to find the jar and the jar directory.
If null, failure is more likely.public static File locatorFile(java.lang.Class<?> aClass, java.lang.String src)
aClass
- A Class object for a class in the application.
This value is needed to find the jar and the jar directory.
It is also the default class for an asset: value.
If null and the value is needed, the function returns null.src
- The locator to parse. To denote just a directory,
the last character in the file should be a slash.public static Reader getReader(java.lang.Class<?> aClass, java.lang.String src)
src
- Input location as described for getInputStreamaClass
- A Class object for a class in the application.
This value is needed to find the jar and the jar directory.
If null, function returns null.public static Reader getReader(java.lang.Class<?> aClass, IOUtils.InfoInputStream iis)
iis
- An InfoIputStream to read from.aClass
- A Class object for a class in the application.
This value is needed to find the jar and the jar directory.
If null, function returns null.public static java.nio.charset.Charset checkCharset(java.lang.String name)
name
- Name of desired charset.public static java.lang.String getMimeForExt(java.lang.String ext)
public static void putMimeForExt(java.lang.String ext, java.lang.String mime)
public static IOUtils.StreamToObject getParserForMime(java.lang.String mime)
mime
- A mimetype of the form basis/specific,
where the basis is one of application, audio, text, or image.public static void putParserForMime(java.lang.String mime, IOUtils.StreamToObject parser)
public static java.lang.Object fetchFromLocator(java.lang.Class<?> aClass, java.lang.String src, java.lang.Object sample)
aClass
- the Class required for locators with schemes
resource: and jardir:. May be null if these schemes
are not to be used; but this risks NullPointer exceptions.src
- the URL source for the objectsample
- depends on expected type to arrive on the URL.
The type of sample is a fallback mechanism for determining
the type of the expected result.
for a text, String; null = HYBRID-UTF-8 string content may be charset name for byte[], may be a Byte object for xml, DocumentBuilder; null = newDocumentBuilder() parameters of the DocumentBuilder may be set in the DocumentBuilderFactory before creating the DocumentBuilder for audio, AudioAsset, possibly with format set; null = {get format from the stream} (best if left at null) for image, an Image to suggest that result will be an image for jed, an JEditorPane destination for the parse charset is via getClientProperty("charset") if mimetype is not apparent in the stream, use type of editorkit sample and default: text/html; charset=HYBRID-UTF-8 otherwise it creates a new JeditorPane
public static java.lang.String bytesToHex(byte[] bytes)
bytes
- the inputpublic static javax.swing.JComponent getComponentFor(java.lang.Object obj)
obj
- An Object. especially one that can be viewed by a JComponent.public static void showObject(java.lang.String title, javax.swing.JComponent comp)