@PublicAPI(stability=UNCOMMITTED, mayInstantiate=false, mayExtend=false, mayInvoke=true) public final class StaticUtils extends Object
Modifier and Type | Field and Description |
---|---|
static int |
INT_SIZE
The number of bytes of a Java int.
|
static int |
KB
Number of bytes in a Kibibyte.
|
static int |
LONG_SIZE
The number of bytes of a Java long.
|
static int |
MB
Number of bytes in a Mebibyte.
|
Modifier and Type | Method and Description |
---|---|
static void |
addSuperiorObjectClasses(Map<ObjectClass,String> objectClasses)
Add all of the superior objectclasses to the specified objectclass
map if they don't already exist.
|
static ArrayList<String> |
arrayToList(String... stringArray)
Retrieves an array list containing the contents of the provided array.
|
static void |
byteArrayToHexPlusAscii(StringBuilder buffer,
byte[] b,
int indent)
Appends a string representation of the provided byte array to the given
buffer using the specified indent.
|
static void |
byteArrayToHexPlusAscii(StringBuilder buffer,
ByteBuffer b,
int indent)
Appends a string representation of the remaining unread data in the
provided byte buffer to the given buffer using the specified indent.
|
static String |
bytesToColonDelimitedHex(byte[] b)
Retrieves a string representation of the contents of the provided byte
array using hexadecimal characters and a colon between each byte.
|
static String |
bytesToHex(byte[] b)
Retrieves a string representation of the contents of the provided byte
array using hexadecimal characters and a space between each byte.
|
static String |
bytesToHex(ByteBuffer b)
Retrieves a string representation of the contents of the provided byte
buffer using hexadecimal characters and a space between each byte.
|
static String |
bytesToHex(org.forgerock.opendj.ldap.ByteSequence b)
Retrieves a string representation of the contents of the provided byte
sequence using hexadecimal characters and a space between each byte.
|
static String |
bytesToHexNoSpace(byte[] b)
Retrieves a string representation of the contents of the provided byte
array using hexadecimal characters with no space between each byte.
|
static String |
byteToHex(byte b)
Retrieves a string representation of the provided byte in hexadecimal.
|
static String |
byteToLowerHex(byte b)
Retrieves a string representation of the provided byte in hexadecimal.
|
static void |
checkOnlyOneArgPresent(com.forgerock.opendj.cli.Argument... args)
Checks that no more that one of a set of arguments is present.
|
static void |
close(Closeable... closeables)
Closes the provided
Closeable 's ignoring any errors which
occurred. |
static void |
close(Collection<? extends Closeable> closeables)
Closes the provided
Closeable 's ignoring any errors which occurred. |
static void |
close(InitialContext... ctxs)
Closes the provided
InitialContext 's ignoring any errors which occurred. |
static int |
compare(byte[] a,
byte[] a2)
Compare two byte arrays for order.
|
static Entry |
createEntry(DN dn)
Creates a new, blank entry with the given DN.
|
static String |
decodeUTF8(byte[] bytes)
Returns the provided byte array decoded as a UTF-8 string without throwing
an UnsupportedEncodingException.
|
static int |
exec(String command,
String[] args,
File workingDirectory,
Map<String,String> environment,
List<String> output)
Executes the specified command on the system and captures its output.
|
static String |
formatDateTimeString(Date date)
Formats a Date to String representation in "yyyyMMddHHmmss'Z'".
|
static String |
getBacktrace()
Retrieves a backtrace for the current thread consisting only of filenames
and line numbers that may be useful in debugging the origin of problems
that should not have happened.
|
static String |
getBacktrace(Throwable t)
Retrieves a backtrace for the provided exception consisting of only
filenames and line numbers that may be useful in debugging the origin of
problems.
|
static byte[] |
getBytes(String s)
Construct a byte array containing the UTF-8 encoding of the
provided string.
|
static LocalizableMessage |
getExceptionMessage(Throwable t)
Retrieves the best human-readable message for the provided exception.
|
static File |
getFileForPath(String path)
Retrieves a
File object corresponding to the specified path. |
static File |
getFileForPath(String path,
ServerContext serverContext)
Retrieves a
File object corresponding to the specified path. |
static boolean |
hasDescriptor(LocalizableMessage msg,
LocalizableMessageDescriptor.Arg0 desc)
Test if the provided message corresponds to the provided descriptor.
|
static boolean |
hasDescriptor(LocalizableMessage msg,
LocalizableMessageDescriptor.Arg1 desc)
Test if the provided message corresponds to the provided descriptor.
|
static boolean |
hasDescriptor(LocalizableMessage msg,
LocalizableMessageDescriptor.Arg2 desc)
Test if the provided message corresponds to the provided descriptor.
|
static boolean |
hasDescriptor(LocalizableMessage msg,
LocalizableMessageDescriptor.Arg3 desc)
Test if the provided message corresponds to the provided descriptor.
|
static boolean |
hasDescriptor(LocalizableMessage msg,
LocalizableMessageDescriptor.Arg7 desc)
Test if the provided message corresponds to the provided descriptor.
|
static byte[] |
hexStringToByteArray(String hexString)
Converts the provided hexadecimal string to a byte array.
|
static boolean |
isAddressInUse(InetAddress address,
int port,
boolean allowReuse)
Indicates whether the provided TCP address is already in use.
|
static boolean |
isAlpha(char c)
Indicates whether the provided character is an ASCII alphabetic character.
|
static boolean |
isClassAvailable(String className)
Returns true if the class is available in the classpath.
|
static boolean |
isDigit(char c)
Indicates whether the provided character is a numeric digit.
|
static boolean |
isEmailAddress(String addr)
Indicates whether or not a string represents a syntactically correct
email address.
|
static boolean |
isHexDigit(byte b)
Indicates whether the provided byte represents a hexadecimal digit.
|
static boolean |
isHexDigit(char c)
Indicates whether the provided character is a hexadecimal digit.
|
static boolean |
isOEMVersion()
Returns true if the version of the server is an OEM one, and therefore doesn't support the JE backend.
|
static boolean |
isRelativePath(String path)
Indicates whether the provided path refers to a relative path rather than
an absolute path.
|
static boolean |
isValidSchemaElement(String element,
int startPos,
int endPos,
LocalizableMessageBuilder invalidReason)
Indicates whether the provided string contains a name or OID for a schema
element like an attribute type or objectclass.
|
static boolean |
listsAreEqual(List<?> list1,
List<?> list2)
Indicates whether the two array lists are equal.
|
static String[] |
listToArray(List<String> stringList)
Retrieves a string array containing the contents of the provided
list of strings.
|
static boolean |
mayUseExec()
Indicates whether the use of the exec method will be allowed on this
system.
|
static void |
moveFile(File fileToMove,
File targetDirectory)
Moves the indicated file to the specified directory by creating a new file
in the target directory, copying the contents of the existing file, and
removing the existing file.
|
static boolean |
needsBase64Encoding(org.forgerock.opendj.ldap.ByteSequence valueBytes)
Indicates whether the provided value needs to be base64-encoded if it is
represented in LDIF form.
|
static boolean |
needsBase64Encoding(String valueString)
Indicates whether the provided value needs to be base64-encoded if it is
represented in LDIF form.
|
static Date |
parseDateTimeString(String timeStr)
Converts a string representing a time in "yyyyMMddHHmmss.SSS'Z'" or
"yyyyMMddHHmmss" to a
Date . |
static boolean |
recursiveDelete(File file)
Attempts to delete the specified file or directory.
|
static void |
renameFile(File fileToRename,
File target)
Renames the source file to the target file.
|
static LocalizableMessage |
secondsToTimeString(long numSeconds)
Retrieves a user-friendly string that indicates the length of time (in
days, hours, minutes, and seconds) in the specified number of seconds.
|
static void |
sleep(long millis)
Calls
Thread.sleep(long) , surrounding it with the mandatory
try / catch(InterruptedException) block. |
static boolean |
stackTraceContainsCause(Throwable throwable,
Class<? extends Throwable> searchedCause)
Check if the stack trace of provided exception contains a given cause.
|
static void |
stackTraceToSingleLineString(StringBuilder buffer,
Throwable t)
Appends a single-line string representation of the provided exception to
the given buffer.
|
static String |
stackTraceToSingleLineString(Throwable t)
Retrieves a stack trace from the provided exception as a single-line
string.
|
static void |
stackTraceToString(StringBuilder buffer,
Throwable t)
Appends a string representation of the stack trace for the provided
exception to the given buffer.
|
static String |
stackTraceToString(Throwable t)
Retrieves a string representation of the stack trace for the provided
exception.
|
static <T> Iterable<T> |
toIterable(Iterator<T> iterator)
|
static void |
toLowerCase(org.forgerock.opendj.ldap.ByteSequence b,
StringBuilder buffer,
boolean trim)
Appends a lower-case string representation of a given ByteSequence to a StringBuilder,
verifying for null input.
|
static String |
toLowerCase(String s)
Returns a lower-case string representation of a given string, verifying for null input string.
|
static StringBuilder |
toRFC3641StringValue(StringBuilder builder,
String string)
Append a string to a string builder, escaping any double quotes
according to the StringValue production in RFC 3641.
|
static void |
toUpperCase(byte[] b,
StringBuilder buffer,
boolean trim)
Appends an uppercase string representation of the contents of the given
byte array to the provided buffer, optionally trimming leading and trailing
spaces.
|
static String |
toUpperCase(String s)
Retrieves an uppercase representation of the given string.
|
static void |
toUpperCase(String s,
StringBuilder buffer)
Appends an uppercase representation of the given string to the provided
buffer.
|
static boolean |
writeWithTimeout(ClientConnection clientConnection,
ByteBuffer buffer)
Writes the contents of the provided buffer to the client,
terminating the connection if the write is unsuccessful for too
long (e.g., if the client is unresponsive or there is a network
problem).
|
public static final int INT_SIZE
public static final int LONG_SIZE
public static final int KB
Example usage:
int _10KB = 10 * KB;
public static final int MB
Example usage:
int _10MB = 10 * MB;
public static byte[] getBytes(String s)
String.getBytes(String)
for ASCII strings.s
- The string to convert to a UTF-8 byte array.public static String decodeUTF8(byte[] bytes)
try { return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException e) { // Should never happen: UTF-8 is always supported. throw new RuntimeException(e); }
bytes
- The byte array to be decoded as a UTF-8 string.public static String byteToHex(byte b)
b
- The byte for which to retrieve the hexadecimal string
representation.public static String byteToLowerHex(byte b)
b
- The byte for which to retrieve the hexadecimal string
representation.public static String bytesToHexNoSpace(byte[] b)
b
- The byte array containing the data.public static String bytesToHex(byte[] b)
b
- The byte array containing the data.public static String bytesToHex(org.forgerock.opendj.ldap.ByteSequence b)
b
- The byte sequence containing the data.public static String bytesToColonDelimitedHex(byte[] b)
b
- The byte array containing the data.public static String bytesToHex(ByteBuffer b)
b
- The byte buffer containing the data.public static void byteArrayToHexPlusAscii(StringBuilder buffer, byte[] b, int indent)
buffer
- The buffer to which the information is to be appended.b
- The byte array containing the data to write.indent
- The number of spaces to indent the output.public static void byteArrayToHexPlusAscii(StringBuilder buffer, ByteBuffer b, int indent)
buffer
- The buffer to which the information is to be appended.b
- The byte buffer containing the data to write.
The data from the position to the limit is written.indent
- The number of spaces to indent the output.public static int compare(byte[] a, byte[] a2)
a
- The first byte array to be compared.a2
- The second byte array to be compared.public static boolean listsAreEqual(List<?> list1, List<?> list2)
list1
- The first list for which to make the determination.list2
- The second list for which to make the determination.true
if the two array lists are equal, or
false
if they are not.public static LocalizableMessage getExceptionMessage(Throwable t)
t
- The Throwable
object for which to retrieve the message.public static String stackTraceToSingleLineString(Throwable t)
t
- The exception for which to retrieve the stack trace.public static void stackTraceToSingleLineString(StringBuilder buffer, Throwable t)
buffer
- The buffer to which the information is to be appended.t
- The exception for which to retrieve the stack trace.public static String stackTraceToString(Throwable t)
t
- The exception for which to retrieve the stack trace.public static boolean stackTraceContainsCause(Throwable throwable, Class<? extends Throwable> searchedCause)
throwable
- exception that may contain the causesearchedCause
- class of the cause to look for. Any subclass will match.public static void stackTraceToString(StringBuilder buffer, Throwable t)
buffer
- The buffer to which the information is to be appended.t
- The exception for which to retrieve the stack trace.public static String getBacktrace()
public static String getBacktrace(Throwable t)
getBacktrace
without any arguments if an exception has already
been thrown.t
- The exception for which to obtain the backtrace.public static boolean isDigit(char c)
c
- The character for which to make the determination.true
if the provided character represents a numeric
digit, or false
if not.public static boolean isAlpha(char c)
c
- The character for which to make the determination.true
if the provided value is an uppercase or
lowercase ASCII alphabetic character, or false
if it
is not.public static boolean isHexDigit(char c)
c
- The character for which to make the determination.true
if the provided character represents a
hexadecimal digit, or false
if not.public static boolean isHexDigit(byte b)
b
- The byte for which to make the determination.true
if the provided byte represents a hexadecimal
digit, or false
if not.public static byte[] hexStringToByteArray(String hexString) throws ParseException
hexString
- The hexadecimal string to convert to a byte array.ParseException
- If the provided string contains invalid
hexadecimal digits or does not contain an even
number of digits.public static boolean needsBase64Encoding(org.forgerock.opendj.ldap.ByteSequence valueBytes)
valueBytes
- The binary representation of the attribute value for
which to make the determination.true
if the value needs to be base64-encoded if it is
represented in LDIF form, or false
if not.public static boolean needsBase64Encoding(String valueString)
valueString
- The string representation of the attribute value for
which to make the determination.true
if the value needs to be base64-encoded if it is
represented in LDIF form, or false
if not.public static boolean mayUseExec()
true
if the use of the exec method should be allowed,
or false
if it should not be allowed.public static int exec(String command, String[] args, File workingDirectory, Map<String,String> environment, List<String> output) throws IOException, SecurityException, InterruptedException
command
- The command to execute.args
- The set of arguments to provide to the command.workingDirectory
- The working directory to use for the command, or
null
if the default directory
should be used.environment
- The set of environment variables that should be
set when executing the command, or
null
if none are needed.output
- The output generated by the command while it was
running. This will include both standard
output and standard error. It may be
null
if the output does not need to
be captured.IOException
- If an I/O problem occurs while trying to execute the
command.SecurityException
- If the security policy will not allow the
command to be executed.InterruptedException
- If the current thread is interrupted by
another thread while it is waiting, then
the wait is ended and an InterruptedException
is thrown.public static boolean isValidSchemaElement(String element, int startPos, int endPos, LocalizableMessageBuilder invalidReason)
element
- The string containing the substring for which to
make the determination.startPos
- The position of the first character that is to be
checked.endPos
- The position of the first character after the start
position that is not to be checked.invalidReason
- The buffer to which the invalid reason is to be
appended if a problem is found.true
if the provided string contains a valid name or
OID for a schema element, or false
if it does not.public static boolean isAddressInUse(InetAddress address, int port, boolean allowReuse)
address
- IP address of the TCP address for which to make
the determination.port
- TCP port number of the TCP address for which to
make the determination.allowReuse
- Whether or not TCP address reuse is allowed when
making the determination.true
if the provided TCP address is already in
use, or false
otherwise.public static String toLowerCase(String s)
StaticUtils.toLowerCase(String s)
s
- the mixed case stringpublic static void toLowerCase(org.forgerock.opendj.ldap.ByteSequence b, StringBuilder buffer, boolean trim)
StaticUtils.toLowerCase(ByteSequence s, StringBuilder string)
b
- The byte array for which to obtain the lowercase string
representation.buffer
- The buffer to which the lowercase form of the string should
be appended.trim
- Indicates whether leading and trailing spaces should be
omitted from the string representation.public static String toUpperCase(String s)
s
- The string for which to obtain the uppercase representation.public static void toUpperCase(String s, StringBuilder buffer)
s
- The string for which to obtain the uppercase
representation.buffer
- The buffer to which the uppercase form of the string should
be appended.public static void toUpperCase(byte[] b, StringBuilder buffer, boolean trim)
b
- The byte array for which to obtain the uppercase string
representation.buffer
- The buffer to which the uppercase form of the string should
be appended.trim
- Indicates whether leading and trailing spaces should be
omitted from the string representation.public static StringBuilder toRFC3641StringValue(StringBuilder builder, String string)
In RFC 3641 the StringValue production looks like this:
StringValue = dquote *SafeUTF8Character dquote dquote = %x22 ; " (double quote) SafeUTF8Character = %x00-21 / %x23-7F / ; ASCII minus dquote dquote dquote / ; escaped double quote %xC0-DF %x80-BF / ; 2 byte UTF-8 character %xE0-EF 2(%x80-BF) / ; 3 byte UTF-8 character %xF0-F7 3(%x80-BF) ; 4 byte UTF-8 character
That is, strings are surrounded by double-quotes and any internal double-quotes are doubled up.
builder
- The string builder.string
- The string to escape and append.public static String[] listToArray(List<String> stringList)
stringList
- The string list to convert to an array.public static ArrayList<String> arrayToList(String... stringArray)
stringArray
- The string array to convert to an array list.public static boolean recursiveDelete(File file)
file
- The file or directory to be removed.true
if the specified file and any subordinates are all
successfully removed, or false
if at least one element in
the subtree could not be removed or file does not exists.public static void moveFile(File fileToMove, File targetDirectory) throws IOException
fileToMove
- The file to move to the target directory.targetDirectory
- The directory into which the file should be moved.IOException
- If a problem occurs while attempting to move the
file.public static void renameFile(File fileToRename, File target) throws IOException
fileToRename
- The file to rename.target
- The file to which fileToRename
will be
moved.IOException
- If a problem occurs while attempting to rename the
file. On the Windows platform, this typically
indicates that the file is in use by this or another
application.public static boolean isRelativePath(String path)
path
- The path string for which to make the determination.true
if the provided path is relative, or
false
if it is absolute.public static File getFileForPath(String path)
File
object corresponding to the specified path.
If the given path is an absolute path, then it will be used. If the path
is relative, then it will be interpreted as if it were relative to the
Directory Server root.path
- The path string to be retrieved as a File
File
object that corresponds to the specified path.public static File getFileForPath(String path, ServerContext serverContext)
File
object corresponding to the specified path.
If the given path is an absolute path, then it will be used. If the path
is relative, then it will be interpreted as if it were relative to the
Directory Server root.path
- The path string to be retrieved as a File
.serverContext
- The server context.File
object that corresponds to the specified path.public static Entry createEntry(DN dn)
dn
- The DN to use for the entry.public static LocalizableMessage secondsToTimeString(long numSeconds)
numSeconds
- The number of seconds to be converted to a more
user-friendly value.public static void checkOnlyOneArgPresent(com.forgerock.opendj.cli.Argument... args) throws com.forgerock.opendj.cli.ArgumentException
args
- to test for the presence of more than onecom.forgerock.opendj.cli.ArgumentException
- if more than one of args
is
present and containing an error message identifying the
arguments in violationpublic static Date parseDateTimeString(String timeStr) throws ParseException
Date
.timeStr
- string formatted appropriatelytimeStr
is nullParseException
- if there was a problem converting the string to
a Date
.public static String formatDateTimeString(Date date)
date
- to format; null if date
is nullpublic static boolean isEmailAddress(String addr)
addr
- to validatetrue
indicates that the string is a
syntactically correct email addresspublic static boolean writeWithTimeout(ClientConnection clientConnection, ByteBuffer buffer) throws IOException
ClientConnection.getWriteSelector
method, but it is
capable of working even if that method returns null
. clientConnection
- The client connection to which the data is to be written.buffer
- The data to be written to the client.true
if all the data in the provided buffer was
written to the client and the connection may remain
established, or false
if a problem occurred
and the client connection is no longer valid. Note that if
this method does return false
, then it must
have already disconnected the client.IOException
- If a problem occurs while attempting to write data to the
client. The caller will be responsible for catching this
and terminating the client connection.public static void addSuperiorObjectClasses(Map<ObjectClass,String> objectClasses)
objectClasses
- A Map of objectclasses.public static void close(Closeable... closeables)
Closeable
's ignoring any errors which
occurred.closeables
- The closeables to be closed, which may be
null
.public static void close(Collection<? extends Closeable> closeables)
Closeable
's ignoring any errors which occurred.closeables
- The closeables to be closed, which may be null
.public static void close(InitialContext... ctxs)
InitialContext
's ignoring any errors which occurred.ctxs
- The contexts to be closed, which may be null
.public static void sleep(long millis)
Thread.sleep(long)
, surrounding it with the mandatory
try
/ catch(InterruptedException)
block.millis
- the length of time to sleep in millisecondspublic static boolean hasDescriptor(LocalizableMessage msg, LocalizableMessageDescriptor.Arg0 desc)
msg
- The i18n message.desc
- The message descriptor.true
if message corresponds to descriptorpublic static boolean hasDescriptor(LocalizableMessage msg, LocalizableMessageDescriptor.Arg1 desc)
msg
- The i18n message.desc
- The message descriptor.true
if message corresponds to descriptorpublic static boolean hasDescriptor(LocalizableMessage msg, LocalizableMessageDescriptor.Arg2 desc)
msg
- The i18n message.desc
- The message descriptor.true
if message corresponds to descriptorpublic static boolean hasDescriptor(LocalizableMessage msg, LocalizableMessageDescriptor.Arg3 desc)
msg
- The i18n message.desc
- The message descriptor.true
if message corresponds to descriptorpublic static boolean hasDescriptor(LocalizableMessage msg, LocalizableMessageDescriptor.Arg7 desc)
msg
- The i18n message.desc
- The message descriptor.true
if message corresponds to descriptorpublic static <T> Iterable<T> toIterable(Iterator<T> iterator)
Iterable
returning the passed in Iterator
. THis
allows using methods returning Iterators with foreach statements.
For example, consider a method with this signature:
public Iterator<String> myIteratorMethod();
Classical use with for or while loop:
for (Iterator<String> it = myIteratorMethod(); it.hasNext();) { String s = it.next(); // use it } Iterator<String> it = myIteratorMethod(); while(it.hasNext();) { String s = it.next(); // use it }Improved use with foreach:
for (String s : StaticUtils.toIterable(myIteratorMethod())) { }
T
- the generic type of the passed in Iterator and for the returned
Iterable.iterator
- the Iterator that will be returned by the Iterable.public static boolean isOEMVersion()
true
if the version of the server is an OEM version and false
otherwise.public static boolean isClassAvailable(String className)
className
- the string representing the class to check.true
if the class is available in the classpath and false
otherwise.Copyright © 2010-2016 ForgeRock AS. All Rights Reserved.