StaticUtils.java revision b8c6b80da1cb6118167a934daa480eb381c59e0e
/*
* 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
* 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-2015 ForgeRock AS
*/
/**
* 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.
*/
mayInstantiate=false,
mayExtend=false,
mayInvoke=true)
public final class StaticUtils
{
/** 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.
* <p>
* Example usage:
* <pre>
* int _10KB = 10 * KB;
* </pre>
*/
public static final int KB = 1024;
/**
* Number of bytes in a Mebibyte.
* <p>
* Example usage:
* <pre>
* int _10MB = 10 * MB;
* </pre>
*/
/** 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.
*/
{
if (s == null)
{
return null;
}
try
{
char c;
byte[] returnArray = new byte[length];
for (int i=0; i < length; i++)
{
c = s.charAt(i);
returnArray[i] = (byte) (c & 0x0000007F);
if (c != returnArray[i])
{
return s.getBytes("UTF-8");
}
}
return returnArray;
}
catch (Exception e)
{
logger.traceException(e);
try
{
return s.getBytes("UTF-8");
}
{
return s.getBytes();
}
}
}
/**
* Returns the provided byte array decoded as a UTF-8 string without throwing
* an UnsupportedEncodingException. This method is equivalent to:
*
* <pre>
* try
* {
* return new String(bytes, "UTF-8");
* }
* catch (UnsupportedEncodingException e)
* {
* // Should never happen: UTF-8 is always supported.
* throw new RuntimeException(e);
* }
* </pre>
*
* @param bytes
* The byte array to be decoded as a UTF-8 string.
* @return The decoded string.
*/
{
{
return "".intern();
}
for (int i = 0; i < sz; i++)
{
final byte b = bytes[i];
if ((b & 0x7f) != b)
{
try
{
}
catch (UnsupportedEncodingException e)
{
// Should never happen: UTF-8 is always supported.
throw new RuntimeException(e);
}
break;
}
}
}
/**
* Construct a byte array containing the UTF-8 encoding of the
* provided <code>char</code> array.
*
* @param chars
* The character array to convert to a UTF-8 byte array.
* @return Returns a byte array containing the UTF-8 encoding of the
* provided <code>char</code> array.
*/
{
}
/**
* 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.
*/
{
switch (b & 0xFF)
{
case 0x00: return "00";
case 0x01: return "01";
case 0x02: return "02";
case 0x03: return "03";
case 0x04: return "04";
case 0x05: return "05";
case 0x06: return "06";
case 0x07: return "07";
case 0x08: return "08";
case 0x09: return "09";
case 0x0A: return "0A";
case 0x0B: return "0B";
case 0x0C: return "0C";
case 0x0D: return "0D";
case 0x0E: return "0E";
case 0x0F: return "0F";
case 0x10: return "10";
case 0x11: return "11";
case 0x12: return "12";
case 0x13: return "13";
case 0x14: return "14";
case 0x15: return "15";
case 0x16: return "16";
case 0x17: return "17";
case 0x18: return "18";
case 0x19: return "19";
case 0x1A: return "1A";
case 0x1B: return "1B";
case 0x1C: return "1C";
case 0x1D: return "1D";
case 0x1E: return "1E";
case 0x1F: return "1F";
case 0x20: return "20";
case 0x21: return "21";
case 0x22: return "22";
case 0x23: return "23";
case 0x24: return "24";
case 0x25: return "25";
case 0x26: return "26";
case 0x27: return "27";
case 0x28: return "28";
case 0x29: return "29";
case 0x2A: return "2A";
case 0x2B: return "2B";
case 0x2C: return "2C";
case 0x2D: return "2D";
case 0x2E: return "2E";
case 0x2F: return "2F";
case 0x30: return "30";
case 0x31: return "31";
case 0x32: return "32";
case 0x33: return "33";
case 0x34: return "34";
case 0x35: return "35";
case 0x36: return "36";
case 0x37: return "37";
case 0x38: return "38";
case 0x39: return "39";
case 0x3A: return "3A";
case 0x3B: return "3B";
case 0x3C: return "3C";
case 0x3D: return "3D";
case 0x3E: return "3E";
case 0x3F: return "3F";
case 0x40: return "40";
case 0x41: return "41";
case 0x42: return "42";
case 0x43: return "43";
case 0x44: return "44";
case 0x45: return "45";
case 0x46: return "46";
case 0x47: return "47";
case 0x48: return "48";
case 0x49: return "49";
case 0x4A: return "4A";
case 0x4B: return "4B";
case 0x4C: return "4C";
case 0x4D: return "4D";
case 0x4E: return "4E";
case 0x4F: return "4F";
case 0x50: return "50";
case 0x51: return "51";
case 0x52: return "52";
case 0x53: return "53";
case 0x54: return "54";
case 0x55: return "55";
case 0x56: return "56";
case 0x57: return "57";
case 0x58: return "58";
case 0x59: return "59";
case 0x5A: return "5A";
case 0x5B: return "5B";
case 0x5C: return "5C";
case 0x5D: return "5D";
case 0x5E: return "5E";
case 0x5F: return "5F";
case 0x60: return "60";
case 0x61: return "61";
case 0x62: return "62";
case 0x63: return "63";
case 0x64: return "64";
case 0x65: return "65";
case 0x66: return "66";
case 0x67: return "67";
case 0x68: return "68";
case 0x69: return "69";
case 0x6A: return "6A";
case 0x6B: return "6B";
case 0x6C: return "6C";
case 0x6D: return "6D";
case 0x6E: return "6E";
case 0x6F: return "6F";
case 0x70: return "70";
case 0x71: return "71";
case 0x72: return "72";
case 0x73: return "73";
case 0x74: return "74";
case 0x75: return "75";
case 0x76: return "76";
case 0x77: return "77";
case 0x78: return "78";
case 0x79: return "79";
case 0x7A: return "7A";
case 0x7B: return "7B";
case 0x7C: return "7C";
case 0x7D: return "7D";
case 0x7E: return "7E";
case 0x7F: return "7F";
case 0x80: return "80";
case 0x81: return "81";
case 0x82: return "82";
case 0x83: return "83";
case 0x84: return "84";
case 0x85: return "85";
case 0x86: return "86";
case 0x87: return "87";
case 0x88: return "88";
case 0x89: return "89";
case 0x8A: return "8A";
case 0x8B: return "8B";
case 0x8C: return "8C";
case 0x8D: return "8D";
case 0x8E: return "8E";
case 0x8F: return "8F";
case 0x90: return "90";
case 0x91: return "91";
case 0x92: return "92";
case 0x93: return "93";
case 0x94: return "94";
case 0x95: return "95";
case 0x96: return "96";
case 0x97: return "97";
case 0x98: return "98";
case 0x99: return "99";
case 0x9A: return "9A";
case 0x9B: return "9B";
case 0x9C: return "9C";
case 0x9D: return "9D";
case 0x9E: return "9E";
case 0x9F: return "9F";
case 0xA0: return "A0";
case 0xA1: return "A1";
case 0xA2: return "A2";
case 0xA3: return "A3";
case 0xA4: return "A4";
case 0xA5: return "A5";
case 0xA6: return "A6";
case 0xA7: return "A7";
case 0xA8: return "A8";
case 0xA9: return "A9";
case 0xAA: return "AA";
case 0xAB: return "AB";
case 0xAC: return "AC";
case 0xAD: return "AD";
case 0xAE: return "AE";
case 0xAF: return "AF";
case 0xB0: return "B0";
case 0xB1: return "B1";
case 0xB2: return "B2";
case 0xB3: return "B3";
case 0xB4: return "B4";
case 0xB5: return "B5";
case 0xB6: return "B6";
case 0xB7: return "B7";
case 0xB8: return "B8";
case 0xB9: return "B9";
case 0xBA: return "BA";
case 0xBB: return "BB";
case 0xBC: return "BC";
case 0xBD: return "BD";
case 0xBE: return "BE";
case 0xBF: return "BF";
case 0xC0: return "C0";
case 0xC1: return "C1";
case 0xC2: return "C2";
case 0xC3: return "C3";
case 0xC4: return "C4";
case 0xC5: return "C5";
case 0xC6: return "C6";
case 0xC7: return "C7";
case 0xC8: return "C8";
case 0xC9: return "C9";
case 0xCA: return "CA";
case 0xCB: return "CB";
case 0xCC: return "CC";
case 0xCD: return "CD";
case 0xCE: return "CE";
case 0xCF: return "CF";
case 0xD0: return "D0";
case 0xD1: return "D1";
case 0xD2: return "D2";
case 0xD3: return "D3";
case 0xD4: return "D4";
case 0xD5: return "D5";
case 0xD6: return "D6";
case 0xD7: return "D7";
case 0xD8: return "D8";
case 0xD9: return "D9";
case 0xDA: return "DA";
case 0xDB: return "DB";
case 0xDC: return "DC";
case 0xDD: return "DD";
case 0xDE: return "DE";
case 0xDF: return "DF";
case 0xE0: return "E0";
case 0xE1: return "E1";
case 0xE2: return "E2";
case 0xE3: return "E3";
case 0xE4: return "E4";
case 0xE5: return "E5";
case 0xE6: return "E6";
case 0xE7: return "E7";
case 0xE8: return "E8";
case 0xE9: return "E9";
case 0xEA: return "EA";
case 0xEB: return "EB";
case 0xEC: return "EC";
case 0xED: return "ED";
case 0xEE: return "EE";
case 0xEF: return "EF";
case 0xF0: return "F0";
case 0xF1: return "F1";
case 0xF2: return "F2";
case 0xF3: return "F3";
case 0xF4: return "F4";
case 0xF5: return "F5";
case 0xF6: return "F6";
case 0xF7: return "F7";
case 0xF8: return "F8";
case 0xF9: return "F9";
case 0xFA: return "FA";
case 0xFB: return "FB";
case 0xFC: return "FC";
case 0xFD: return "FD";
case 0xFE: return "FE";
case 0xFF: return "FF";
default: return "??";
}
}
/**
* 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(byte b)
{
switch (b & 0xFF)
{
case 0x00: return "00";
case 0x01: return "01";
case 0x02: return "02";
case 0x03: return "03";
case 0x04: return "04";
case 0x05: return "05";
case 0x06: return "06";
case 0x07: return "07";
case 0x08: return "08";
case 0x09: return "09";
case 0x0A: return "0a";
case 0x0B: return "0b";
case 0x0C: return "0c";
case 0x0D: return "0d";
case 0x0E: return "0e";
case 0x0F: return "0f";
case 0x10: return "10";
case 0x11: return "11";
case 0x12: return "12";
case 0x13: return "13";
case 0x14: return "14";
case 0x15: return "15";
case 0x16: return "16";
case 0x17: return "17";
case 0x18: return "18";
case 0x19: return "19";
case 0x1A: return "1a";
case 0x1B: return "1b";
case 0x1C: return "1c";
case 0x1D: return "1d";
case 0x1E: return "1e";
case 0x1F: return "1f";
case 0x20: return "20";
case 0x21: return "21";
case 0x22: return "22";
case 0x23: return "23";
case 0x24: return "24";
case 0x25: return "25";
case 0x26: return "26";
case 0x27: return "27";
case 0x28: return "28";
case 0x29: return "29";
case 0x2A: return "2a";
case 0x2B: return "2b";
case 0x2C: return "2c";
case 0x2D: return "2d";
case 0x2E: return "2e";
case 0x2F: return "2f";
case 0x30: return "30";
case 0x31: return "31";
case 0x32: return "32";
case 0x33: return "33";
case 0x34: return "34";
case 0x35: return "35";
case 0x36: return "36";
case 0x37: return "37";
case 0x38: return "38";
case 0x39: return "39";
case 0x3A: return "3a";
case 0x3B: return "3b";
case 0x3C: return "3c";
case 0x3D: return "3d";
case 0x3E: return "3e";
case 0x3F: return "3f";
case 0x40: return "40";
case 0x41: return "41";
case 0x42: return "42";
case 0x43: return "43";
case 0x44: return "44";
case 0x45: return "45";
case 0x46: return "46";
case 0x47: return "47";
case 0x48: return "48";
case 0x49: return "49";
case 0x4A: return "4a";
case 0x4B: return "4b";
case 0x4C: return "4c";
case 0x4D: return "4d";
case 0x4E: return "4e";
case 0x4F: return "4f";
case 0x50: return "50";
case 0x51: return "51";
case 0x52: return "52";
case 0x53: return "53";
case 0x54: return "54";
case 0x55: return "55";
case 0x56: return "56";
case 0x57: return "57";
case 0x58: return "58";
case 0x59: return "59";
case 0x5A: return "5a";
case 0x5B: return "5b";
case 0x5C: return "5c";
case 0x5D: return "5d";
case 0x5E: return "5e";
case 0x5F: return "5f";
case 0x60: return "60";
case 0x61: return "61";
case 0x62: return "62";
case 0x63: return "63";
case 0x64: return "64";
case 0x65: return "65";
case 0x66: return "66";
case 0x67: return "67";
case 0x68: return "68";
case 0x69: return "69";
case 0x6A: return "6a";
case 0x6B: return "6b";
case 0x6C: return "6c";
case 0x6D: return "6d";
case 0x6E: return "6e";
case 0x6F: return "6f";
case 0x70: return "70";
case 0x71: return "71";
case 0x72: return "72";
case 0x73: return "73";
case 0x74: return "74";
case 0x75: return "75";
case 0x76: return "76";
case 0x77: return "77";
case 0x78: return "78";
case 0x79: return "79";
case 0x7A: return "7a";
case 0x7B: return "7b";
case 0x7C: return "7c";
case 0x7D: return "7d";
case 0x7E: return "7e";
case 0x7F: return "7f";
case 0x80: return "80";
case 0x81: return "81";
case 0x82: return "82";
case 0x83: return "83";
case 0x84: return "84";
case 0x85: return "85";
case 0x86: return "86";
case 0x87: return "87";
case 0x88: return "88";
case 0x89: return "89";
case 0x8A: return "8a";
case 0x8B: return "8b";
case 0x8C: return "8c";
case 0x8D: return "8d";
case 0x8E: return "8e";
case 0x8F: return "8f";
case 0x90: return "90";
case 0x91: return "91";
case 0x92: return "92";
case 0x93: return "93";
case 0x94: return "94";
case 0x95: return "95";
case 0x96: return "96";
case 0x97: return "97";
case 0x98: return "98";
case 0x99: return "99";
case 0x9A: return "9a";
case 0x9B: return "9b";
case 0x9C: return "9c";
case 0x9D: return "9d";
case 0x9E: return "9e";
case 0x9F: return "9f";
case 0xA0: return "a0";
case 0xA1: return "a1";
case 0xA2: return "a2";
case 0xA3: return "a3";
case 0xA4: return "a4";
case 0xA5: return "a5";
case 0xA6: return "a6";
case 0xA7: return "a7";
case 0xA8: return "a8";
case 0xA9: return "a9";
case 0xAA: return "aa";
case 0xAB: return "ab";
case 0xAC: return "ac";
case 0xAD: return "ad";
case 0xAE: return "ae";
case 0xAF: return "af";
case 0xB0: return "b0";
case 0xB1: return "b1";
case 0xB2: return "b2";
case 0xB3: return "b3";
case 0xB4: return "b4";
case 0xB5: return "b5";
case 0xB6: return "b6";
case 0xB7: return "b7";
case 0xB8: return "b8";
case 0xB9: return "b9";
case 0xBA: return "ba";
case 0xBB: return "bb";
case 0xBC: return "bc";
case 0xBD: return "bd";
case 0xBE: return "be";
case 0xBF: return "bf";
case 0xC0: return "c0";
case 0xC1: return "c1";
case 0xC2: return "c2";
case 0xC3: return "c3";
case 0xC4: return "c4";
case 0xC5: return "c5";
case 0xC6: return "c6";
case 0xC7: return "c7";
case 0xC8: return "c8";
case 0xC9: return "c9";
case 0xCA: return "ca";
case 0xCB: return "cb";
case 0xCC: return "cc";
case 0xCD: return "cd";
case 0xCE: return "ce";
case 0xCF: return "cf";
case 0xD0: return "d0";
case 0xD1: return "d1";
case 0xD2: return "d2";
case 0xD3: return "d3";
case 0xD4: return "d4";
case 0xD5: return "d5";
case 0xD6: return "d6";
case 0xD7: return "d7";
case 0xD8: return "d8";
case 0xD9: return "d9";
case 0xDA: return "da";
case 0xDB: return "db";
case 0xDC: return "dc";
case 0xDD: return "dd";
case 0xDE: return "de";
case 0xDF: return "df";
case 0xE0: return "e0";
case 0xE1: return "e1";
case 0xE2: return "e2";
case 0xE3: return "e3";
case 0xE4: return "e4";
case 0xE5: return "e5";
case 0xE6: return "e6";
case 0xE7: return "e7";
case 0xE8: return "e8";
case 0xE9: return "e9";
case 0xEA: return "ea";
case 0xEB: return "eb";
case 0xEC: return "ec";
case 0xED: return "ed";
case 0xEE: return "ee";
case 0xEF: return "ef";
case 0xF0: return "f0";
case 0xF1: return "f1";
case 0xF2: return "f2";
case 0xF3: return "f3";
case 0xF4: return "f4";
case 0xF5: return "f5";
case 0xF6: return "f6";
case 0xF7: return "f7";
case 0xF8: return "f8";
case 0xF9: return "f9";
case 0xFA: return "fa";
case 0xFB: return "fb";
case 0xFC: return "fc";
case 0xFD: return "fd";
case 0xFE: return "fe";
case 0xFF: return "ff";
default: return "??";
}
}
/**
* Retrieves the printable ASCII representation of the provided byte.
*
* @param b The byte for which to retrieve the printable ASCII
* representation.
*
* @return The printable ASCII representation of the provided byte, or a
* space if the provided byte does not have printable ASCII
* representation.
*/
public static char byteToASCII(byte b)
{
if ((b >= 32) && (b <= 126))
{
return (char) b;
}
return ' ';
}
/**
* 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)
{
{
return "";
}
int arrayLength = b.length;
for (int i=0; i < arrayLength; i++)
{
}
}
/**
* 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)
{
{
return "";
}
int arrayLength = b.length;
for (int i=1; i < arrayLength; i++)
{
}
}
/**
* 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.
*/
{
{
return "";
}
int arrayLength = b.length();
for (int i=1; i < arrayLength; i++)
{
}
}
/**
* 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)
{
{
return "";
}
int arrayLength = b.length;
for (int i=1; i < arrayLength; i++)
{
}
}
/**
* 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.
*/
{
if (b == null)
{
return "";
}
if (length == 0)
{
return "";
}
for (int i=1; i < length; i++)
{
}
}
/**
* 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.
*/
int indent)
{
for (int i=0 ; i < indent; i++)
{
}
int pos = 0;
{
pos++;
{
if (i == 7)
{
}
}
}
if (remaining > 0)
{
pos++;
for (int i=1; i < 16; i++)
{
if (i < remaining)
{
pos++;
}
else
{
}
if (i == 7)
{
if (i < remaining)
{
}
}
}
}
}
/**
* 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.
*/
int indent)
{
for (int i=0 ; i < indent; i++)
{
}
int pos = 0;
{
byte currentByte = b.get();
pos++;
{
currentByte = b.get();
if (i == 7)
{
}
}
}
if (remaining > 0)
{
byte currentByte = b.get();
for (int i=1; i < 16; i++)
{
if (i < remaining)
{
currentByte = b.get();
}
else
{
}
if (i == 7)
{
if (i < remaining)
{
}
}
}
}
}
/**
* Retrieves a binary representation of the provided byte. It will always be
*
* @param b The byte for which to retrieve the binary representation.
*
* @return The binary representation for the provided byte.
*/
public static String byteToBinary(byte b)
{
switch (b & 0xFF)
{
case 0x00: return "00000000";
case 0x01: return "00000001";
case 0x02: return "00000010";
case 0x03: return "00000011";
case 0x04: return "00000100";
case 0x05: return "00000101";
case 0x06: return "00000110";
case 0x07: return "00000111";
case 0x08: return "00001000";
case 0x09: return "00001001";
case 0x0A: return "00001010";
case 0x0B: return "00001011";
case 0x0C: return "00001100";
case 0x0D: return "00001101";
case 0x0E: return "00001110";
case 0x0F: return "00001111";
case 0x10: return "00010000";
case 0x11: return "00010001";
case 0x12: return "00010010";
case 0x13: return "00010011";
case 0x14: return "00010100";
case 0x15: return "00010101";
case 0x16: return "00010110";
case 0x17: return "00010111";
case 0x18: return "00011000";
case 0x19: return "00011001";
case 0x1A: return "00011010";
case 0x1B: return "00011011";
case 0x1C: return "00011100";
case 0x1D: return "00011101";
case 0x1E: return "00011110";
case 0x1F: return "00011111";
case 0x20: return "00100000";
case 0x21: return "00100001";
case 0x22: return "00100010";
case 0x23: return "00100011";
case 0x24: return "00100100";
case 0x25: return "00100101";
case 0x26: return "00100110";
case 0x27: return "00100111";
case 0x28: return "00101000";
case 0x29: return "00101001";
case 0x2A: return "00101010";
case 0x2B: return "00101011";
case 0x2C: return "00101100";
case 0x2D: return "00101101";
case 0x2E: return "00101110";
case 0x2F: return "00101111";
case 0x30: return "00110000";
case 0x31: return "00110001";
case 0x32: return "00110010";
case 0x33: return "00110011";
case 0x34: return "00110100";
case 0x35: return "00110101";
case 0x36: return "00110110";
case 0x37: return "00110111";
case 0x38: return "00111000";
case 0x39: return "00111001";
case 0x3A: return "00111010";
case 0x3B: return "00111011";
case 0x3C: return "00111100";
case 0x3D: return "00111101";
case 0x3E: return "00111110";
case 0x3F: return "00111111";
case 0x40: return "01000000";
case 0x41: return "01000001";
case 0x42: return "01000010";
case 0x43: return "01000011";
case 0x44: return "01000100";
case 0x45: return "01000101";
case 0x46: return "01000110";
case 0x47: return "01000111";
case 0x48: return "01001000";
case 0x49: return "01001001";
case 0x4A: return "01001010";
case 0x4B: return "01001011";
case 0x4C: return "01001100";
case 0x4D: return "01001101";
case 0x4E: return "01001110";
case 0x4F: return "01001111";
case 0x50: return "01010000";
case 0x51: return "01010001";
case 0x52: return "01010010";
case 0x53: return "01010011";
case 0x54: return "01010100";
case 0x55: return "01010101";
case 0x56: return "01010110";
case 0x57: return "01010111";
case 0x58: return "01011000";
case 0x59: return "01011001";
case 0x5A: return "01011010";
case 0x5B: return "01011011";
case 0x5C: return "01011100";
case 0x5D: return "01011101";
case 0x5E: return "01011110";
case 0x5F: return "01011111";
case 0x60: return "01100000";
case 0x61: return "01100001";
case 0x62: return "01100010";
case 0x63: return "01100011";
case 0x64: return "01100100";
case 0x65: return "01100101";
case 0x66: return "01100110";
case 0x67: return "01100111";
case 0x68: return "01101000";
case 0x69: return "01101001";
case 0x6A: return "01101010";
case 0x6B: return "01101011";
case 0x6C: return "01101100";
case 0x6D: return "01101101";
case 0x6E: return "01101110";
case 0x6F: return "01101111";
case 0x70: return "01110000";
case 0x71: return "01110001";
case 0x72: return "01110010";
case 0x73: return "01110011";
case 0x74: return "01110100";
case 0x75: return "01110101";
case 0x76: return "01110110";
case 0x77: return "01110111";
case 0x78: return "01111000";
case 0x79: return "01111001";
case 0x7A: return "01111010";
case 0x7B: return "01111011";
case 0x7C: return "01111100";
case 0x7D: return "01111101";
case 0x7E: return "01111110";
case 0x7F: return "01111111";
case 0x80: return "10000000";
case 0x81: return "10000001";
case 0x82: return "10000010";
case 0x83: return "10000011";
case 0x84: return "10000100";
case 0x85: return "10000101";
case 0x86: return "10000110";
case 0x87: return "10000111";
case 0x88: return "10001000";
case 0x89: return "10001001";
case 0x8A: return "10001010";
case 0x8B: return "10001011";
case 0x8C: return "10001100";
case 0x8D: return "10001101";
case 0x8E: return "10001110";
case 0x8F: return "10001111";
case 0x90: return "10010000";
case 0x91: return "10010001";
case 0x92: return "10010010";
case 0x93: return "10010011";
case 0x94: return "10010100";
case 0x95: return "10010101";
case 0x96: return "10010110";
case 0x97: return "10010111";
case 0x98: return "10011000";
case 0x99: return "10011001";
case 0x9A: return "10011010";
case 0x9B: return "10011011";
case 0x9C: return "10011100";
case 0x9D: return "10011101";
case 0x9E: return "10011110";
case 0x9F: return "10011111";
case 0xA0: return "10100000";
case 0xA1: return "10100001";
case 0xA2: return "10100010";
case 0xA3: return "10100011";
case 0xA4: return "10100100";
case 0xA5: return "10100101";
case 0xA6: return "10100110";
case 0xA7: return "10100111";
case 0xA8: return "10101000";
case 0xA9: return "10101001";
case 0xAA: return "10101010";
case 0xAB: return "10101011";
case 0xAC: return "10101100";
case 0xAD: return "10101101";
case 0xAE: return "10101110";
case 0xAF: return "10101111";
case 0xB0: return "10110000";
case 0xB1: return "10110001";
case 0xB2: return "10110010";
case 0xB3: return "10110011";
case 0xB4: return "10110100";
case 0xB5: return "10110101";
case 0xB6: return "10110110";
case 0xB7: return "10110111";
case 0xB8: return "10111000";
case 0xB9: return "10111001";
case 0xBA: return "10111010";
case 0xBB: return "10111011";
case 0xBC: return "10111100";
case 0xBD: return "10111101";
case 0xBE: return "10111110";
case 0xBF: return "10111111";
case 0xC0: return "11000000";
case 0xC1: return "11000001";
case 0xC2: return "11000010";
case 0xC3: return "11000011";
case 0xC4: return "11000100";
case 0xC5: return "11000101";
case 0xC6: return "11000110";
case 0xC7: return "11000111";
case 0xC8: return "11001000";
case 0xC9: return "11001001";
case 0xCA: return "11001010";
case 0xCB: return "11001011";
case 0xCC: return "11001100";
case 0xCD: return "11001101";
case 0xCE: return "11001110";
case 0xCF: return "11001111";
case 0xD0: return "11010000";
case 0xD1: return "11010001";
case 0xD2: return "11010010";
case 0xD3: return "11010011";
case 0xD4: return "11010100";
case 0xD5: return "11010101";
case 0xD6: return "11010110";
case 0xD7: return "11010111";
case 0xD8: return "11011000";
case 0xD9: return "11011001";
case 0xDA: return "11011010";
case 0xDB: return "11011011";
case 0xDC: return "11011100";
case 0xDD: return "11011101";
case 0xDE: return "11011110";
case 0xDF: return "11011111";
case 0xE0: return "11100000";
case 0xE1: return "11100001";
case 0xE2: return "11100010";
case 0xE3: return "11100011";
case 0xE4: return "11100100";
case 0xE5: return "11100101";
case 0xE6: return "11100110";
case 0xE7: return "11100111";
case 0xE8: return "11101000";
case 0xE9: return "11101001";
case 0xEA: return "11101010";
case 0xEB: return "11101011";
case 0xEC: return "11101100";
case 0xED: return "11101101";
case 0xEE: return "11101110";
case 0xEF: return "11101111";
case 0xF0: return "11110000";
case 0xF1: return "11110001";
case 0xF2: return "11110010";
case 0xF3: return "11110011";
case 0xF4: return "11110100";
case 0xF5: return "11110101";
case 0xF6: return "11110110";
case 0xF7: return "11110111";
case 0xF8: return "11111000";
case 0xF9: return "11111001";
case 0xFA: return "11111010";
case 0xFB: return "11111011";
case 0xFC: return "11111100";
case 0xFD: return "11111101";
case 0xFE: return "11111110";
case 0xFF: return "11111111";
default: return "????????";
}
}
/**
* 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.
*/
if (a == a2) {
return 0;
}
if (a == null) {
return -1;
}
return 1;
}
for (int i = 0; i < minLength; i++) {
int firstByte = 0xFF & a[i];
if (firstByte != secondByte) {
if (firstByte < secondByte) {
return -1;
} else if (firstByte > secondByte) {
return 1;
}
}
}
}
/**
* 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</CODE> if the two array lists are equal, or
* <CODE>false</CODE> if they are not.
*/
{
{
}
{
return false;
}
{
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))
{
}
// Otherwise we can just retrieve the elements efficiently via their index.
for (int i=0; i < numElements; i++)
{
{
{
return false;
}
}
{
return false;
}
}
return true;
}
/**
* Return true if and only if o1 and o2 are both null or o1.equals(o2).
*
* @param o1 the first object to compare
* @param o2 the second object to compare
* @return true iff o1 and o2 are equal
*/
{
{
}
else
{
}
}
/**
* 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.
*/
{
if (t instanceof IdentifiedException)
{
} else {
}
}
else if (t instanceof NullPointerException)
{
{
}
}
else if ((t instanceof InvocationTargetException) &&
{
return getExceptionMessage(t.getCause());
}
else
{
if (periodPos > 0)
{
}
else
{
}
if (t.getMessage() == null)
{
{
// FIXME Temporary to debug issue 2256.
if (t instanceof IllegalStateException)
{
{
}
}
}
}
else
{
}
}
}
/**
* 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.
*/
{
}
/**
* 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.
*/
Throwable t)
{
}
/**
* 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.
*/
{
stackTraceToString(buffer, t);
}
/**
* 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(
{
{
{
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.
*/
{
if (t == null)
{
return;
}
for (StackTraceElement e : t.getStackTrace())
{
}
{
t = t.getCause();
for (StackTraceElement e : t.getStackTrace())
{
}
}
}
/**
* 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()
{
{
{
}
}
}
/**
* 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</CODE> 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.
*/
{
{
{
}
}
}
/**
* Indicates whether the provided character is a numeric digit.
*
* @param c The character for which to make the determination.
*
* @return <CODE>true</CODE> if the provided character represents a numeric
* digit, or <CODE>false</CODE> if not.
*/
public static boolean isDigit(char c)
{
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return true;
default:
return false;
}
}
/**
* Indicates whether the provided character is an ASCII alphabetic character.
*
* @param c The character for which to make the determination.
*
* @return <CODE>true</CODE> if the provided value is an uppercase or
* lowercase ASCII alphabetic character, or <CODE>false</CODE> if it
* is not.
*/
public static boolean isAlpha(char c)
{
switch (c)
{
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
return true;
case '[':
case '\\':
case ']':
case '^':
case '_':
case '`':
// Making sure all possible cases are present in one contiguous range
// can result in a performance improvement.
return false;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
return true;
default:
return false;
}
}
/**
* Indicates whether the provided character is a hexadecimal digit.
*
* @param c The character for which to make the determination.
*
* @return <CODE>true</CODE> if the provided character represents a
* hexadecimal digit, or <CODE>false</CODE> if not.
*/
public static boolean isHexDigit(char c)
{
switch (c)
{
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;
}
}
/**
* Indicates whether the provided byte represents a hexadecimal digit.
*
* @param b The byte for which to make the determination.
*
* @return <CODE>true</CODE> if the provided byte represents a hexadecimal
* digit, or <CODE>false</CODE> 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.
*/
throws ParseException
{
int length;
{
return new byte[0];
}
{
}
int pos = 0;
byte[] returnArray = new byte[arrayLength];
for (int i=0; i < arrayLength; i++)
{
{
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:
}
{
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:
}
}
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</CODE> if the value needs to be base64-encoded if it is
* represented in LDIF form, or <CODE>false</CODE> if not.
*/
{
int length;
{
return false;
}
// If the value starts with a space, colon, or less than, then it needs to
// be base64-encoded.
{
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.
{
return true;
}
// If the value contains a null, newline, or return character, then it needs
// to be base64-encoded.
byte b;
{
b = valueBytes.byteAt(i);
if ((b > 127) || (b < 0))
{
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</CODE> if the value needs to be base64-encoded if it is
* represented in LDIF form, or <CODE>false</CODE> if not.
*/
{
int length;
{
return false;
}
// If the value starts with a space, colon, or less than, then it needs to
// be base64-encoded.
{
case ' ':
case ':':
case '<':
return true;
}
// If the value ends with a space, then it needs to be base64-encoded.
{
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</CODE> if the use of the exec method should be allowed,
* or <CODE>false</CODE> if it should not be allowed.
*/
public static boolean mayUseExec()
{
}
/**
* 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</CODE> 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</CODE> 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</CODE> 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.
*/
{
// See whether we'll allow the use of exec on this system. If not, then
// throw an exception.
if (! mayUseExec())
{
}
{
{
}
}
{
}
{
}
// We must exhaust stdout and stderr before calling waitfor. Since we
// redirected the error stream, we just have to read from stdout.
try
{
{
{
}
}
}
catch(IOException ioe)
{
// If this happens, then we have no choice but to forcefully terminate
// the process.
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
throw ioe;
}
finally
{
try
{
}
catch(IOException e)
{
logger.traceException(e);
}
}
}
/**
* Indicates whether the provided string contains a name or OID for a schema
* element like an attribute type or objectclass.
*
* @param element The string containing the substring for which to
* make the determination.
* @param startPos The position of the first character that is to be
* checked.
* @param endPos The position of the first character after the start
* position that is not to be checked.
* @param invalidReason The buffer to which the invalid reason is to be
* appended if a problem is found.
*
* @return <CODE>true</CODE> if the provided string contains a valid name or
* OID for a schema element, or <CODE>false</CODE> if it does not.
*/
int endPos,
{
{
return false;
}
if (isAlpha(c))
{
// This can only be a name and not an OID. The only remaining characters
// must be letters, digits, dashes, and possibly the underscore.
{
if (!isAlpha(c)
&& !isDigit(c)
&& c != '-'
{
// This is an illegal character for an attribute name.
return false;
}
}
}
else if (isDigit(c))
{
// This should indicate an OID, but it may also be a name if name
// exceptions are enabled. Since we don't know for sure, we'll just
// hold off until we know for sure.
boolean isNumeric = true;
boolean lastWasDot = false;
{
if (c == '.')
{
if (isKnown)
{
if (isNumeric)
{
// This is probably legal unless the last character was also a
// period.
if (lastWasDot)
{
element, i));
return false;
}
else
{
lastWasDot = true;
}
}
else
{
// This is an illegal character.
element, c, i));
return false;
}
}
else
{
// Now we know that this must be a numeric OID and not an attribute
// name with exceptions allowed.
lastWasDot = true;
isKnown = true;
isNumeric = true;
}
}
else
{
lastWasDot = false;
{
if (isKnown)
{
if (isNumeric)
{
// This is an illegal character for a numeric OID.
element, c, i));
return false;
}
}
else
{
// Now we know that this must be an attribute name with exceptions
// allowed and not a numeric OID.
isKnown = true;
isNumeric = false;
}
}
else if (! isDigit(c))
{
// This is an illegal character.
element, c, i));
return false;
}
}
}
}
else
{
// This is an illegal character.
return false;
}
// If we've gotten here, then the value is fine.
return true;
}
/**
* Indicates whether the provided TCP address is already in use.
*
* @param address IP address of the TCP address for which to make
* the determination.
* @param port TCP port number of the TCP address for which to
* make the determination.
* @param allowReuse Whether or not TCP address reuse is allowed when
* making the determination.
*
* @return <CODE>true</CODE> if the provided TCP address is already in
* use, or <CODE>false</CODE> otherwise.
*/
public static boolean isAddressInUse(
boolean allowReuse)
{
// Return pessimistic.
boolean isInUse = true;
try {
// HACK:
// With dual stacks we can have a situation when INADDR_ANY/PORT
// is bound in TCP4 space but available in TCP6 space and since
// JavaServerSocket implemantation will always use TCP46 on dual
// stacks the bind below will always succeed in such cases thus
// shadowing anything that is already bound to INADDR_ANY/PORT.
// While technically correct, with IPv4 and IPv6 being separate
// address spaces, it presents a problem to end users because a
// common case scenario is to have a single service serving both
// address spaces ie listening to the same port in both spaces
// on wildcard addresses 0 and ::. ServerSocket implemantation
// does not provide any means of working with each address space
// separately such as doing TCP4 or TCP6 only binds thus we have
// to do a dummy connect to INADDR_ANY/PORT to check if it is
// bound to something already. This is only needed for wildcard
// addresses as specific IPv4 or IPv6 addresses will always be
// handled in their respective address space.
if (address.isAnyLocalAddress()) {
clientSocket = new Socket();
try {
// This might fail on some stacks but this is the best we
// can do. No need for explicit timeout since it is local
// address and we have to know for sure unless it fails.
} catch (IOException e) {
// Expected, ignore.
}
if (clientSocket.isConnected()) {
return true;
}
}
serverSocket = new ServerSocket();
isInUse = false;
} catch (IOException e) {
isInUse = true;
} finally {
try {
if (serverSocket != null) {
}
} catch (Exception e) {}
try {
if (clientSocket != null) {
}
} catch (Exception e) {}
}
return isInUse;
}
/**
* Retrieves a lowercase representation of the given string. This
* implementation presumes that the provided string will contain only ASCII
* characters and is optimized for that case. However, if a non-ASCII
* character is encountered it will fall back on a more expensive algorithm
* that will work properly for non-ASCII characters.
*
* @param s The string for which to obtain the lowercase representation.
*
* @return The lowercase representation of the given string.
*/
{
if (s == null)
{
return null;
}
toLowerCase(s, buffer);
}
/**
* Appends a lowercase representation of the given string to the provided
* buffer. This implementation presumes that the provided string will contain
* only ASCII characters and is optimized for that case. However, if a
* non-ASCII character is encountered it will fall back on a more expensive
* algorithm that will work properly for non-ASCII characters.
*
* @param s The string for which to obtain the lowercase
* representation.
* @param buffer The buffer to which the lowercase form of the string should
* be appended.
*/
{
if (s == null)
{
return;
}
for (int i=0; i < length; i++)
{
char c = s.charAt(i);
if ((c & 0x7F) != c)
{
return;
}
switch (c)
{
case 'A':
break;
case 'B':
break;
case 'C':
break;
case 'D':
break;
case 'E':
break;
case 'F':
break;
case 'G':
break;
case 'H':
break;
case 'I':
break;
case 'J':
break;
case 'K':
break;
case 'L':
break;
case 'M':
break;
case 'N':
break;
case 'O':
break;
case 'P':
break;
case 'Q':
break;
case 'R':
break;
case 'S':
break;
case 'T':
break;
case 'U':
break;
case 'V':
break;
case 'W':
break;
case 'X':
break;
case 'Y':
break;
case 'Z':
break;
default:
}
}
}
/**
* Appends a lowercase string representation of the contents of the given byte
* array to the provided buffer, optionally trimming leading and trailing
* spaces. This implementation presumes that the provided string will contain
* only ASCII characters and is optimized for that case. However, if a
* non-ASCII character is encountered it will fall back on a more expensive
* algorithm that will work properly for non-ASCII characters.
*
* @param b The byte array for which to obtain the lowercase string
* representation.
* @param buffer The buffer to which the lowercase form of the string should
* be appended.
* @param trim Indicates whether leading and trailing spaces should be
* omitted from the string representation.
*/
boolean trim)
{
if (b == null)
{
return;
}
for (int i=0; i < length; i++)
{
{
b.toString().toLowerCase());
break;
}
switch (b.byteAt(i))
{
case ' ':
// If we don't care about trimming, then we can always append the
// space. Otherwise, only do so if there are other characters in the
// value.
{
break;
}
break;
case 'A':
break;
case 'B':
break;
case 'C':
break;
case 'D':
break;
case 'E':
break;
case 'F':
break;
case 'G':
break;
case 'H':
break;
case 'I':
break;
case 'J':
break;
case 'K':
break;
case 'L':
break;
case 'M':
break;
case 'N':
break;
case 'O':
break;
case 'P':
break;
case 'Q':
break;
case 'R':
break;
case 'S':
break;
case 'T':
break;
case 'U':
break;
case 'V':
break;
case 'W':
break;
case 'X':
break;
case 'Y':
break;
case 'Z':
break;
default:
}
}
if (trim)
{
// Strip off any trailing spaces.
{
{
}
else
{
break;
}
}
}
}
/**
* Retrieves an uppercase representation of the given string. This
* implementation presumes that the provided string will contain only ASCII
* characters and is optimized for that case. However, if a non-ASCII
* character is encountered it will fall back on a more expensive algorithm
* that will work properly for non-ASCII characters.
*
* @param s The string for which to obtain the uppercase representation.
*
* @return The uppercase representation of the given string.
*/
{
if (s == null)
{
return null;
}
toUpperCase(s, buffer);
}
/**
* Appends an uppercase representation of the given string to the provided
* buffer. This implementation presumes that the provided string will contain
* only ASCII characters and is optimized for that case. However, if a
* non-ASCII character is encountered it will fall back on a more expensive
* algorithm that will work properly for non-ASCII characters.
*
* @param s The string for which to obtain the uppercase
* representation.
* @param buffer The buffer to which the uppercase form of the string should
* be appended.
*/
{
if (s == null)
{
return;
}
for (int i=0; i < length; i++)
{
char c = s.charAt(i);
if ((c & 0x7F) != c)
{
return;
}
switch (c)
{
case 'a':
break;
case 'b':
break;
case 'c':
break;
case 'd':
break;
case 'e':
break;
case 'f':
break;
case 'g':
break;
case 'h':
break;
case 'i':
break;
case 'j':
break;
case 'k':
break;
case 'l':
break;
case 'm':
break;
case 'n':
break;
case 'o':
break;
case 'p':
break;
case 'q':
break;
case 'r':
break;
case 's':
break;
case 't':
break;
case 'u':
break;
case 'v':
break;
case 'w':
break;
case 'x':
break;
case 'y':
break;
case 'z':
break;
default:
}
}
}
/**
* Appends an uppercase string representation of the contents of the given
* byte array to the provided buffer, optionally trimming leading and trailing
* spaces. This implementation presumes that the provided string will contain
* only ASCII characters and is optimized for that case. However, if a
* non-ASCII character is encountered it will fall back on a more expensive
* algorithm that will work properly for non-ASCII characters.
*
* @param b The byte array for which to obtain the uppercase string
* representation.
* @param buffer The buffer to which the uppercase form of the string should
* be appended.
* @param trim Indicates whether leading and trailing spaces should be
* omitted from the string representation.
*/
{
if (b == null)
{
return;
}
for (int i=0; i < length; i++)
{
if ((b[i] & 0x7F) != b[i])
{
try
{
}
catch (Exception e)
{
logger.traceException(e);
}
break;
}
switch (b[i])
{
case ' ':
// If we don't care about trimming, then we can always append the
// space. Otherwise, only do so if there are other characters in the
// value.
{
break;
}
break;
case 'a':
break;
case 'b':
break;
case 'c':
break;
case 'd':
break;
case 'e':
break;
case 'f':
break;
case 'g':
break;
case 'h':
break;
case 'i':
break;
case 'j':
break;
case 'k':
break;
case 'l':
break;
case 'm':
break;
case 'n':
break;
case 'o':
break;
case 'p':
break;
case 'q':
break;
case 'r':
break;
case 's':
break;
case 't':
break;
case 'u':
break;
case 'v':
break;
case 'w':
break;
case 'x':
break;
case 'y':
break;
case 'z':
break;
default:
}
}
if (trim)
{
// Strip off any trailing spaces.
{
{
}
else
{
break;
}
}
}
}
/**
* Append a string to a string builder, escaping any double quotes
* according to the StringValue production in RFC 3641.
* <p>
* In RFC 3641 the StringValue production looks like this:
*
* <pre>
* 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
* </pre>
*
* <p>
* 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.
*/
{
// Initial double-quote.
for (char c : string.toCharArray())
{
if (c == '"')
{
// Internal double-quotes are escaped using a double-quote.
}
}
// Trailing double-quote.
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.
*/
{
if (stringList == null)
{
return null;
}
return stringArray;
}
/**
* Retrieves an array list containing the contents of the provided array.
*
* @param stringArray The string array to convert to an array list.
*
* @return An array list containing the contents of the provided array.
*/
{
if (stringArray == null)
{
return null;
}
for (String s : stringArray)
{
stringList.add(s);
}
return stringList;
}
/**
* Attempts to delete the specified file or directory. If it is a directory,
* then any files or subdirectories that it contains will be recursively
* deleted as well.
*
* @param file
* The file or directory to be removed.
* @return {@code true} if the specified file and any subordinates are all
* successfully removed, or {@code false} if at least one element in
* the subtree could not be removed or file does not exists.
*/
{
{
boolean successful = true;
if (file.isDirectory())
{
{
{
successful &= recursiveDelete(f);
}
}
}
}
return false;
}
/**
* 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. The file to move must exist and must be a
* file. The target directory must exist, must be a directory, and must not
* be the directory in which the file currently resides.
*
* @param fileToMove The file to move to the target directory.
* @param targetDirectory The directory into which the file should be moved.
*
* @throws IOException If a problem occurs while attempting to move the
* file.
*/
throws IOException
{
if (! fileToMove.exists())
{
}
if (! fileToMove.isFile())
{
}
if (! targetDirectory.exists())
{
}
if (! targetDirectory.isDirectory())
{
}
byte[] buffer = new byte[8192];
while (true)
{
if (bytesRead < 0)
{
break;
}
}
inputStream.close();
fileToMove.delete();
}
/**
* Renames the source file to the target file. If the target file exists
* it is first deleted. The rename and delete operation return values
* are checked for success and if unsuccessful, this method throws an
* exception.
*
* @param fileToRename The file to rename.
* @param target The file to which <code>fileToRename</code> will be
* moved.
* @throws 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.
*/
throws IOException {
{
synchronized(target)
{
{
}
}
{
}
}
}
/**
* Indicates whether the provided path refers to a relative path rather than
* an absolute path.
*
* @param path The path string for which to make the determination.
*
* @return <CODE>true</CODE> if the provided path is relative, or
* <CODE>false</CODE> if it is absolute.
*/
{
return (! f.isAbsolute());
}
/**
* Retrieves a <CODE>File</CODE> 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.
*
* @param path The path string to be retrieved as a <CODE>File</CODE>
*
* @return A <CODE>File</CODE> object that corresponds to the specified path.
*/
{
if (f.isAbsolute())
{
return f;
}
else
{
path);
}
}
/**
* Retrieves a <CODE>File</CODE> 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.
*
* @param path
* The path string to be retrieved as a <CODE>File</CODE>.
* @param serverContext
* The server context.
*
* @return A <CODE>File</CODE> object that corresponds to the specified path.
*/
{
if (f.isAbsolute())
{
return f;
}
else
{
path);
}
}
/**
* Creates a new, blank entry with the given DN. It will contain only the
* attribute(s) contained in the RDN. The choice of objectclasses will be
* based on the RDN attribute. If there is a single RDN attribute, then the
* following mapping will be used:
* <BR>
* <UL>
* <LI>c attribute :: country objectclass</LI>
* <LI>dc attribute :: domain objectclass</LI>
* <LI>o attribute :: organization objectclass</LI>
* <LI>ou attribute :: organizationalUnit objectclass</LI>
* </UL>
* <BR>
* 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.
*/
{
// If the provided DN was null or empty, then return null because we don't
// support it.
{
return null;
}
// Get the information about the RDN attributes.
// If there is only one RDN attribute, then see which objectclass we should use.
// Get the top and untypedObject classes to include in the entry.
// Iterate through the RDN attributes and add them to the set of user or
// operational attributes.
boolean extensibleObjectAdded = false;
for (int i=0; i < numAVAs; i++)
{
// First, see if this type is allowed by the untypedObject class. If not,
// then we'll need to include the extensibleObject class.
{
if (extensibleObjectOC == null)
{
}
extensibleObjectAdded = true;
}
// Create the attribute and add it to the appropriate map.
if (attrType.isOperational())
{
}
else
{
}
}
// Create and return the entry.
}
{
if (numAVAs == 1)
{
{
return OC_COUNTRY;
}
{
return OC_DOMAIN;
}
{
return OC_ORGANIZATION;
}
{
return OC_ORGANIZATIONAL_UNIT_LC;
}
}
return OC_UNTYPED_OBJECT_LC;
}
{
{
}
else
{
}
}
/**
* Retrieves a user-friendly string that indicates the length of time (in
* days, hours, minutes, and seconds) in the specified number of seconds.
*
* @param numSeconds The number of seconds to be converted to a more
* user-friendly value.
*
* @return The user-friendly representation of the specified number of
* seconds.
*/
{
if (numSeconds < 60)
{
// We can express it in seconds.
}
else if (numSeconds < 3600)
{
// We can express it in minutes and seconds.
long m = numSeconds / 60;
long s = numSeconds % 60;
return INFO_TIME_IN_MINUTES_SECONDS.get(m, s);
}
else if (numSeconds < 86400)
{
// We can express it in hours, minutes, and seconds.
long h = numSeconds / 3600;
return INFO_TIME_IN_HOURS_MINUTES_SECONDS.get(h, m, s);
}
else
{
// We can express it in days, hours, minutes, and seconds.
long d = numSeconds / 86400;
return INFO_TIME_IN_DAYS_HOURS_MINUTES_SECONDS.get(d, h, m, s);
}
}
/**
* Checks that no more that one of a set of arguments is present. This
* utility should be used after argument parser has parsed a set of
* arguments.
*
* @param args to test for the presence of more than one
* @throws ArgumentException if more than one of <code>args</code> is
* present and containing an error message identifying the
* arguments in violation
*/
throws ArgumentException
{
throw new ArgumentException(
}
}
}
}
}
/**
* Converts a string representing a time in "yyyyMMddHHmmss.SSS'Z'" or
* "yyyyMMddHHmmss" to a <code>Date</code>.
*
* @param timeStr string formatted appropriately
* @return Date object; null if <code>timeStr</code> is null
* @throws ParseException if there was a problem converting the string to
* a <code>Date</code>.
*/
{
{
{
try
{
dateFormat.setLenient(true);
}
catch (ParseException pe)
{
// Best effort: try with GMT time.
dateFormat.setLenient(true);
}
}
else
{
dateFormat.setLenient(true);
}
}
return dateTime;
}
/**
* Formats a Date to String representation in "yyyyMMddHHmmss'Z'".
*
* @param date to format; null if <code>date</code> is null
* @return string representation of the date
*/
{
{
}
return timeStr;
}
/**
* Indicates whether or not a string represents a syntactically correct
* email address.
*
* @param addr to validate
* @return boolean where <code>true</code> indicates that the string is a
* syntactically correct email address
*/
// This just does basic syntax checking. Perhaps we
// might want to be stricter about this.
}
/**
* 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). If possible, it will attempt to use the selector returned
* by the {@code ClientConnection.getWriteSelector} method, but it is
* capable of working even if that method returns {@code null}. <BR>
*
* 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</CODE> if all the data in the provided buffer was
* written to the client and the connection may remain
* established, or <CODE>false</CODE> if a problem occurred
* and the client connection is no longer valid. Note that if
* this method does return <CODE>false</CODE>, 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.
*/
{
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;
}
{
// 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()
{
{
// 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.
try
{
while (buffer.hasRemaining())
{
if (currentTime >= stopTime)
{
// We've been blocked for too long.
return false;
}
else
{
}
{
if (k.isWritable())
{
if (bytesWritten < 0)
{
// The client connection has been closed.
return false;
}
}
}
if (buffer.hasRemaining())
{
}
}
return true;
}
finally
{
{
}
}
}
/**
* Add all of the superior objectclasses to the specified objectclass
* map if they don't already exist. Used by add and import-ldif to
* add missing superior objectclasses to entries that don't have them.
*
* @param objectClasses A Map of objectclasses.
*/
String> objectClasses) {
{
{
{
if (additionalClasses == null)
{
additionalClasses = new HashSet<>();
}
}
}
}
if (additionalClasses != null)
{
{
}
}
}
{
if (objectClasses != null){
{
}
{
{
}
}
}
}
/**
* Closes the provided {@link Closeable}'s ignoring any errors which
* occurred.
*
* @param closeables The closeables to be closed, which may be
* <code>null</code>.
*/
{
if (closeables == null)
{
return;
}
}
/**
* Closes the provided {@link Closeable}'s ignoring any errors which occurred.
*
* @param closeables
* The closeables to be closed, which may be <code>null</code>.
*/
{
if (closeables == null)
{
return;
}
{
{
try
{
}
catch (IOException ignored)
{
}
}
}
}
/**
* Closes the provided {@link InitialContext}s ignoring any errors which occurred.
*
* @param ctxs
* The contexts to be closed, which may be <code>null</code>.
*/
{
{
return;
}
{
{
try
{
}
catch (NamingException ignored)
{
// ignore
}
}
}
}
/**
* Calls {@link Thread#sleep(long)}, surrounding it with the mandatory
* <code>try</code> / <code>catch(InterruptedException)</code> block.
*
* @param millis
* the length of time to sleep in milliseconds
*/
{
try
{
}
catch (InterruptedException wokenUp)
{
// ignore
}
}
/**
* Test if the provided message corresponds to the provided descriptor.
*
* @param msg
* The i18n message.
* @param desc
* The message descriptor.
* @return {@code true} if message corresponds to descriptor
*/
{
}
/**
* Test if the provided message corresponds to the provided descriptor.
*
* @param msg
* The i18n message.
* @param desc
* The message descriptor.
* @return {@code true} if message corresponds to descriptor
*/
{
}
/**
* Test if the provided message corresponds to the provided descriptor.
*
* @param msg
* The i18n message.
* @param desc
* The message descriptor.
* @return {@code true} if message corresponds to descriptor
*/
{
}
/**
* Test if the provided message corresponds to the provided descriptor.
*
* @param msg
* The i18n message.
* @param desc
* The message descriptor.
* @return {@code true} if message corresponds to descriptor
*/
{
}
/**
* Test if the provided message corresponds to the provided descriptor.
*
* @param msg
* The i18n message.
* @param desc
* The message descriptor.
* @return {@code true} if message corresponds to descriptor
*/
{
}
/**
* Returns an {@link Iterable} returning the passed in {@link Iterator}. THis
* allows using methods returning Iterators with foreach statements.
* <p>
* For example, consider a method with this signature:
* <p>
* <code>public Iterator<String> myIteratorMethod();</code>
* <p>
* Classical use with for or while loop:
*
* <pre>
* 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
* }
* </pre>
*
* Improved use with foreach:
*
* <pre>
* for (String s : StaticUtils.toIterable(myIteratorMethod()))
* {
* }
* </pre>
*
* </p>
*
* @param <T>
* the generic type of the passed in Iterator and for the returned
* Iterable.
* @param iterator
* the Iterator that will be returned by the Iterable.
* @return an Iterable returning the passed in Iterator
*/
{
return new Iterable<T>()
{
{
return iterator;
}
};
}
}