public final class Utils extends Object
Modifier and Type | Class and Description |
---|---|
static class |
Utils.ComparingByteArrays
Comparator that performs a lexicographical comparison of byte arrays, treating them as
unsigned.
|
static class |
Utils.ComparingByteBuffers |
Modifier and Type | Method and Description |
---|---|
static boolean |
arrayPrefixEquals(byte[] a,
byte[] b,
int length)
Returns true if the prefix of the given length for the input arrays are equal.
|
static <T> T |
assertNonNull(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 ByteBuffer |
clear(ByteBuffer buff)
Equivalent to calling
Buffer.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 byte[] |
decodeBase64String(String encoded)
Takes a Base64-encoded String, decodes it, and returns contents as a byte array.
|
static String |
encodeBase64String(byte[] data)
Takes data in a byte array, encodes them in Base64, and returns the result as a String.
|
static ByteBuffer |
flip(ByteBuffer buff)
Equivalent to calling
Buffer.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 calling
Buffer.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 calling
Buffer.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 of
arr which is guaranteed to be exactly len elements long. |
public static int compareObjectIdentity(Object a, Object b)
a
- b
- public static long saturatingAdd(long a, long b)
public static <T> T assertNonNull(T object, String paramName) throws NullPointerException
object
- value to be null-checkedparamName
- message for the potential NullPointerException
object
NullPointerException
- if object
is nullpublic static byte[] truncate(byte[] arr, int len) throws IllegalArgumentException
arr
which is guaranteed to be exactly len
elements long. If arr
is already exactly len
elements long, then arr
is returned without copy or modification. If arr
is longer than len
, then
a truncated copy is returned. If arr
is shorter than len
then this throws an
IllegalArgumentException
.IllegalArgumentException
public static SecureRandom getSecureRandom()
public static ByteBuffer flip(ByteBuffer buff)
Buffer.flip()
but in a manner which is safe when compiled on
Java 9 or newer but used on Java 8 or older.public static ByteBuffer clear(ByteBuffer buff)
Buffer.clear()
but in a manner which is safe when compiled on
Java 9 or newer but used on Java 8 or older.public static ByteBuffer position(ByteBuffer buff, int newPosition)
Buffer.position(int)
but in a manner which is safe when
compiled on Java 9 or newer but used on Java 8 or older.public static ByteBuffer limit(ByteBuffer buff, int newLimit)
Buffer.limit(int)
but in a manner which is safe when compiled
on Java 9 or newer but used on Java 8 or older.public static byte[] decodeBase64String(String encoded)
encoded
- Base64 encoded Stringpublic static String encodeBase64String(byte[] data)
data
- The data to encode.data
.public static byte[] bigIntegerToByteArray(BigInteger bigInteger, int length)
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 bytepublic static boolean arrayPrefixEquals(byte[] a, byte[] b, int length)
a
- The first array.b
- The second array.length
- The length of the prefix to compare.Copyright © 2023. All rights reserved.