public final class Util extends Object
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_BUFFER_SIZE
The default buffer size for the data read/copy operations in this class.
|
Modifier and Type | Method and Description |
---|---|
static byte[] |
addCrc32(byte[] b)
Computes the CRC32 checksum for the given data.
|
static byte[] |
addCrc32(byte[] b,
int off,
int len)
Computes the CRC32 checksum for
len bytes of the given data, starting from off . |
static byte[] |
calculateCrc32(byte[] b,
int off,
int length)
Computes the CRC32 checksum for
length bytes of the given data, starting from off . |
static byte[] |
calculateHMAC(byte[] message,
byte[] keyBytes,
String algorithm)
Calculates the RFC 2104 compatible HMAC for the given message, key, and algorithm.
|
static void |
closeQuietly(InputStream input)
Closes an
InputStream unconditionally. |
static void |
closeQuietly(OutputStream output)
Closes an
OutputStream unconditionally. |
static boolean |
containsInt(int[] array,
int key)
Checks if an element is present in an int array.
|
static int |
copyData(InputStream in,
OutputStream out)
Copies all available data from
in to out . |
static int |
copyData(InputStream in,
OutputStream out,
int limit)
Copies up to
limit bytes of data from in to out . |
static int |
copyData(InputStream in,
OutputStream out,
int limit,
int bufSize)
Copies up to
limit bytes of data from in to out . |
static byte[] |
copyOf(byte[] b)
Creates a copy of the given byte array.
|
static byte[] |
copyOf(byte[] b,
int off,
int len)
Creates a copy of a section of the given byte array.
|
static String |
decodeString(byte[] buf,
int ofs,
int len)
Decodes UTF-8 string from the given buffer.
|
static long |
decodeUnsignedLong(byte[] buf,
int ofs,
int len)
Decodes an unsigned integer from the given buffer.
|
static byte[] |
encodeUnsignedLong(long value)
Encodes the given value in a minimal number of bytes, in network byte order (most significant bits first).
|
static boolean |
equals(Object o1,
Object o2)
Checks if two objects are equal.
|
static boolean |
equalsIgnoreOrder(Collection<?> c1,
Collection<?> c2)
Checks if two collections are equal ignoring the order of components.
|
static int |
gcd(int a,
int b)
Computes the greatest common divisor (GCD) of two integers.
|
static String |
getDefaultTrustStore() |
static byte[] |
join(byte[] a,
byte[] b)
Joins two byte arrays into one.
|
static int |
lcm(int a,
int b)
Computes the least common multiple (LCM) of two integers.
|
static KeyStore |
loadKeyStore(File file,
String password)
Loads and returns the
KeyStore from the file system. |
static Long |
nextLong()
Returns the next pseudorandom, uniformly distributed long value from the Math.random() sequence.
|
static void |
notNull(Object o,
String name)
Checks if the input object is null or not.
|
static byte[] |
toByteArray(InputStream in)
Copies all available data from
in to byte array. |
static byte[] |
toByteArray(InputStream in,
int bufferSize)
Copies all available data from
in to byte array. |
static byte[] |
toByteArray(int value)
Converts
value to four-byte array. |
static byte[] |
toByteArray(long value)
Converts
value to eight-byte array. |
static byte[] |
toByteArray(short value)
Converts
value to two-byte array. |
static byte[] |
toByteArray(String value)
Encodes the given string in UTF-8.
|
static int |
toInt(byte[] b)
Converts the first four bytes of
b to a 32-bit signed integer. |
static int |
toInt(byte[] b,
int offset)
Converts four bytes of
b , starting from offset , to a 32-bit signed integer. |
static long |
toLong(byte[] b)
Converts the first eight bytes of
b to a 64-bit signed integer. |
static long |
toLong(byte[] b,
int offset)
Converts eight bytes of
b , starting from offset , to a 64-bit signed integer. |
static short |
toShort(byte[] b)
Converts the first two bytes of
b to a 16-bit signed integer. |
static short |
toShort(byte[] b,
int offset)
Converts two bytes of
b , starting from offset , to a 16-bit signed integer. |
static URL |
toUrl(String url)
Creates an URL object from the String representation.
|
public static final int DEFAULT_BUFFER_SIZE
public static byte[] addCrc32(byte[] b) throws NullPointerException, ArrayIndexOutOfBoundsException
The checksum is appended to the original data and the result returned in a newly allocated array.
b
- the data to compute the checksum for.NullPointerException
- if b
is null
.ArrayIndexOutOfBoundsException
- if the half-range [off..off+len)
is not in [0..b.length)
.public static byte[] addCrc32(byte[] b, int off, int len) throws NullPointerException, ArrayIndexOutOfBoundsException
len
bytes of the given data, starting from off
.
The checksum is appended to the original data and the result returned in a newly allocated array.
b
- the data to compute the checksum for.off
- the buffer containing the data to compute the checksum for.len
- number of bytes to include in the checksum.NullPointerException
- if b
is null
.ArrayIndexOutOfBoundsException
- if the half-range [off..off+len)
is not in [0..b.length)
.public static byte[] calculateCrc32(byte[] b, int off, int length)
length
bytes of the given data, starting from off
.b
- the the data to compute the checksum for.off
- the buffer containing the data to compute the checksum for.length
- number of bytes to include in the checksum.public static String decodeString(byte[] buf, int ofs, int len) throws CharacterCodingException
buf
- the buffer.ofs
- offset of the UTF-8 data in the buffer.len
- length of the UTF-8 data to decode.CharacterCodingException
- when the specified data is not in valid UTF-8 format.public static byte[] toByteArray(String value)
value
- the string to encode.public static byte[] toByteArray(InputStream in) throws IOException
in
to byte array.in
- input stream to copy data from.IOException
public static byte[] toByteArray(InputStream in, int bufferSize) throws IOException
in
to byte array.in
- input stream to copy data from.bufferSize
- buffer size to use.IOException
public static byte[] copyOf(byte[] b)
b
- the array to copy.b
, or null if b
is null.public static byte[] copyOf(byte[] b, int off, int len) throws NullPointerException, ArrayIndexOutOfBoundsException
b
- the array to copy.off
- the start offset of the data within b
.len
- the number of bytes to copy.b
.NullPointerException
- if b
is null.ArrayIndexOutOfBoundsException
- if the half-range [off..off+len)
is not in [0..b.length)
.public static byte[] join(byte[] a, byte[] b)
a
- first byte array to join, not null.b
- second byte array to join, not null.public static int lcm(int a, int b) throws ArithmeticException
Least common multiple is the smallest positive integer that can be divided by both numbers without a remainder.
a
- the first integer.b
- the second integer.a
and b
, or null,
if either a
or b
is null.ArithmeticException
- when the result is too big to fit into an int
.public static int gcd(int a, int b)
Greatest common divisor is the largest integer that divides both numbers without remainder.
a
- the first integer.b
- the second integer.a
and b
, or null,
if both a
and b
are null.public static byte[] toByteArray(short value)
value
to two-byte array.
Bytes are returned in network byte order (ordered from the most to the least significant byte).
value
- the value to convert.public static byte[] toByteArray(int value)
value
to four-byte array.
Bytes are returned in network byte order (ordered from the most to the least significant byte).
value
- the value to convert.public static byte[] toByteArray(long value)
value
to eight-byte array.
Bytes are returned in network byte order (ordered from the most to the least significant byte).
value
- the value to convert.public static short toShort(byte[] b)
b
to a 16-bit signed integer.
Assumes network byte order (ordered from the most to the least significant byte).
b
- the buffer to read from.public static short toShort(byte[] b, int offset)
b
, starting from offset
, to a 16-bit signed integer.
Assumes network byte order (ordered from the most to the least significant byte).
b
- the buffer to read from.offset
- start offset in the buffer.public static int toInt(byte[] b)
b
to a 32-bit signed integer.
Assumes network byte order (ordered from the most to the least significant byte).
b
- the buffer to read from.public static int toInt(byte[] b, int offset)
b
, starting from offset
, to a 32-bit signed integer.
Assumes network byte order (ordered from the most to the least significant byte).
b
- the buffer to read from.offset
- start offset in the buffer.public static long toLong(byte[] b)
b
to a 64-bit signed integer.
Assumes network byte order (ordered from the most to the least significant byte).
b
- the buffer to read from.public static long toLong(byte[] b, int offset)
b
, starting from offset
, to a 64-bit signed integer.
Assumes network byte order (ordered from the most to the least significant byte).
b
- the buffer to read from.offset
- start offset in the buffer.public static long decodeUnsignedLong(byte[] buf, int ofs, int len) throws IllegalArgumentException
buf
- the buffer.ofs
- offset of the data in the buffer.len
- length of the data to decode.IllegalArgumentException
- when result does not fit into 63-bit unsigned integer.public static byte[] encodeUnsignedLong(long value)
value
- the value to encode (the encoding is unsigned, so only non-negative values are supported).public static byte[] calculateHMAC(byte[] message, byte[] keyBytes, String algorithm) throws NoSuchAlgorithmException, InvalidKeyException
message
- message for which the MAC is to be calculated.keyBytes
- key for calculation.algorithm
- algorithm to be used (MD5, SHA1, SHA256).NoSuchAlgorithmException
- if invalid algorithm is provided.InvalidKeyException
- if invalid key is provided.IllegalArgumentException
- if HMAC key is null.public static int copyData(InputStream in, OutputStream out) throws IOException
in
to out
.
Allocates a temporary memory buffer of DEFAULT_BUFFER_SIZE
bytes for this.
in
- input stream to copy data from.out
- output stream to copy data to.IOException
- if one is thrown by either in
or out
.public static int copyData(InputStream in, OutputStream out, int limit, int bufSize) throws IOException
limit
bytes of data from in
to out
.
May copy less than limit
bytes if in
does not have that much data available.
Allocates a temporary memory buffer of bufSize
bytes for this.
in
- input stream to copy data from.out
- output stream to copy data to.limit
- maximum number of bytes to copy (-1
to copy all bytes).bufSize
- size of the buffer to allocate (larger buffer may speed up the process).IOException
- if one is thrown by either in
or out
.public static int copyData(InputStream in, OutputStream out, int limit) throws IOException
limit
bytes of data from in
to out
.
May copy less than limit
bytes if in
does not have that much data available.
Allocates a temporary memory buffer of DEFAULT_BUFFER_SIZE
bytes for this.
in
- input stream to copy data from.out
- output stream to copy data to.limit
- maximum number of bytes to copy.IOException
- if one is thrown by either in
or out
.public static void closeQuietly(InputStream input)
InputStream
unconditionally.
Equivalent to InputStream.close()
, except any exceptions will be ignored. This is typically used in
finally
blocks.
input
- InputStream
to close.public static void closeQuietly(OutputStream output)
OutputStream
unconditionally.
Equivalent to OutputStream.close()
, except any exceptions will be ignored. This is typically used in
finally
blocks.
output
- OutputStream
to close.public static Long nextLong()
Returns the next pseudorandom, uniformly distributed long value from the Math.random() sequence.
NB! All values are greater than or equal to zero.public static void notNull(Object o, String name)
o
- input object.name
- input object name.public static boolean equals(Object o1, Object o2)
o1
- first input object.o2
- second input object.public static boolean equalsIgnoreOrder(Collection<?> c1, Collection<?> c2)
c1
- first collection.c2
- second collection.public static boolean containsInt(int[] array, int key)
array
- an array of int values.key
- an int value.public static URL toUrl(String url)
url
- the String to parse as an URL.public static String getDefaultTrustStore()
public static KeyStore loadKeyStore(File file, String password) throws KSIException
KeyStore
from the file system.file
- file to load from file system.password
- password to access the keystore.KeyStore
KSIException
Copyright © 2024 Guardtime. All rights reserved.