/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt * or http://forgerock.org/license/CDDLv1.0.html. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at legal-notices/CDDLv1_0.txt. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: * Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END * * * Copyright 2006-2010 Sun Microsystems, Inc. * Portions Copyright 2011-2016 ForgeRock AS */ package org.opends.server.util; import static org.opends.messages.UtilityMessages.*; import static org.opends.server.util.ServerConstants.*; import java.io.BufferedReader; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.RandomAccess; import java.util.TimeZone; import javax.naming.InitialContext; import javax.naming.NamingException; import org.forgerock.i18n.LocalizableMessage; import org.forgerock.i18n.LocalizableMessageBuilder; import org.forgerock.i18n.LocalizableMessageDescriptor; import org.forgerock.i18n.slf4j.LocalizedLogger; import org.forgerock.opendj.ldap.AVA; import org.forgerock.opendj.ldap.ByteSequence; import org.forgerock.opendj.ldap.ByteString; import org.forgerock.opendj.ldap.schema.AttributeType; import org.forgerock.util.Reject; import org.opends.messages.ToolMessages; import org.opends.server.api.ClientConnection; import org.opends.server.core.DirectoryServer; import org.opends.server.core.ServerContext; import org.opends.server.types.Attribute; import org.opends.server.types.AttributeBuilder; import org.opends.server.types.DN; import org.opends.server.types.Entry; import org.opends.server.types.IdentifiedException; import org.opends.server.types.ObjectClass; import org.opends.server.types.RDN; import com.forgerock.opendj.cli.Argument; import com.forgerock.opendj.cli.ArgumentException; /** * This class defines a number of static utility methods that may be used * throughout the server. Note that because of the frequency with which these * methods are expected to be used, very little debug logging will be performed * to prevent the log from filling up with unimportant calls and to reduce the * impact that debugging may have on performance. */ @org.opends.server.types.PublicAPI( stability=org.opends.server.types.StabilityLevel.UNCOMMITTED, mayInstantiate=false, mayExtend=false, mayInvoke=true) public final class StaticUtils { private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); /** The number of bytes of a Java int. A Java int is 32 bits, i.e. 4 bytes. */ public static final int INT_SIZE = 4; /** The number of bytes of a Java long. A Java int is 64 bits, i.e. 8 bytes. */ public static final int LONG_SIZE = 8; /** * Number of bytes in a Kibibyte. *
* Example usage: *
* int _10KB = 10 * KB; **/ public static final int KB = 1024; /** * Number of bytes in a Mebibyte. *
* Example usage: *
* int _10MB = 10 * MB; **/ public static final int MB = KB * KB; /** Private constructor to prevent instantiation. */ private StaticUtils() { // No implementation required. } /** * Construct a byte array containing the UTF-8 encoding of the * provided string. This is significantly faster * than calling {@link String#getBytes(String)} for ASCII strings. * * @param s * The string to convert to a UTF-8 byte array. * @return Returns a byte array containing the UTF-8 encoding of the * provided string. */ public static byte[] getBytes(String s) { return com.forgerock.opendj.util.StaticUtils.getBytes(s); } /** * Returns the provided byte array decoded as a UTF-8 string without throwing * an UnsupportedEncodingException. This method is equivalent to: * *
* try
* {
* return new String(bytes, "UTF-8");
* }
* catch (UnsupportedEncodingException e)
* {
* // Should never happen: UTF-8 is always supported.
* throw new RuntimeException(e);
* }
*
*
* @param bytes
* The byte array to be decoded as a UTF-8 string.
* @return The decoded string.
*/
public static String decodeUTF8(final byte[] bytes)
{
Reject.ifNull(bytes);
if (bytes.length == 0)
{
return "".intern();
}
final StringBuilder builder = new StringBuilder(bytes.length);
final int sz = bytes.length;
for (int i = 0; i < sz; i++)
{
final byte b = bytes[i];
if ((b & 0x7f) != b)
{
try
{
builder.append(new String(bytes, i, (sz - i), "UTF-8"));
}
catch (UnsupportedEncodingException e)
{
// Should never happen: UTF-8 is always supported.
throw new RuntimeException(e);
}
break;
}
builder.append((char) b);
}
return builder.toString();
}
/**
* Retrieves a string representation of the provided byte in hexadecimal.
*
* @param b The byte for which to retrieve the hexadecimal string
* representation.
* @return The string representation of the provided byte in hexadecimal.
*/
public static String byteToHex(final byte b)
{
return com.forgerock.opendj.util.StaticUtils.byteToHex(b);
}
/**
* Retrieves a string representation of the provided byte in hexadecimal.
*
* @param b The byte for which to retrieve the hexadecimal string
* representation.
* @return The string representation of the provided byte in hexadecimal
* using lowercase characters.
*/
public static String byteToLowerHex(final byte b)
{
return com.forgerock.opendj.util.StaticUtils.byteToLowerHex(b);
}
/**
* Retrieves a string representation of the contents of the provided byte
* array using hexadecimal characters with no space between each byte.
*
* @param b The byte array containing the data.
*
* @return A string representation of the contents of the provided byte
* array using hexadecimal characters.
*/
public static String bytesToHexNoSpace(byte[] b)
{
if (b == null || b.length == 0)
{
return "";
}
int arrayLength = b.length;
StringBuilder buffer = new StringBuilder(arrayLength * 2);
for (int i=0; i < arrayLength; i++)
{
buffer.append(byteToHex(b[i]));
}
return buffer.toString();
}
/**
* Retrieves a string representation of the contents of the provided byte
* array using hexadecimal characters and a space between each byte.
*
* @param b The byte array containing the data.
* @return A string representation of the contents of the provided byte
* array using hexadecimal characters.
*/
public static String bytesToHex(byte[] b)
{
if (b == null || b.length == 0)
{
return "";
}
int arrayLength = b.length;
StringBuilder buffer = new StringBuilder((arrayLength - 1) * 3 + 2);
buffer.append(byteToHex(b[0]));
for (int i=1; i < arrayLength; i++)
{
buffer.append(" ");
buffer.append(byteToHex(b[i]));
}
return buffer.toString();
}
/**
* Retrieves a string representation of the contents of the provided byte
* sequence using hexadecimal characters and a space between each byte.
*
* @param b The byte sequence containing the data.
* @return A string representation of the contents of the provided byte
* sequence using hexadecimal characters.
*/
public static String bytesToHex(ByteSequence b)
{
if (b == null || b.length() == 0)
{
return "";
}
int arrayLength = b.length();
StringBuilder buffer = new StringBuilder((arrayLength - 1) * 3 + 2);
buffer.append(byteToHex(b.byteAt(0)));
for (int i=1; i < arrayLength; i++)
{
buffer.append(" ");
buffer.append(byteToHex(b.byteAt(i)));
}
return buffer.toString();
}
/**
* Retrieves a string representation of the contents of the provided byte
* array using hexadecimal characters and a colon between each byte.
*
* @param b The byte array containing the data.
*
* @return A string representation of the contents of the provided byte
* array using hexadecimal characters.
*/
public static String bytesToColonDelimitedHex(byte[] b)
{
if (b == null || b.length == 0)
{
return "";
}
int arrayLength = b.length;
StringBuilder buffer = new StringBuilder((arrayLength - 1) * 3 + 2);
buffer.append(byteToHex(b[0]));
for (int i=1; i < arrayLength; i++)
{
buffer.append(":");
buffer.append(byteToHex(b[i]));
}
return buffer.toString();
}
/**
* Retrieves a string representation of the contents of the provided byte
* buffer using hexadecimal characters and a space between each byte.
*
* @param b The byte buffer containing the data.
*
* @return A string representation of the contents of the provided byte
* buffer using hexadecimal characters.
*/
public static String bytesToHex(ByteBuffer b)
{
if (b == null)
{
return "";
}
int position = b.position();
int limit = b.limit();
int length = limit - position;
if (length == 0)
{
return "";
}
StringBuilder buffer = new StringBuilder((length - 1) * 3 + 2);
buffer.append(byteToHex(b.get()));
for (int i=1; i < length; i++)
{
buffer.append(" ");
buffer.append(byteToHex(b.get()));
}
b.position(position);
b.limit(limit);
return buffer.toString();
}
/**
* Appends a string representation of the provided byte array to the given
* buffer using the specified indent. The data will be formatted with sixteen
* hex bytes in a row followed by the ASCII representation, then wrapping to a
* new line as necessary.
*
* @param buffer The buffer to which the information is to be appended.
* @param b The byte array containing the data to write.
* @param indent The number of spaces to indent the output.
*/
public static void byteArrayToHexPlusAscii(StringBuilder buffer, byte[] b,
int indent)
{
StringBuilder indentBuf = new StringBuilder(indent);
for (int i=0 ; i < indent; i++)
{
indentBuf.append(' ');
}
int length = b.length;
int pos = 0;
while (length - pos >= 16)
{
StringBuilder asciiBuf = new StringBuilder(17);
buffer.append(indentBuf);
buffer.append(byteToHex(b[pos]));
asciiBuf.append(byteToASCII(b[pos]));
pos++;
for (int i=1; i < 16; i++, pos++)
{
buffer.append(' ');
buffer.append(byteToHex(b[pos]));
asciiBuf.append(byteToASCII(b[pos]));
if (i == 7)
{
buffer.append(" ");
asciiBuf.append(' ');
}
}
buffer.append(" ");
buffer.append(asciiBuf);
buffer.append(EOL);
}
int remaining = length - pos;
if (remaining > 0)
{
StringBuilder asciiBuf = new StringBuilder(remaining+1);
buffer.append(indentBuf);
buffer.append(byteToHex(b[pos]));
asciiBuf.append(byteToASCII(b[pos]));
pos++;
for (int i=1; i < 16; i++)
{
buffer.append(' ');
if (i < remaining)
{
buffer.append(byteToHex(b[pos]));
asciiBuf.append(byteToASCII(b[pos]));
pos++;
}
else
{
buffer.append(" ");
}
if (i == 7)
{
buffer.append(" ");
if (i < remaining)
{
asciiBuf.append(' ');
}
}
}
buffer.append(" ");
buffer.append(asciiBuf);
buffer.append(EOL);
}
}
private static char byteToASCII(byte b)
{
return com.forgerock.opendj.util.StaticUtils.byteToASCII(b);
}
/**
* Appends a string representation of the remaining unread data in the
* provided byte buffer to the given buffer using the specified indent.
* The data will be formatted with sixteen hex bytes in a row followed by
* the ASCII representation, then wrapping to a new line as necessary.
* The state of the byte buffer is not changed.
*
* @param buffer The buffer to which the information is to be appended.
* @param b The byte buffer containing the data to write.
* The data from the position to the limit is written.
* @param indent The number of spaces to indent the output.
*/
public static void byteArrayToHexPlusAscii(StringBuilder buffer, ByteBuffer b,
int indent)
{
StringBuilder indentBuf = new StringBuilder(indent);
for (int i=0 ; i < indent; i++)
{
indentBuf.append(' ');
}
int position = b.position();
int limit = b.limit();
int length = limit - position;
int pos = 0;
while (length - pos >= 16)
{
StringBuilder asciiBuf = new StringBuilder(17);
byte currentByte = b.get();
buffer.append(indentBuf);
buffer.append(byteToHex(currentByte));
asciiBuf.append(byteToASCII(currentByte));
pos++;
for (int i=1; i < 16; i++, pos++)
{
currentByte = b.get();
buffer.append(' ');
buffer.append(byteToHex(currentByte));
asciiBuf.append(byteToASCII(currentByte));
if (i == 7)
{
buffer.append(" ");
asciiBuf.append(' ');
}
}
buffer.append(" ");
buffer.append(asciiBuf);
buffer.append(EOL);
}
int remaining = length - pos;
if (remaining > 0)
{
StringBuilder asciiBuf = new StringBuilder(remaining+1);
byte currentByte = b.get();
buffer.append(indentBuf);
buffer.append(byteToHex(currentByte));
asciiBuf.append(byteToASCII(currentByte));
for (int i=1; i < 16; i++)
{
buffer.append(' ');
if (i < remaining)
{
currentByte = b.get();
buffer.append(byteToHex(currentByte));
asciiBuf.append(byteToASCII(currentByte));
}
else
{
buffer.append(" ");
}
if (i == 7)
{
buffer.append(" ");
if (i < remaining)
{
asciiBuf.append(' ');
}
}
}
buffer.append(" ");
buffer.append(asciiBuf);
buffer.append(EOL);
}
b.position(position);
b.limit(limit);
}
/**
* Compare two byte arrays for order. Returns a negative integer,
* zero, or a positive integer as the first argument is less than,
* equal to, or greater than the second.
*
* @param a
* The first byte array to be compared.
* @param a2
* The second byte array to be compared.
* @return Returns a negative integer, zero, or a positive integer
* if the first byte array is less than, equal to, or greater
* than the second.
*/
public static int compare(byte[] a, byte[] a2) {
if (a == a2) {
return 0;
}
if (a == null) {
return -1;
}
if (a2 == null) {
return 1;
}
int minLength = Math.min(a.length, a2.length);
for (int i = 0; i < minLength; i++) {
int firstByte = 0xFF & a[i];
int secondByte = 0xFF & a2[i];
if (firstByte != secondByte) {
if (firstByte < secondByte) {
return -1;
} else if (firstByte > secondByte) {
return 1;
}
}
}
return a.length - a2.length;
}
/**
* Indicates whether the two array lists are equal. They will be
* considered equal if they have the same number of elements, and
* the corresponding elements between them are equal (in the same
* order).
*
* @param list1
* The first list for which to make the determination.
* @param list2
* The second list for which to make the determination.
* @return {@code true} if the two array lists are equal, or
* {@code false} if they are not.
*/
public static boolean listsAreEqual(List> list1, List> list2)
{
if (list1 == null)
{
return list2 == null;
}
else if (list2 == null)
{
return false;
}
int numElements = list1.size();
if (numElements != list2.size())
{
return false;
}
// If either of the lists doesn't support random access, then fall back
// on their equals methods and go ahead and create some garbage with the
// iterators.
if (!(list1 instanceof RandomAccess) ||
!(list2 instanceof RandomAccess))
{
return list1.equals(list2);
}
// Otherwise we can just retrieve the elements efficiently via their index.
for (int i=0; i < numElements; i++)
{
Object o1 = list1.get(i);
Object o2 = list2.get(i);
if (o1 == null)
{
if (o2 != null)
{
return false;
}
}
else if (! o1.equals(o2))
{
return false;
}
}
return true;
}
/**
* Retrieves the best human-readable message for the provided exception. For
* exceptions defined in the OpenDJ project, it will attempt to use the
* message (combining it with the message ID if available). For some
* exceptions that use encapsulation (e.g., InvocationTargetException), it
* will be unwrapped and the cause will be treated. For all others, the
*
*
* @param t The {@code Throwable} object for which to retrieve the message.
*
* @return The human-readable message generated for the provided exception.
*/
public static LocalizableMessage getExceptionMessage(Throwable t)
{
if (t instanceof IdentifiedException)
{
IdentifiedException ie = (IdentifiedException) t;
StringBuilder message = new StringBuilder();
message.append(ie.getMessage());
message.append(" (id=");
LocalizableMessage ieMsg = ie.getMessageObject();
if (ieMsg != null) {
message.append(ieMsg.resourceName()).append("-").append(ieMsg.ordinal());
} else {
message.append("-1");
}
message.append(")");
return LocalizableMessage.raw(message.toString());
}
else
{
return com.forgerock.opendj.util.StaticUtils.getExceptionMessage(t);
}
}
/**
* Retrieves a stack trace from the provided exception as a single-line
* string.
*
* @param t The exception for which to retrieve the stack trace.
*
* @return A stack trace from the provided exception as a single-line string.
*/
public static String stackTraceToSingleLineString(Throwable t)
{
return com.forgerock.opendj.util.StaticUtils.stackTraceToSingleLineString(t, DynamicConstants.DEBUG_BUILD);
}
/**
* Appends a single-line string representation of the provided exception to
* the given buffer.
*
* @param buffer The buffer to which the information is to be appended.
* @param t The exception for which to retrieve the stack trace.
*/
public static void stackTraceToSingleLineString(StringBuilder buffer,
Throwable t)
{
com.forgerock.opendj.util.StaticUtils.stackTraceToSingleLineString(buffer, t, DynamicConstants.DEBUG_BUILD);
}
/**
* Retrieves a string representation of the stack trace for the provided
* exception.
*
* @param t The exception for which to retrieve the stack trace.
*
* @return A string representation of the stack trace for the provided
* exception.
*/
public static String stackTraceToString(Throwable t)
{
StringBuilder buffer = new StringBuilder();
stackTraceToString(buffer, t);
return buffer.toString();
}
/**
* Check if the stack trace of provided exception contains a given cause.
*
* @param throwable
* exception that may contain the cause
* @param searchedCause
* class of the cause to look for. Any subclass will match.
* @return true if and only if the given cause is found as a cause of any
* level in the provided exception.
*/
public static boolean stackTraceContainsCause(
Throwable throwable, Class extends Throwable> searchedCause)
{
Throwable t = throwable;
while ((t = t.getCause()) != null)
{
if (searchedCause.isAssignableFrom(t.getClass()))
{
return true;
}
}
return false;
}
/**
* Appends a string representation of the stack trace for the provided
* exception to the given buffer.
*
* @param buffer The buffer to which the information is to be appended.
* @param t The exception for which to retrieve the stack trace.
*/
public static void stackTraceToString(StringBuilder buffer, Throwable t)
{
if (t == null)
{
return;
}
buffer.append(t);
for (StackTraceElement e : t.getStackTrace())
{
buffer.append(EOL);
buffer.append(" ");
buffer.append(e.getClassName());
buffer.append(".");
buffer.append(e.getMethodName());
buffer.append("(");
buffer.append(e.getFileName());
buffer.append(":");
buffer.append(e.getLineNumber());
buffer.append(")");
}
while (t.getCause() != null)
{
t = t.getCause();
buffer.append(EOL);
buffer.append("Caused by ");
buffer.append(t);
for (StackTraceElement e : t.getStackTrace())
{
buffer.append(EOL);
buffer.append(" ");
buffer.append(e.getClassName());
buffer.append(".");
buffer.append(e.getMethodName());
buffer.append("(");
buffer.append(e.getFileName());
buffer.append(":");
buffer.append(e.getLineNumber());
buffer.append(")");
}
}
buffer.append(EOL);
}
/**
* 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. Note that this may be an expensive
* operation to perform, so it should only be used for error conditions or
* debugging.
*
* @return A backtrace for the current thread.
*/
public static String getBacktrace()
{
StringBuilder buffer = new StringBuilder();
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
if (elements.length > 1)
{
buffer.append(elements[1].getFileName());
buffer.append(":");
buffer.append(elements[1].getLineNumber());
for (int i=2; i < elements.length; i++)
{
buffer.append(" ");
buffer.append(elements[i].getFileName());
buffer.append(":");
buffer.append(elements[i].getLineNumber());
}
}
return buffer.toString();
}
/**
* Retrieves a backtrace for the provided exception consisting of only
* filenames and line numbers that may be useful in debugging the origin of
* problems. This is less expensive than the call to
* {@code getBacktrace} without any arguments if an exception has already
* been thrown.
*
* @param t The exception for which to obtain the backtrace.
*
* @return A backtrace from the provided exception.
*/
public static String getBacktrace(Throwable t)
{
StringBuilder buffer = new StringBuilder();
StackTraceElement[] elements = t.getStackTrace();
if (elements.length > 0)
{
buffer.append(elements[0].getFileName());
buffer.append(":");
buffer.append(elements[0].getLineNumber());
for (int i=1; i < elements.length; i++)
{
buffer.append(" ");
buffer.append(elements[i].getFileName());
buffer.append(":");
buffer.append(elements[i].getLineNumber());
}
}
return buffer.toString();
}
/**
* Indicates whether the provided character is a numeric digit.
*
* @param c The character for which to make the determination.
*
* @return {@code true} if the provided character represents a numeric
* digit, or {@code false} if not.
*/
public static boolean isDigit(final char c) {
return com.forgerock.opendj.util.StaticUtils.isDigit(c);
}
/**
* Indicates whether the provided character is an ASCII alphabetic character.
*
* @param c The character for which to make the determination.
*
* @return {@code true} if the provided value is an uppercase or
* lowercase ASCII alphabetic character, or {@code false} if it
* is not.
*/
public static boolean isAlpha(final char c) {
return com.forgerock.opendj.util.StaticUtils.isAlpha(c);
}
/**
* Indicates whether the provided character is a hexadecimal digit.
*
* @param c The character for which to make the determination.
*
* @return {@code true} if the provided character represents a
* hexadecimal digit, or {@code false} if not.
*/
public static boolean isHexDigit(final char c) {
return com.forgerock.opendj.util.StaticUtils.isHexDigit(c);
}
/**
* Indicates whether the provided byte represents a hexadecimal digit.
*
* @param b The byte for which to make the determination.
*
* @return {@code true} if the provided byte represents a hexadecimal
* digit, or {@code false} if not.
*/
public static boolean isHexDigit(byte b)
{
switch (b)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
return true;
default:
return false;
}
}
/**
* Converts the provided hexadecimal string to a byte array.
*
* @param hexString The hexadecimal string to convert to a byte array.
*
* @return The byte array containing the binary representation of the
* provided hex string.
*
* @throws ParseException If the provided string contains invalid
* hexadecimal digits or does not contain an even
* number of digits.
*/
public static byte[] hexStringToByteArray(String hexString)
throws ParseException
{
int length;
if (hexString == null || ((length = hexString.length()) == 0))
{
return new byte[0];
}
if ((length % 2) == 1)
{
LocalizableMessage message = ERR_HEX_DECODE_INVALID_LENGTH.get(hexString);
throw new ParseException(message.toString(), 0);
}
int pos = 0;
int arrayLength = length / 2;
byte[] returnArray = new byte[arrayLength];
for (int i=0; i < arrayLength; i++)
{
switch (hexString.charAt(pos++))
{
case '0':
returnArray[i] = 0x00;
break;
case '1':
returnArray[i] = 0x10;
break;
case '2':
returnArray[i] = 0x20;
break;
case '3':
returnArray[i] = 0x30;
break;
case '4':
returnArray[i] = 0x40;
break;
case '5':
returnArray[i] = 0x50;
break;
case '6':
returnArray[i] = 0x60;
break;
case '7':
returnArray[i] = 0x70;
break;
case '8':
returnArray[i] = (byte) 0x80;
break;
case '9':
returnArray[i] = (byte) 0x90;
break;
case 'A':
case 'a':
returnArray[i] = (byte) 0xA0;
break;
case 'B':
case 'b':
returnArray[i] = (byte) 0xB0;
break;
case 'C':
case 'c':
returnArray[i] = (byte) 0xC0;
break;
case 'D':
case 'd':
returnArray[i] = (byte) 0xD0;
break;
case 'E':
case 'e':
returnArray[i] = (byte) 0xE0;
break;
case 'F':
case 'f':
returnArray[i] = (byte) 0xF0;
break;
default:
LocalizableMessage message = ERR_HEX_DECODE_INVALID_CHARACTER.get(
hexString, hexString.charAt(pos-1));
throw new ParseException(message.toString(), 0);
}
switch (hexString.charAt(pos++))
{
case '0':
// No action required.
break;
case '1':
returnArray[i] |= 0x01;
break;
case '2':
returnArray[i] |= 0x02;
break;
case '3':
returnArray[i] |= 0x03;
break;
case '4':
returnArray[i] |= 0x04;
break;
case '5':
returnArray[i] |= 0x05;
break;
case '6':
returnArray[i] |= 0x06;
break;
case '7':
returnArray[i] |= 0x07;
break;
case '8':
returnArray[i] |= 0x08;
break;
case '9':
returnArray[i] |= 0x09;
break;
case 'A':
case 'a':
returnArray[i] |= 0x0A;
break;
case 'B':
case 'b':
returnArray[i] |= 0x0B;
break;
case 'C':
case 'c':
returnArray[i] |= 0x0C;
break;
case 'D':
case 'd':
returnArray[i] |= 0x0D;
break;
case 'E':
case 'e':
returnArray[i] |= 0x0E;
break;
case 'F':
case 'f':
returnArray[i] |= 0x0F;
break;
default:
LocalizableMessage message = ERR_HEX_DECODE_INVALID_CHARACTER.get(
hexString, hexString.charAt(pos-1));
throw new ParseException(message.toString(), 0);
}
}
return returnArray;
}
/**
* Indicates whether the provided value needs to be base64-encoded if it is
* represented in LDIF form.
*
* @param valueBytes The binary representation of the attribute value for
* which to make the determination.
*
* @return {@code true} if the value needs to be base64-encoded if it is
* represented in LDIF form, or {@code false} if not.
*/
public static boolean needsBase64Encoding(ByteSequence valueBytes)
{
int length;
if (valueBytes == null || ((length = valueBytes.length()) == 0))
{
return false;
}
// If the value starts with a space, colon, or less than, then it needs to
// be base64-encoded.
switch (valueBytes.byteAt(0))
{
case 0x20: // Space
case 0x3A: // Colon
case 0x3C: // Less-than
return true;
}
// If the value ends with a space, then it needs to be base64-encoded.
if (length > 1 && valueBytes.byteAt(length - 1) == 0x20)
{
return true;
}
// If the value contains a null, newline, or return character, then it needs
// to be base64-encoded.
byte b;
for (int i = 0; i < valueBytes.length(); i++)
{
b = valueBytes.byteAt(i);
if (b < 0 || 127 < b)
{
return true;
}
switch (b)
{
case 0x00: // Null
case 0x0A: // New line
case 0x0D: // Carriage return
return true;
}
}
// If we've made it here, then there's no reason to base64-encode.
return false;
}
/**
* Indicates whether the provided value needs to be base64-encoded if it is
* represented in LDIF form.
*
* @param valueString The string representation of the attribute value for
* which to make the determination.
*
* @return {@code true} if the value needs to be base64-encoded if it is
* represented in LDIF form, or {@code false} if not.
*/
public static boolean needsBase64Encoding(String valueString)
{
int length;
if (valueString == null || ((length = valueString.length()) == 0))
{
return false;
}
// If the value starts with a space, colon, or less than, then it needs to
// be base64-encoded.
switch (valueString.charAt(0))
{
case ' ':
case ':':
case '<':
return true;
}
// If the value ends with a space, then it needs to be base64-encoded.
if (length > 1 && valueString.charAt(length - 1) == ' ')
{
return true;
}
// If the value contains a null, newline, or return character, then it needs
// to be base64-encoded.
for (int i=0; i < length; i++)
{
char c = valueString.charAt(i);
if (c <= 0 || c == 0x0A || c == 0x0D || c > 127)
{
return true;
}
}
// If we've made it here, then there's no reason to base64-encode.
return false;
}
/**
* Indicates whether the use of the exec method will be allowed on this
* system. It will be allowed by default, but that capability will be removed
* if the org.opends.server.DisableExec system property is set and has any
* value other than "false", "off", "no", or "0".
*
* @return {@code true} if the use of the exec method should be allowed,
* or {@code false} if it should not be allowed.
*/
public static boolean mayUseExec()
{
return !DirectoryServer.getEnvironmentConfig().disableExec();
}
/**
* Executes the specified command on the system and captures its output. This
* will not return until the specified process has completed.
*
* @param command The command to execute.
* @param args The set of arguments to provide to the command.
* @param workingDirectory The working directory to use for the command, or
* {@code null} if the default directory
* should be used.
* @param environment The set of environment variables that should be
* set when executing the command, or
* {@code null} if none are needed.
* @param output The output generated by the command while it was
* running. This will include both standard
* output and standard error. It may be
* {@code null} if the output does not need to
* be captured.
*
* @return The exit code for the command.
*
* @throws IOException If an I/O problem occurs while trying to execute the
* command.
*
* @throws SecurityException If the security policy will not allow the
* command to be executed.
*
* @throws 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 int exec(String command, String[] args, File workingDirectory,
Map* 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.
*
* @param builder
* The string builder.
* @param string
* The string to escape and append.
* @return Returns the string builder.
*/
public static StringBuilder toRFC3641StringValue(StringBuilder builder,
String string)
{
// Initial double-quote.
builder.append('"');
for (char c : string.toCharArray())
{
if (c == '"')
{
// Internal double-quotes are escaped using a double-quote.
builder.append('"');
}
builder.append(c);
}
// Trailing double-quote.
builder.append('"');
return builder;
}
/**
* Retrieves a string array containing the contents of the provided
* list of strings.
*
* @param stringList
* The string list to convert to an array.
* @return A string array containing the contents of the provided list
* of strings.
*/
public static String[] listToArray(List
* For example, consider a method with this signature:
*
*
* Classical use with for or while loop:
*
*
*
*
*
* Any other single RDN attribute types, or any case in which there are
* multiple RDN attributes, will use the untypedObject objectclass. If the
* RDN includes one or more attributes that are not allowed in the
* untypedObject objectclass, then the extensibleObject class will also be
* added. Note that this method cannot be used to generate an entry
* with an empty or null DN.
*
* @param dn The DN to use for the entry.
*
* @return The entry created with the provided DN.
*/
public static Entry createEntry(DN dn)
{
// If the provided DN was null or empty, then return null because we don't
// support it.
if (dn == null || dn.isRootDN())
{
return null;
}
// Get the information about the RDN attributes.
RDN rdn = dn.rdn();
// If there is only one RDN attribute, then see which objectclass we should use.
ObjectClass structuralClass = DirectoryServer.getObjectClass(getObjectClassName(rdn));
// Get the top and untypedObject classes to include in the entry.
LinkedHashMap
*
* Note that the original position and limit values will not be
* preserved, so if that is important to the caller, then it should
* record them before calling this method and restore them after it
* returns.
*
* @param clientConnection
* The client connection to which the data is to be written.
* @param buffer
* The data to be written to the client.
* @return {@code true} if all the data in the provided buffer was
* written to the client and the connection may remain
* established, or {@code false} if a problem occurred
* and the client connection is no longer valid. Note that if
* this method does return {@code false}, then it must
* have already disconnected the client.
* @throws 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 boolean writeWithTimeout(ClientConnection clientConnection,
ByteBuffer buffer) throws IOException
{
SocketChannel socketChannel = clientConnection.getSocketChannel();
long startTime = System.currentTimeMillis();
long waitTime = clientConnection.getMaxBlockedWriteTimeLimit();
if (waitTime <= 0)
{
// We won't support an infinite time limit, so fall back to using
// five minutes, which is a very long timeout given that we're
// blocking a worker thread.
waitTime = 300000L;
}
long stopTime = startTime + waitTime;
Selector selector = clientConnection.getWriteSelector();
if (selector == null)
{
// The client connection does not provide a selector, so we'll
// fall back
// to a more inefficient way that will work without a selector.
while (buffer.hasRemaining()
&& System.currentTimeMillis() < stopTime)
{
if (socketChannel.write(buffer) < 0)
{
// The client connection has been closed.
return false;
}
}
if (buffer.hasRemaining())
{
// If we've gotten here, then the write timed out.
return false;
}
return true;
}
// Register with the selector for handling write operations.
SelectionKey key =
socketChannel.register(selector, SelectionKey.OP_WRITE);
try
{
selector.select(waitTime);
while (buffer.hasRemaining())
{
long currentTime = System.currentTimeMillis();
if (currentTime >= stopTime)
{
// We've been blocked for too long.
return false;
}
else
{
waitTime = stopTime - currentTime;
}
Iteratorpublic Iterator<String> myIteratorMethod();
*
* 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()))
* {
* }
*
*
*