public class Utilities extends Object
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_ISO_CODE_PAGE
The string used for the ISO 8859-1 codepage: "8859_1".
|
static char[] |
HEXES_CA
The hex characters as a character array.
|
static String |
HEXES_S
The hex characters as a String.
|
static boolean |
isVerbose
Flag for verbose output.
|
static String |
LINE_SEPARATOR
The Line separator ("\r\n" for Windows, "\n" for Unix).
|
static String |
PROGRAM_DOMAIN |
static String |
PROGRAM_NAME |
static String |
PROGRAM_NAME_CLIENT |
static String |
PROGRAM_NAME_EDITOR |
static String |
PROGRAM_NAME_SERVER |
static String |
PROGRAM_NAME_UPPER |
Constructor and Description |
---|
Utilities() |
Modifier and Type | Method and Description |
---|---|
static URLClassLoader |
createClassLoader(String spec,
boolean doVerifyJarZip,
ClassLoader parent)
Creates a class loader from a specification of JAR/ZIP files or directories in a string that
is either comma or colon separated.
|
static URLClassLoader |
createClassLoader(String spec,
boolean doVerifyJarZip,
ClassLoader parentClassLoader,
StringBuilder parsedClassPath)
Creates a class loader from a specification of JAR/ZIP files or directories in a string that
is either comma or colon separated.
|
static Transformer |
createDefaultTransformer()
Creates a default transformer using UTF-8 encoding and indenting.
|
static Document |
createDocument()
Creates an empty name-space aware Document.
|
static String |
createString(byte[] data)
Helper function for non-initialized byte-to-string conversion
using ISO 8859-1 character set.
|
static String |
createString(byte[] data,
int offset,
int length)
Helper function for non-initialized byte-to-string conversion
using ISO 8859-1 character set.
|
static void |
disposeInternalFrame(JInternalFrame f)
Disposes of a JInternalFrame window inside a JDesktopPane.
|
static String |
enableUnlimitedCryptography()
Make sure to enable unlimited cryptography (try to enable it for Java 8 update 152 or better).
|
static String |
enableUnlimitedCryptography(boolean allowLegacySSL)
Make sure to enable unlimited cryptography (try to enable it for Java 8 update 152 or better).
|
static boolean |
endsWith(byte[] source,
byte[] suffix)
Tests if the source byte array ends with the specified suffix byte array.
|
static boolean |
equals(Object o1,
Object o2)
Checks for equality of two objects, e.g.
|
static String |
formatDurationMillis(long duration)
Formats a String from a duration in milliseconds as hhhhh:mm:ss.mmm.
|
static InetAddress[] |
getAllByName(String host)
Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system.
|
static String |
getBuild()
Get the program build number from the program version string.
|
static int |
getBuildNumber()
Gets the program build number.
|
static InetAddress |
getByName(String host)
Determines the IP address of a host, given the host's name.
|
static byte[] |
getBytes(Document document)
Gets the byte data from an XML document.
|
static byte[] |
getBytes(String data)
Helper function for non-initialized string-to-byte conversion
using ISO 8859-1 character set.
|
static TransformerFactory |
getDefaultTransformerFactory()
Creates the default transformer factory.
|
static String |
getEnglishSize(long size)
Gets a size as nn.m TB/GB/MB/KB or bytes.
|
static String |
getHostName(InetAddress ina)
Gets the host name backed up by a cache so that it is only retrieved once.
|
static String |
getHTMLString(String s)
Converts a string so that it doesn't include HTML tags that may affect the display
of the text.
|
static byte[] |
getIPv4Address(String name)
Gets the IPv4 byte address.
|
static String |
getStackTrace(Throwable e)
After an exception, call this method to get the stack trace.
|
static String |
getTS()
Gets a timestamp string in format "HH:MM:SS.mmm".
|
static String |
getUserPrivateDirectory()
Gets the directory used to store private information for the user.
|
static String |
getUserPrivateDirectory(boolean doSystemOut)
Gets the directory used to store private information for the user.
|
static String |
getVersion()
Get the version number from the program version string, e.g.
|
static int |
hashCode(byte[] source)
Returns a hash code for the byte array.
|
static int |
indexOf(byte[] source,
byte[] search)
Returns the index within the
source array of the first occurrence of the
specified search byte array. |
static int |
indexOf(byte[] source,
byte[] search,
int fromIndex)
Returns the index within the source byte array of the first occurrence of the
specified search byte array, starting at the specified index.
|
static boolean |
isJarOrZip(String fn)
Checks if this file is a JAR or ZIP file (by the extension).
|
static boolean |
isLocalAddress(String host)
Checks if it's a local (intranet) host address.
|
static boolean |
isLocalhost(String name)
Check if it's localhost.
|
static int |
lastIndexOf(byte[] source,
byte[] search)
Returns the index within the source byte array of the rightmost occurrence
of the specified search byte array.
|
static int |
lastIndexOf(byte[] source,
byte[] search,
int fromIndex)
Returns the index within the source byte array of the last occurrence of the
specified search byte array, searching backward starting at the specified index.
|
static ArrayList<String> |
parseArgs(String cmdline)
Parses the arguments divided by spaces but kept together
by quotes.
|
static void |
setToolTipText(JComponent parent,
ArrayList<JComponent> listOrNull,
String s)
Sets the tool-tip text for the JComponent all children, except the ones and its children
that may be in a non-null "listOrNull" array.
|
static void |
setToolTipText(JComponent parent,
String s)
Sets the tool-tip text for the JComponent all children, but not the JComponent "parent" itself!
|
static boolean |
startsWith(byte[] source,
byte[] prefix)
Tests if the source byte array starts with the specified prefix byte array.
|
static boolean |
startsWith(byte[] source,
byte[] prefix,
int sourceIndex)
Tests if the source sub-byte array of a byte array beginning at the
specified source index starts with the specified prefix.
|
static String |
toHex(byte[] ab)
Converts to hex.
|
public static final String DEFAULT_ISO_CODE_PAGE
public static final String LINE_SEPARATOR
public static boolean isVerbose
public static String PROGRAM_NAME
public static String PROGRAM_NAME_UPPER
public static String PROGRAM_NAME_CLIENT
public static String PROGRAM_NAME_SERVER
public static String PROGRAM_NAME_EDITOR
public static String PROGRAM_DOMAIN
public static final String HEXES_S
public static final char[] HEXES_CA
public static String getStackTrace(Throwable e)
e
- The exception.public static final String createString(byte[] data)
public static final String createString(byte[] data, int offset, int length)
public static final byte[] getBytes(String data)
public static String getUserPrivateDirectory()
public static String getUserPrivateDirectory(boolean doSystemOut)
doSystemOut
- If printing should be done or not.public static void setToolTipText(JComponent parent, String s)
public static void setToolTipText(JComponent parent, ArrayList<JComponent> listOrNull, String s)
public static String getHTMLString(String s)
public static URLClassLoader createClassLoader(String spec, boolean doVerifyJarZip, ClassLoader parent) throws MalformedURLException
The new URLClassLoader is created for the given URLs. The URLs will be searched in the order specified for classes and resources after first searching in the specified parent class loader.
spec
- The URL's separated by comma or colon.doVerifyJarZip
- Each entry referring to a JAR/ZIP file will be checked if it's a valid.parent
- The parent class loader for delegation, null for none.
IT IS NOT RECOMMENDED TO USE null
for the parent
class loader
as this has been modified dramatically in Java 9, 10, 11 or better!
MalformedURLException
- If an URL is malformed.public static URLClassLoader createClassLoader(String spec, boolean doVerifyJarZip, ClassLoader parentClassLoader, StringBuilder parsedClassPath) throws MalformedURLException
The new URLClassLoader is created for the given URLs. The URLs will be searched in the order specified for classes and resources after first searching in the specified parent class loader.
spec
- The URL's separated by comma or colon.doVerifyJarZip
- Each entry referring to a JAR/ZIP file will be checked if it's a valid.parentClassLoader
- The parent class loader for delegation, null
for none.
IT IS NOT RECOMMENDED TO USE null
for the parentClassLoader
as this has been modified dramatically in Java 9, 10, 11 or better!
parsedClassPath
- If non-null, this StringBuilder instance will contain the parsed classpath
separated with ";" and correct character case.MalformedURLException
- If an URL is malformed.public static void disposeInternalFrame(JInternalFrame f)
public static TransformerFactory getDefaultTransformerFactory()
public static Transformer createDefaultTransformer() throws TransformerConfigurationException
TransformerConfigurationException
- When the transformer configuration is in error.public static byte[] getBytes(Document document) throws TransformerException
document
- the Document.TransformerException
- When there is a error in the transformation.public static Document createDocument()
public static boolean isJarOrZip(String fn)
public static boolean startsWith(byte[] source, byte[] prefix, int sourceIndex)
source
- The source byte array.prefix
- The prefix byte array.sourceIndex
- Where to begin looking in the source array.true
if the source
byte array starting at the specified
sourceIndex
starts with the prefix
byte array;
false
otherwise.
The result is false
if sourceIndex
is
negative or greater than the length of this source array.public static boolean startsWith(byte[] source, byte[] prefix)
source
- The source byte array.prefix
- The prefix byte array.true
if the source
byte array starts
with the prefix
byte array; false
otherwise.public static boolean endsWith(byte[] source, byte[] suffix)
source
- The source byte array.suffix
- The suffix byte array.true
if the source
byte array ends
with the suffix
byte array; false
otherwise.public static int hashCode(byte[] source)
using(source[0]&0xFF)*31^(n-1) + (source[1]&0xFF)*31^(n-2) + ... + (source[n-1]&0xFF)
int
arithmetic, where source[i]
is the
ith byte of the array, n
is the length of
the byte array, and ^
indicates exponentiation.
(The hash value of the array length is zero.)
Note: each byte uses the value 0-255 and not -127 to 128.
Note 2: this method returns the same hash code as
new String(source,"8859_1").hashCode()
.
source
- the byte array.public static int indexOf(byte[] source, byte[] search)
source
array of the first occurrence of the
specified search
byte array. The integer returned is the smallest value
k such that:
isstartsWith(source,search,k)
true
.source
- the source byte array.search
- the byte array to locate in the source array.search
first occurs in source
;
-1 if isn't present.public static int indexOf(byte[] source, byte[] search, int fromIndex)
If no such value of k exists, then -1 is returned.k >= Math.min(fromIndex,source.length) && this.startsWith(source,search,k)
source
- the source byte array.search
- the byte array to locate in the source array.fromIndex
- the index from which to start the search.source
byte array of
the first occurrence of the specified search
byte array,
starting at the specified index.public static int lastIndexOf(byte[] source, byte[] search)
source.length
.
The returned index is the largest value k such that
is true.startsWith(source,search,k)
source
- the bytes being searched.search
- the byte array to search for.-1
is returned.public static int lastIndexOf(byte[] source, byte[] search, int fromIndex)
If no such value of k exists, then -1 is returned.k <= Math.min(fromIndex,source.length) && startsWith(source,search,k)
source
- the bytes being searched.search
- the byte array to search for.fromIndex
- the index to start the search from.public static String toHex(byte[] ab)
ab
- Array of bytes to convert to a hex string of twice the size.public static InetAddress getByName(String host) throws UnknownHostException
The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.
If the host is null then an InetAddress representing an address of the loopback interface is returned.
host
- the specified host, or null.UnknownHostException
- if no IP address for the host could be found, or if a scope_id was specified for a global IPv6 address.public static InetAddress[] getAllByName(String host) throws UnknownHostException
The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.
For host specified in literal IPv6 address, either the form defined in RFC 2732 or the literal IPv6 address format defined in RFC 2373 is accepted. A literal IPv6 address may also be qualified by appending a scoped zone identifier or scope_id.
If the host is null then an InetAddress representing an address of the loopback interface is returned.
host
- host - the name of the host, or null.UnknownHostException
- if no IP address for the host could be found, or if a scope_id was specified for a global IPv6 address.public static byte[] getIPv4Address(String name)
public static boolean isLocalhost(String name)
public static boolean isLocalAddress(String host)
host
- The host name.public static String getHostName(InetAddress ina)
public static String getTS()
public static String getBuild()
public static int getBuildNumber()
public static String getVersion()
public static ArrayList<String> parseArgs(String cmdline)
public static boolean equals(Object o1, Object o2)
public static String formatDurationMillis(long duration)
duration
- Duration in milliseconds.public static String getEnglishSize(long size)
size
- The positive size value.public static String enableUnlimitedCryptography()
allowUnsafe
- Flag to allow unsafe stuff, i.e. not to set the system properties:
public static String enableUnlimitedCryptography(boolean allowLegacySSL)
allowLegacySSL
- Flag to allow unsafe legacy stuff, i.e. not to set the system properties:
Phantom® and NetPhantom® are registered trademarks of Mindus SARL.
© © Mindus SARL, 2024. All rights reserved.