Java Array Truncate truncate(byte[] array, int newLength)

Here you can find the source of truncate(byte[] array, int newLength)

Description

Truncates the given array to the request length.

License

Open Source License

Parameter

Parameter Description
array the array to be truncated.
newLength the new length in bytes.

Return

the truncated array.

Declaration

public static byte[] truncate(byte[] array, int newLength) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  ww w. ja  v  a2  s .  c o m*/
     * Truncates the given array to the request length.
     * 
     * @param array
     *            the array to be truncated.
     * @param newLength
     *            the new length in bytes.
     * @return the truncated array.
     */
    public static byte[] truncate(byte[] array, int newLength) {
        if (array.length < newLength) {
            return array;
        } else {
            byte[] truncated = new byte[newLength];
            System.arraycopy(array, 0, truncated, 0, newLength);

            return truncated;
        }
    }
}

Related

  1. truncate(byte[] element, int length)
  2. truncate(byte[] nextPartBytes, int partSize)
  3. truncate(final byte[] arr, final int len)
  4. truncate(int[] array, int maxLen)