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

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

Description

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

License

Apache License

Parameter

Parameter Description
source 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[] source, int newLength) 

Method Source Code

//package com.java2s;
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
    /*from w ww . ja v a2s .c  o  m*/
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

public class Main {
    /**
     * Copies the specified array, truncating or padding with zeros (if
     * necessary) so the copy has the specified length. This method is temporary
     * replace for Arrays.copyOf() until we start to require JDK 1.6.
     * 
     * @param source
     *            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
     */
    public static byte[] copyOf(byte[] source, int newLength) {
        byte[] result = new byte[newLength];
        System.arraycopy(source, 0, result, 0, Math.min(source.length, newLength));
        return result;
    }

    /**
     * This is really a debugging version of <code>System.arraycopy()</code>.
     * Use it to provide better exception messages when copying arrays around.
     * For production use it's better to use the original for speed.
     */
    public static void arraycopy(byte[] src, int src_position, byte[] dst, int dst_position, int length) {
        if (src_position < 0)
            throw new IllegalArgumentException("src_position was less than 0.  Actual value " + src_position);
        if (src_position >= src.length)
            throw new IllegalArgumentException(
                    "src_position was greater than src array size.  Tried to write starting at position "
                            + src_position + " but the array length is " + src.length);
        if (src_position + length > src.length)
            throw new IllegalArgumentException(
                    "src_position + length would overrun the src array.  Expected end at " + (src_position + length)
                            + " actual end at " + src.length);
        if (dst_position < 0)
            throw new IllegalArgumentException("dst_position was less than 0.  Actual value " + dst_position);
        if (dst_position >= dst.length)
            throw new IllegalArgumentException(
                    "dst_position was greater than dst array size.  Tried to write starting at position "
                            + dst_position + " but the array length is " + dst.length);
        if (dst_position + length > dst.length)
            throw new IllegalArgumentException(
                    "dst_position + length would overrun the dst array.  Expected end at " + (dst_position + length)
                            + " actual end at " + dst.length);

        System.arraycopy(src, src_position, dst, dst_position, length);
    }
}

Related

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