Package org.ldaptive
Class LdapUtils
java.lang.Object
org.ldaptive.LdapUtils
Provides utility methods for this package.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intPrime number to assist in calculating hash codes.private static final PatternPattern to match ipv4 addresses.private static final PatternPattern to match ipv6 hex compressed addresses.private static final PatternPattern to match ipv6 addresses.private static final intSize of buffer in bytes to use when reading files. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanDetermines equality of the supplied objects.static <T> Collection<T>assertNotContainsNullArg(Collection<T> o, String msg) ThrowsIllegalArgumentExceptionif the supplied collection contains a null value.static <T> T[]assertNotContainsNullArg(T[] o, String msg) ThrowsIllegalArgumentExceptionif the supplied array contains a null value.static <T> Collection<T>assertNotContainsNullArgOr(Collection<T> o, Predicate<Collection<T>> predicate, String msg) ThrowsIllegalArgumentExceptionif the supplied collection contains a null value or the supplied predicate returns true.static <T> T[]assertNotContainsNullArgOr(T[] o, Predicate<T[]> predicate, String msg) ThrowsIllegalArgumentExceptionif the supplied array contains a null value or the supplied predicate returns true.static <T> Collection<T>assertNotContainsNullState(Collection<T> o, String msg) ThrowsIllegalStateExceptionif the supplied collection contains a null value.static <T> T[]assertNotContainsNullState(T[] o, String msg) ThrowsIllegalStateExceptionif the supplied array contains a null value.static <T> Collection<T>assertNotContainsNullStateOr(Collection<T> o, Predicate<Collection<T>> predicate, String msg) ThrowsIllegalStateExceptionif the supplied collection contains a null value or the supplied predicate returns true.static <T> T[]assertNotContainsNullStateOr(T[] o, Predicate<T[]> predicate, String msg) ThrowsIllegalStateExceptionif the supplied array contains a null value or the supplied predicate returns true.static <T> TassertNotNullArg(T o, String msg) ThrowsIllegalArgumentExceptionif the supplied object is null.static <T> TassertNotNullArgOr(T o, Predicate<T> predicate, String msg) ThrowsIllegalArgumentExceptionif the supplied object is null or the supplied predicate returns true.static <T> TassertNotNullState(T o, String msg) ThrowsIllegalStateExceptionif the supplied object is null.static <T> TassertNotNullStateOr(T o, Predicate<T> predicate, String msg) ThrowsIllegalStateExceptionif the supplied object is null or the supplied predicate returns true.static byte[]base64Decode(String value) This will decode the supplied value as a base64 encoded string to a byte[].static Stringbase64Encode(byte... value) This will convert the supplied value to a base64 encoded string.static Stringbase64Encode(String value) This will convert the supplied value to a base64 encoded string.static StringcompressSpace(String value, boolean trim) Changes the supplied value by replacing multiple spaces with a single space.static intcomputeHashCode(int seed, Object... objects) Computes a hash code for the supplied objects using the supplied seed.private static intcomputeHashCode(Object object) Computes a hash code for the supplied object.static <T> T[]concatArrays(T[] first, T[]... rest) Concatenates multiple arrays together.static byte[]copyArray(byte[] array) Creates a copy of the supplied array.static <T> T[]copyArray(T[] array) Creates a copy of the supplied array.static <T> Constructor<T>createConstructorFromProperty(String property) Looks for the supplied system property value and loads a class with that name.static byte[]hexDecode(char[] value) This will decode the supplied value as a hex encoded string to a byte[].static char[]hexEncode(byte... value) This will convert the supplied value to a hex encoded string.static char[]hexEncode(char... value) This will convert the supplied value to a hex encoded string.static booleanReturns whether the supplied string represents an IP address.static intParse the supplied value as an integer.static longParse the supplied value as a long.static StringpercentDecode(String value) Implementation of percent decoding as described in RFC 3986 section 2.1.static StringpercentEncode(String value) Implementation of percent encoding as described in RFC 3986 section 2.1.static StringpercentEncodeControlChars(String value) Converts all characters <= 0x1F and 0x7F to percent encoded hex.static byte[]Reads the data in the supplied stream and returns it as a byte array.static booleanshouldBase64Encode(byte[] value) Determines whether the supplied value should be base64 encoded.static booleanshouldBase64Encode(byte[] value, boolean strict) Determines whether the supplied value should be base64 encoded.static booleanshouldBase64Encode(String value) static booleanshouldBase64Encode(String value, boolean strict) static StringtoLowerCase(String value) Converts the supplied value to lower case.static StringtoLowerCase(String value, boolean allowNull) Converts the supplied value to lower case.static StringConverts the characters A-Z to a-z.static StringConverts the supplied string to upper case.static StringConverts the characters a-z to A-Z.static StringRemoves the space character from both the beginning and end of the supplied value.static Stringutf8Encode(byte[] value) This will convert the supplied value to a UTF-8 encoded string.static Stringutf8Encode(byte[] value, boolean allowNull) This will convert the supplied value to a UTF-8 encoded string.static byte[]utf8Encode(char[] value) This will convert the supplied value to a UTF-8 encoded byte array.static byte[]utf8Encode(char[] value, boolean allowNull) This will convert the supplied value to a UTF-8 encoded byte array.static byte[]utf8Encode(String value) This will convert the supplied value to a UTF-8 encoded byte array.static byte[]utf8Encode(String value, boolean allowNull) This will convert the supplied value to a UTF-8 encoded byte array.
-
Field Details
-
READ_BUFFER_SIZE
private static final int READ_BUFFER_SIZESize of buffer in bytes to use when reading files.- See Also:
-
HASH_CODE_PRIME
private static final int HASH_CODE_PRIMEPrime number to assist in calculating hash codes.- See Also:
-
IPV4_PATTERN
Pattern to match ipv4 addresses. -
IPV6_STD_PATTERN
Pattern to match ipv6 addresses. -
IPV6_HEX_COMPRESSED_PATTERN
Pattern to match ipv6 hex compressed addresses.
-
-
Constructor Details
-
LdapUtils
private LdapUtils()Default constructor.
-
-
Method Details
-
base64Encode
This will convert the supplied value to a base64 encoded string. Returns null if the supplied byte array is null.- Parameters:
value- to base64 encode- Returns:
- base64 encoded value
-
base64Encode
This will convert the supplied value to a base64 encoded string. Returns null if the supplied string is null.- Parameters:
value- to base64 encode- Returns:
- base64 encoded value
-
utf8Encode
This will convert the supplied value to a UTF-8 encoded string. Returns null if the supplied byte array is null.- Parameters:
value- to UTF-8 encode- Returns:
- UTF-8 encoded value
-
utf8Encode
This will convert the supplied value to a UTF-8 encoded string.- Parameters:
value- to UTF-8 encodeallowNull- whether to throwIllegalArgumentExceptionif value is null- Returns:
- UTF-8 encoded value
- Throws:
IllegalArgumentException- if allowNull is false and value is null
-
utf8Encode
This will convert the supplied value to a UTF-8 encoded byte array. Returns null if the supplied string is null.- Parameters:
value- to UTF-8 encode- Returns:
- UTF-8 encoded value
-
utf8Encode
This will convert the supplied value to a UTF-8 encoded byte array.- Parameters:
value- to UTF-8 encodeallowNull- whether to throwIllegalArgumentExceptionif value is null- Returns:
- UTF-8 encoded value
- Throws:
IllegalArgumentException- if allowNull is false and value is null
-
utf8Encode
public static byte[] utf8Encode(char[] value) This will convert the supplied value to a UTF-8 encoded byte array. Returns null if the supplied value is null.- Parameters:
value- to UTF-8 encode- Returns:
- UTF-8 encoded value
-
utf8Encode
public static byte[] utf8Encode(char[] value, boolean allowNull) This will convert the supplied value to a UTF-8 encoded byte array.- Parameters:
value- to UTF-8 encodeallowNull- whether to throwIllegalArgumentExceptionif value is null- Returns:
- UTF-8 encoded value
- Throws:
IllegalArgumentException- if allowNull is false and value is null
-
hexEncode
public static char[] hexEncode(byte... value) This will convert the supplied value to a hex encoded string. Returns null if the supplied byte array is null.- Parameters:
value- to hex encode- Returns:
- hex encoded value
-
hexEncode
public static char[] hexEncode(char... value) This will convert the supplied value to a hex encoded string. Returns null if the supplied char array is null.- Parameters:
value- to hex encode- Returns:
- hex encoded value
-
percentEncode
Implementation of percent encoding as described in RFC 3986 section 2.1.- Parameters:
value- to encode- Returns:
- percent encoded value
-
percentEncodeControlChars
Converts all characters <= 0x1F and 0x7F to percent encoded hex.- Parameters:
value- to encode control characters in- Returns:
- string with percent encoded hex characters or original string if no character needs encoding
-
trimSpace
Removes the space character from both the beginning and end of the supplied value.- Parameters:
value- to trim space character from- Returns:
- trimmed value or same value if no trim was performed
-
compressSpace
Changes the supplied value by replacing multiple spaces with a single space.- Parameters:
value- to compress spacestrim- whether to remove any leading or trailing space characters- Returns:
- normalized value or value if no compress was performed
-
base64Decode
This will decode the supplied value as a base64 encoded string to a byte[]. Returns null if the supplied string is null.- Parameters:
value- to base64 decode- Returns:
- base64 decoded value
-
hexDecode
public static byte[] hexDecode(char[] value) This will decode the supplied value as a hex encoded string to a byte[]. Returns null if the supplied character array is null.- Parameters:
value- to hex decode- Returns:
- hex decoded value
-
percentDecode
Implementation of percent decoding as described in RFC 3986 section 2.1.- Parameters:
value- to decode- Returns:
- percent decoded value or same value if the string does not contain '%'
-
shouldBase64Encode
- Parameters:
value- to inspect- Returns:
- whether the value should be base64 encoded
-
shouldBase64Encode
- Parameters:
value- to inspectstrict- whether to strictly enforce what RFC 2849 requires- Returns:
- whether the value should be base64 encoded
-
shouldBase64Encode
public static boolean shouldBase64Encode(byte[] value) Determines whether the supplied value should be base64 encoded. SeeshouldBase64Encode(byte[], boolean).- Parameters:
value- to inspect- Returns:
- whether the value should be base64 encoded
-
shouldBase64Encode
public static boolean shouldBase64Encode(byte[] value, boolean strict) Determines whether the supplied value should be base64 encoded. See http://www.faqs.org/rfcs/rfc2849.html for more details. A false strict flag will return true if the string contains a character outside of [0x20, 0x7F) that should not be encoded as defined by RFC 2849- Parameters:
value- to inspectstrict- whether to strictly enforce what RFC 2849 requires- Returns:
- whether the value should be base64 encoded
-
toLowerCase
Converts the supplied value to lower case. If the string contains non-ascii characters,Locale.ROOTis used.- Parameters:
value- to lower case- Returns:
- new lower case string
-
toLowerCase
Converts the supplied value to lower case. If the string contains non-ascii characters,Locale.ROOTis used.- Parameters:
value- to lower caseallowNull- whether to throwIllegalArgumentExceptionif value is null- Returns:
- new lower case string
- Throws:
IllegalArgumentException- if allowNull is false and value is null
-
toLowerCaseAscii
Converts the characters A-Z to a-z.- Parameters:
s- to lower case- Returns:
- new string with lower case alphabetical characters
- Throws:
IllegalArgumentException- if the supplied string contains non-ascii characters
-
toUpperCase
Converts the supplied string to upper case. If the string contains non-ascii characters,Locale.ROOTis used.- Parameters:
s- to upper case- Returns:
- new upper case string
-
toUpperCaseAscii
Converts the characters a-z to A-Z.- Parameters:
s- to upper case- Returns:
- new string with upper case alphabetical characters
- Throws:
IllegalArgumentException- if the supplied string contains non-ascii characters
-
readInputStream
Reads the data in the supplied stream and returns it as a byte array.- Parameters:
is- stream to read- Returns:
- bytes read from the stream
- Throws:
IOException- if an error occurs reading data
-
concatArrays
public static <T> T[] concatArrays(T[] first, T[]... rest) Concatenates multiple arrays together.- Type Parameters:
T- type of array- Parameters:
first- array to concatenate. Cannot be null.rest- of the arrays to concatenate. May be null.- Returns:
- array containing the concatenation of all parameters
-
copyArray
public static <T> T[] copyArray(T[] array) Creates a copy of the supplied array.- Type Parameters:
T- type of array- Parameters:
array- array to copy. May be null.- Returns:
- array copy or null if input was null
-
copyArray
public static byte[] copyArray(byte[] array) Creates a copy of the supplied array.- Parameters:
array- array to copy. May be null.- Returns:
- array copy or null if input was null
-
areEqual
Determines equality of the supplied objects. Array types are automatically detected.- Parameters:
o1- to test equality ofo2- to test equality of- Returns:
- whether o1 equals o2
-
computeHashCode
Computes a hash code for the supplied objects using the supplied seed. If a Collection type is found it is iterated over.- Parameters:
seed- odd/prime numberobjects- to calculate hashCode for- Returns:
- hash code for the supplied objects
-
computeHashCode
Computes a hash code for the supplied object. Checks for arrays of primitives and Objects then delegates to theArraysclass. OtherwiseObject.hashCode()is invoked.- Parameters:
object- to calculate hash code for- Returns:
- hash code
-
isIPAddress
Returns whether the supplied string represents an IP address. Matches both IPv4 and IPv6 addresses.- Parameters:
s- to match- Returns:
- whether the supplied string represents an IP address
-
createConstructorFromProperty
Looks for the supplied system property value and loads a class with that name. The default constructor for that class is then returned.- Type Parameters:
T- class type- Parameters:
property- whose value is a class- Returns:
- class constructor or null if no system property was found
- Throws:
IllegalArgumentException- if an error occurs instantiating the constructor
-
parseInt
Parse the supplied value as an integer. Returns the default value if the predicate is not met or if the value cannot be parsed.- Parameters:
value- to parserequire- predicate to enforcedefaultValue- to return if predicate is false- Returns:
- parsed integer or default
-
parseLong
Parse the supplied value as a long. Returns the default value if the predicate is not met or if the value cannot be parsed.- Parameters:
value- to parserequire- predicate to enforcedefaultValue- to return if predicate is false- Returns:
- parsed long or default
-
assertNotNullArg
ThrowsIllegalArgumentExceptionif the supplied object is null.- Type Parameters:
T- type of object- Parameters:
o- to checkmsg- to include in the exception- Returns:
- supplied object
-
assertNotNullArgOr
ThrowsIllegalArgumentExceptionif the supplied object is null or the supplied predicate returns true.- Type Parameters:
T- type of object- Parameters:
o- to checkpredicate- to testmsg- to include in the exception- Returns:
- supplied object
-
assertNotContainsNullArg
ThrowsIllegalArgumentExceptionif the supplied array contains a null value. If a null array is supplied, it will be returned to the caller.- Type Parameters:
T- type of object- Parameters:
o- to checkmsg- to include in the exception- Returns:
- supplied object
-
assertNotContainsNullArgOr
ThrowsIllegalArgumentExceptionif the supplied array contains a null value or the supplied predicate returns true. If a null array is supplied, it will only be passed to the predicate.- Type Parameters:
T- type of object- Parameters:
o- to checkpredicate- to testmsg- to include in the exception- Returns:
- supplied object
-
assertNotContainsNullArg
ThrowsIllegalArgumentExceptionif the supplied collection contains a null value. If a null collection is supplied, it will be returned to the caller.- Type Parameters:
T- type of object- Parameters:
o- to checkmsg- to include in the exception- Returns:
- supplied collection
-
assertNotContainsNullArgOr
public static <T> Collection<T> assertNotContainsNullArgOr(Collection<T> o, Predicate<Collection<T>> predicate, String msg) ThrowsIllegalArgumentExceptionif the supplied collection contains a null value or the supplied predicate returns true. If a null collection is supplied, it will only be passed to the predicate.- Type Parameters:
T- type of object- Parameters:
o- to checkpredicate- to testmsg- to include in the exception- Returns:
- supplied collection
-
assertNotNullState
ThrowsIllegalStateExceptionif the supplied object is null.- Type Parameters:
T- type of object- Parameters:
o- to checkmsg- to include in the exception- Returns:
- supplied object
-
assertNotNullStateOr
ThrowsIllegalStateExceptionif the supplied object is null or the supplied predicate returns true.- Type Parameters:
T- type of object- Parameters:
o- to checkpredicate- to testmsg- to include in the exception- Returns:
- supplied object
-
assertNotContainsNullState
ThrowsIllegalStateExceptionif the supplied array contains a null value. If a null array is supplied, it will be returned to the caller.- Type Parameters:
T- type of object- Parameters:
o- to checkmsg- to include in the exception- Returns:
- supplied object
-
assertNotContainsNullStateOr
ThrowsIllegalStateExceptionif the supplied array contains a null value or the supplied predicate returns true. If a null array is supplied, it will only be passed to the predicate.- Type Parameters:
T- type of object- Parameters:
o- to checkpredicate- to testmsg- to include in the exception- Returns:
- supplied object
-
assertNotContainsNullState
ThrowsIllegalStateExceptionif the supplied collection contains a null value. If a null collection is supplied, it will be returned to the caller.- Type Parameters:
T- type of object- Parameters:
o- to checkmsg- to include in the exception- Returns:
- supplied object
-
assertNotContainsNullStateOr
public static <T> Collection<T> assertNotContainsNullStateOr(Collection<T> o, Predicate<Collection<T>> predicate, String msg) ThrowsIllegalStateExceptionif the supplied collection contains a null value or the supplied predicate returns true. If a null collection is supplied, it will only be passed to the predicate.- Type Parameters:
T- type of object- Parameters:
o- to checkpredicate- to testmsg- to include in the exception- Returns:
- supplied object
-