Monday, September 14, 2015

Java Bytes Utility class


/**
 * A set of byte related utilities, best used as static imports.
 *
 */
public class Bytes {

/**
* Converts {@code bytes} to integer.
*
* <p>Usage:<br/>
* <code>Bytes.toInt(new byte[]{0x00, 0x01, 0x00})</code> will return {@code 256}
* @param bytes a non-null byte array with length less than or equal to 4
* @return converted integer value
* @throws IllegalArgumentException if {@code bytes.length} is greater than 4
*/
public static int toInt(byte[] bytes) {
checkNotNull(bytes, "bytes are null");
checkArgument(bytes.length <= 4, "byteCount should be in closed-range [0:4], found: " + bytes.length);

return toIntInternal(bytes, 0, bytes.length);
}

private static int toIntInternal(byte[] bytes, int startIndex, int endIndex) {
int value = 0;
for (int i = startIndex; i < endIndex; i++) {
value = value << 8 | (bytes[i] & 0xFF);
}

return value;
}

/**
* Converts {@code bytes} to long.
*
* <p>Usage:<br/>
* <code>Bytes.toLong(new byte[]{0x00, 0x01, 0x00, 0x01})</code> will return {@code 65537}
* @param bytes a non-null byte array with length less than or equal to 8
* @return converted long value
* @throws IllegalArgumentException if {@code bytes.length} is greater than 8
*/
public static long toLong(byte[] bytes) {
checkNotNull(bytes, "bytes are null");
checkArgument(bytes.length <= 8, "byteCount should be in closed-range [0:8], found: " + bytes.length);

return toLongInternal(bytes, 0, bytes.length);
}

private static long toLongInternal(byte[] bytes, int startIndex, int endIndex) {
long value = 0;
for (int i = startIndex; i < endIndex; i++) {
value = value << 8 | (bytes[i] & 0xFF);
}

return value;
}

/**
* Returns the values from each provided array combined into a single array.
* For example,<br/>
* <code>concat(new byte[] {a, b}, new byte[] {}, new
* byte[] {c}</code> returns the array <code> {a, b, c}</code>.
*
* @param arrays zero or more {@code byte} arrays
* @return a single array containing all the values from the source arrays, in
* order
*/
public static byte[] concat(byte[]... byteArrays) {
int length = 0;
for (byte[] byteArray : byteArrays) {
length += byteArray.length;
}

byte[] result = new byte[length];
int position = 0;
for (byte[] byteArray : byteArrays) {
System.arraycopy(byteArray, 0, result, position, byteArray.length);
position += byteArray.length;
}
return result;
}
}

public class Preconditions {
private Preconditions(){}

/**
* Ensures that an object reference passed as a parameter to the calling method is not null.
*
* @param reference an object reference
* @param errorMessage the exception message to use if the check fails; will be converted to a
*     string using {@link String#valueOf(Object)}
* @return the non-null reference that was validated
* @throws NullPointerException if {@code reference} is null
*/
public static <T> T checkNotNull(T reference, Object errorMessage) {
if (reference == null) {
throw new NullPointerException(String.valueOf(errorMessage));
}
return reference;
}

/**
* Ensures the truth of an expression involving one or more parameters to the calling method.
*
* @param expression a boolean expression
* @param errorMessage the exception message to use if the check fails; will be converted to a
*     string using {@link String#valueOf(Object)}
* @throws IllegalArgumentException if {@code expression} is false
*/
public static void checkArgument(boolean expression, Object errorMessage) {
if (!expression) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
}
}
}

No comments:

Post a Comment

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...