Class Utils
java.lang.Object
com.amazonaws.encryptionsdk.internal.Utils
Internal utility methods.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classComparator that performs a lexicographical comparison of byte arrays, treating them as unsigned.static class -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanarrayPrefixEquals(byte[] a, byte[] b, int length)Returns true if the prefix of the given length for the input arrays are equal.static <T> TassertNonNull(T object, String paramName)static byte[]bigIntegerToByteArray(BigInteger bigInteger, int length)Removes the leading zero sign byte from the byte array representation of a BigInteger (if present) and left pads with zeroes to produce a byte array of the given length.static ByteBufferclear(ByteBuffer buff)Equivalent to callingByteBuffer.clear()but in a manner which is safe when compiled on Java 9 or newer but used on Java 8 or older.static intcompareObjectIdentity(Object a, Object b)Provides an arbitrary but consistent total ordering over all objects.static byte[]decodeBase64String(String encoded)Takes a Base64-encoded String, decodes it, and returns contents as a byte array.static StringencodeBase64String(byte[] data)Takes data in a byte array, encodes them in Base64, and returns the result as a String.static ByteBufferflip(ByteBuffer buff)Equivalent to callingByteBuffer.flip()but in a manner which is safe when compiled on Java 9 or newer but used on Java 8 or older.static SecureRandomstatic ByteBufferlimit(ByteBuffer buff, int newLimit)Equivalent to callingByteBuffer.limit(int)but in a manner which is safe when compiled on Java 9 or newer but used on Java 8 or older.static ByteBufferposition(ByteBuffer buff, int newPosition)Equivalent to callingByteBuffer.position(int)but in a manner which is safe when compiled on Java 9 or newer but used on Java 8 or older.static longsaturatingAdd(long a, long b)static byte[]truncate(byte[] arr, int len)Returns a possibly truncated version ofarrwhich is guaranteed to be exactlylenelements long.
-
Method Details
-
compareObjectIdentity
Provides an arbitrary but consistent total ordering over all objects. This comparison function will return 0 if and only if a == b, and otherwise will return arbitrarily either -1 or 1, but will do so in a way that results in a consistent total order.- Parameters:
a-b-- Returns:
- -1 or 1 (consistently) if a != b; 0 if a == b.
-
saturatingAdd
public static long saturatingAdd(long a, long b) -
assertNonNull
- Parameters:
object- value to be null-checkedparamName- message for the potentialNullPointerException- Returns:
object- Throws:
NullPointerException- ifobjectis null
-
truncate
Returns a possibly truncated version ofarrwhich is guaranteed to be exactlylenelements long. Ifarris already exactlylenelements long, thenarris returned without copy or modification. Ifarris longer thanlen, then a truncated copy is returned. Ifarris shorter thanlenthen this throws anIllegalArgumentException.- Throws:
IllegalArgumentException
-
getSecureRandom
-
flip
Equivalent to callingByteBuffer.flip()but in a manner which is safe when compiled on Java 9 or newer but used on Java 8 or older. -
clear
Equivalent to callingByteBuffer.clear()but in a manner which is safe when compiled on Java 9 or newer but used on Java 8 or older. -
position
Equivalent to callingByteBuffer.position(int)but in a manner which is safe when compiled on Java 9 or newer but used on Java 8 or older. -
limit
Equivalent to callingByteBuffer.limit(int)but in a manner which is safe when compiled on Java 9 or newer but used on Java 8 or older. -
decodeBase64String
Takes a Base64-encoded String, decodes it, and returns contents as a byte array.- Parameters:
encoded- Base64 encoded String- Returns:
- decoded data as a byte array
-
encodeBase64String
Takes data in a byte array, encodes them in Base64, and returns the result as a String.- Parameters:
data- The data to encode.- Returns:
- Base64 string that encodes the
data.
-
bigIntegerToByteArray
Removes the leading zero sign byte from the byte array representation of a BigInteger (if present) and left pads with zeroes to produce a byte array of the given length.- Parameters:
bigInteger- The BigInteger to convert to a byte arraylength- The length of the byte array, must be at least as long as the BigInteger byte array without the sign byte- Returns:
- The byte array
-
arrayPrefixEquals
public static boolean arrayPrefixEquals(byte[] a, byte[] b, int length)Returns true if the prefix of the given length for the input arrays are equal. This method will return as soon as the first difference is found, and is thus not constant-time.- Parameters:
a- The first array.b- The second array.length- The length of the prefix to compare.- Returns:
- True if the prefixes are equal, false otherwise.
-