Java Array Range Copy copyOf(byte[] original, int newLength)

Here you can find the source of copyOf(byte[] original, int newLength)

Description

Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.

License

Open Source License

Parameter

Parameter Description
original the array to be copied
newLength the length of the copy to be returned

Exception

Parameter Description
NegativeArraySizeException if <tt>newLength</tt> is negative
NullPointerException if <tt>original</tt> is null

Return

a copy of the original array, truncated or padded with zeros to obtain the specified length

Declaration

public static byte[] copyOf(byte[] original, int newLength) 

Method Source Code

//package com.java2s;
/*  /* www . ja  v  a  2  s  . co m*/
 * Copyright 2012, Asamm Software, s. r. o.
 * 
 * This file is part of LocusAPI.
 * 
 * LocusAPI is free software: you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License
 * as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *  
 * LocusAPI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Lesser GNU General Public License for more details.
 *  
 * You should have received a copy of the Lesser GNU General Public
 * License along with LocusAPI. If not, see 
 * <http://www.gnu.org/licenses/lgpl.html/>.
 */

public class Main {
    /**
     * Copies the specified array, truncating or padding with zeros (if necessary)
     * so the copy has the specified length.  For all indices that are
     * valid in both the original array and the copy, the two arrays will
     * contain identical values.  For any indices that are valid in the
     * copy but not the original, the copy will contain <tt>(byte)0</tt>.
     * Such indices will exist if and only if the specified length
     * is greater than that of the original array.
     *
     * @param original the array to be copied
     * @param newLength the length of the copy to be returned
     * @return a copy of the original array, truncated or padded with zeros
     *     to obtain the specified length
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
     * @throws NullPointerException if <tt>original</tt> is null
     * @since 1.6
     */
    public static byte[] copyOf(byte[] original, int newLength) {
        byte[] copy = new byte[newLength];
        System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
        return copy;
    }
}

Related

  1. copyOf(byte[] b, int off, int len)
  2. copyOf(byte[] b, int off, int len)
  3. copyOf(byte[] bytes)
  4. copyOf(byte[] bytes, int startIndex, int length)
  5. copyOf(byte[] original, int newLength)
  6. copyOf(byte[] original, int newLength)
  7. copyOf(byte[] original, int newLength)
  8. copyOf(byte[] source, int newLength)
  9. copyOf(byte[] src, int length)