Java Utililty Methods Array Truncate

List of utility methods to do Array Truncate

Description

The list of methods to do Array Truncate are organized into topic(s).

Method

byte[]truncate(byte[] array, int newLength)
Truncates the given array to the request length.
if (array.length < newLength) {
    return array;
} else {
    byte[] truncated = new byte[newLength];
    System.arraycopy(array, 0, truncated, 0, newLength);
    return truncated;
byte[]truncate(byte[] element, int length)
truncate
byte[] result = new byte[length];
System.arraycopy(element, 0, result, 0, result.length);
return result;
byte[]truncate(byte[] nextPartBytes, int partSize)
truncate
if (partSize >= nextPartBytes.length) {
    return nextPartBytes;
byte[] result = new byte[partSize];
System.arraycopy(nextPartBytes, 0, result, 0, partSize);
return result;
byte[]truncate(final byte[] arr, final int len)
Returns a possibly truncated version of arr which is guaranteed to be exactly len elements long.
if (arr.length == len) {
    return arr;
} else if (arr.length > len) {
    return Arrays.copyOf(arr, len);
} else {
    throw new IllegalArgumentException("arr is not at least " + len + " elements long");
int[]truncate(int[] array, int maxLen)
truncate
if (array.length < maxLen) {
    return array;
int[] result = new int[maxLen];
System.arraycopy(array, 0, result, 0, maxLen);
return result;
int[]truncate(int[] values, int value)
truncates the given array by removing all fields at the end that contain the given value, e.g., {0,1,0,2,3,0,0} => {0,1,0,2,3} for value=0
if (values.length == 0) {
    return values;
if (values[values.length - 1] != value) {
    return values;
int index = values.length - 1;
for (int i = values.length - 1; i >= 0; i--) {
...
short[]truncate(short[] value, int maxSize)
Truncate an array of shorts to a specified size
int actualSize = maxSize;
if (actualSize > value.length) {
    actualSize = value.length;
short[] returnValue = new short[actualSize];
for (int i = 0; i < actualSize; i++) {
    returnValue[i] = value[i];
return returnValue;
int[]truncateAndConvertToInt(long[] longArray)
Converts long[] to int[], truncating to maximum/minimum value if needed.
int[] intArray = new int[longArray.length];
for (int i = 0; i < longArray.length; i++) {
    intArray[i] = longArray[i] > Integer.MAX_VALUE ? Integer.MAX_VALUE
            : longArray[i] < Integer.MIN_VALUE ? Integer.MIN_VALUE : (int) longArray[i];
return intArray;
byte[]truncateBytes(byte[] inputBytes)
truncate Bytes
assert inputBytes != null;
assert inputBytes.length > 1;
int offsetLocation = inputBytes[inputBytes.length - 1] & 0x0F;
assert offsetLocation >= 0;
assert offsetLocation + 3 < inputBytes.length;
byte[] truncated = new byte[4];
truncated[0] = (byte) (inputBytes[offsetLocation] & 0x7F);
truncated[1] = inputBytes[offsetLocation + 1];
...
byte[]truncateByteSegment(byte[] byteSegment, int toSize)
truncate Byte Segment
byte[] newSegment = new byte[toSize];
System.arraycopy(byteSegment, 0, newSegment, 0, toSize);
return newSegment;