Java Array Range Copy copyOf(Object src, int newLength)

Here you can find the source of copyOf(Object src, int newLength)

Description

copy Of

License

Open Source License

Declaration

public static Object copyOf(Object src, int newLength) 

Method Source Code

//package com.java2s;
/**!/*from  ww  w.j  av  a 2 s .  c  o  m*/
 * @author Daniel Friesen
 * @copyright Copyright ? 2009 Daniel Friesen
 * @license MIT License
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

public class Main {
    public static Object copyOf(Object src, int newLength) {
        Object copy = newArray(src, newLength);
        transfer(src, 0, copy, 0, Math.min(length(src), newLength));
        return copy;
    }

    /**
     * Return a new array of the same type as the one passed
     */
    public static Object newArray(Object src, int len) {
        if (src.getClass() == byte[].class)
            return new byte[len];
        if (src.getClass() == char[].class)
            return new char[len];
        throw illegal();
    }

    public static void transfer(Object src, int srcPos, Object dest, int destPos, int length) {
        if (src.getClass() != byte[].class & src.getClass() != char[].class)
            throw illegal();
        System.arraycopy(src, srcPos, dest, destPos, length);
    }

    public static int length(Object src) {
        if (src.getClass() == byte[].class)
            return ((byte[]) src).length;
        if (src.getClass() == char[].class)
            return ((char[]) src).length;
        throw illegal();
    }

    private static IllegalArgumentException illegal() {
        return new IllegalArgumentException("Only char[] and byte[] arrays are handled by ArrayUtils");
    }
}

Related

  1. copyOf(int[] old, int length)
  2. copyOf(int[] source, int length)
  3. copyOf(int[] src, int size)
  4. copyOf(int[][] pixels)
  5. copyOf(Integer[] original)
  6. copyOf(Object[] array, int newLength)
  7. copyOf(Object[] values, int nlen)
  8. copyOf(String[] data, int newLength)
  9. copyOf(String[] original, int newLength)