Class Utils
- java.lang.Object
-
- com.amazonaws.encryptionsdk.internal.Utils
-
public final class Utils extends Object
Internal utility methods.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Utils.ComparingByteArrays
Comparator that performs a lexicographical comparison of byte arrays, treating them as unsigned.static class
Utils.ComparingByteBuffers
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
assertNonNull(T object, String paramName)
static ByteBuffer
clear(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 int
compareObjectIdentity(Object a, Object b)
Provides an arbitrary but consistent total ordering over all objects.static ByteBuffer
flip(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 SecureRandom
getSecureRandom()
static ByteBuffer
limit(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 ByteBuffer
position(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 long
saturatingAdd(long a, long b)
static byte[]
truncate(byte[] arr, int len)
Returns a possibly truncated version ofarr
which is guaranteed to be exactlylen
elements long.
-
-
-
Method Detail
-
compareObjectIdentity
public static int compareObjectIdentity(Object a, Object b)
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
public static <T> T assertNonNull(T object, String paramName) throws NullPointerException
- Parameters:
object
- value to be null-checkedparamName
- message for the potentialNullPointerException
- Returns:
object
- Throws:
NullPointerException
- ifobject
is null
-
truncate
public static byte[] truncate(byte[] arr, int len) throws IllegalArgumentException
Returns a possibly truncated version ofarr
which is guaranteed to be exactlylen
elements long. Ifarr
is already exactlylen
elements long, thenarr
is returned without copy or modification. Ifarr
is longer thanlen
, then a truncated copy is returned. Ifarr
is shorter thanlen
then this throws anIllegalArgumentException
.- Throws:
IllegalArgumentException
-
getSecureRandom
public static SecureRandom getSecureRandom()
-
flip
public static ByteBuffer flip(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.
-
clear
public static ByteBuffer clear(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.
-
position
public static ByteBuffer position(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.
-
limit
public static ByteBuffer limit(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.
-
-