Example usage for java.lang System arraycopy

List of usage examples for java.lang System arraycopy

Introduction

In this page you can find the example usage for java.lang System arraycopy.

Prototype

@HotSpotIntrinsicCandidate
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

Source Link

Document

Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

Usage

From source file:Main.java

public static double[] subarray(double[] orig, int off, int len) {
    if (off + len > orig.length)
        throw new IllegalArgumentException("requested subarray exceeds array length");
    double[] sub = new double[len];
    System.arraycopy(orig, off, sub, 0, len);
    return sub;//from   w ww.j av a2s . c  o m
}

From source file:Main.java

/**
 * Inserts one array into another array.
 *///from w  ww  . j a  va 2s  .  c om
@SuppressWarnings({ "unchecked" })
public static <T> T[] insert(T[] dest, T[] src, int offset, Class componentType) {
    T[] temp = (T[]) Array.newInstance(componentType, dest.length + src.length);
    System.arraycopy(dest, 0, temp, 0, offset);
    System.arraycopy(src, 0, temp, offset, src.length);
    System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset);
    return temp;
}

From source file:Main.java

public static byte[] CropYuv(int src_format, byte[] src_yuv, int src_width, int src_height, int dst_width,
        int dst_height) {
    byte[] dst_yuv;
    if (src_yuv == null)
        return null;
    // simple implementation: copy the corner
    if (src_width == dst_width && src_height == dst_height) {
        dst_yuv = src_yuv;//from  w  ww  . j a va2s . co m
    } else {
        dst_yuv = new byte[(int) (dst_width * dst_height * 1.5)];
        switch (src_format) {
        case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar: // I420
        case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420PackedPlanar: // YV12
        {
            // copy Y
            int src_yoffset = 0;
            int dst_yoffset = 0;
            for (int i = 0; i < dst_height; i++) {
                System.arraycopy(src_yuv, src_yoffset, dst_yuv, dst_yoffset, dst_width);
                src_yoffset += src_width;
                dst_yoffset += dst_width;
            }

            // copy u
            int src_uoffset = 0;
            int dst_uoffset = 0;
            src_yoffset = src_width * src_height;
            dst_yoffset = dst_width * dst_height;
            for (int i = 0; i < dst_height / 2; i++) {
                System.arraycopy(src_yuv, src_yoffset + src_uoffset, dst_yuv, dst_yoffset + dst_uoffset,
                        dst_width / 2);
                src_uoffset += src_width / 2;
                dst_uoffset += dst_width / 2;
            }

            // copy v
            int src_voffset = 0;
            int dst_voffset = 0;
            src_uoffset = src_width * src_height + src_width * src_height / 4;
            dst_uoffset = dst_width * dst_height + dst_width * dst_height / 4;
            for (int i = 0; i < dst_height / 2; i++) {
                System.arraycopy(src_yuv, src_uoffset + src_voffset, dst_yuv, dst_uoffset + dst_voffset,
                        dst_width / 2);
                src_voffset += src_width / 2;
                dst_voffset += dst_width / 2;
            }
        }
            break;
        case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar: // NV12
        case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420PackedSemiPlanar: // NV21
        case MediaCodecInfo.CodecCapabilities.COLOR_TI_FormatYUV420PackedSemiPlanar:
        case MediaCodecInfo.CodecCapabilities.COLOR_QCOM_FormatYUV420SemiPlanar: {
            // copy Y
            int src_yoffset = 0;
            int dst_yoffset = 0;
            for (int i = 0; i < dst_height; i++) {
                System.arraycopy(src_yuv, src_yoffset, dst_yuv, dst_yoffset, dst_width);
                src_yoffset += src_width;
                dst_yoffset += dst_width;
            }

            // copy u and v
            int src_uoffset = 0;
            int dst_uoffset = 0;
            src_yoffset = src_width * src_height;
            dst_yoffset = dst_width * dst_height;
            for (int i = 0; i < dst_height / 2; i++) {
                System.arraycopy(src_yuv, src_yoffset + src_uoffset, dst_yuv, dst_yoffset + dst_uoffset,
                        dst_width);
                src_uoffset += src_width;
                dst_uoffset += dst_width;
            }
        }
            break;

        default: {
            dst_yuv = null;
        }
            break;
        }
    }
    return dst_yuv;
}

From source file:Main.java

@SafeVarargs
public static <T> T[] addAll(T[]... arrays) {
    if (arrays.length == 1) {
        return arrays[0];
    }/*w  w w .j  a  v  a 2s . c o m*/

    int length = 0;
    for (T[] array : arrays) {
        if (array == null) {
            continue;
        }
        length += array.length;
    }
    T[] result = newArray(arrays.getClass().getComponentType().getComponentType(), length);

    length = 0;
    for (T[] array : arrays) {
        if (array == null) {
            continue;
        }
        System.arraycopy(array, 0, result, length, array.length);
        length += array.length;
    }
    return result;
}

From source file:Main.java

public static int[] clone(int[] data) {
    if (data == null) {
        return null;
    }//from  w  ww  .  j a v a  2s . co m
    int[] copy = new int[data.length];

    System.arraycopy(data, 0, copy, 0, data.length);

    return copy;
}

From source file:Main.java

public static long[] copyOfRange(long[] data, int from, int to) {
    int newLength = getLength(from, to);

    long[] tmp = new long[newLength];

    if (data.length - from < newLength) {
        System.arraycopy(data, from, tmp, 0, data.length - from);
    } else {//from   w  ww  .  j a v  a  2s .  c  o m
        System.arraycopy(data, from, tmp, 0, newLength);
    }

    return tmp;
}

From source file:Main.java

public static int[] copy(int[] array) {
    if (array == null)
        return new int[0];
    int[] newArray = new int[array.length];
    System.arraycopy(array, 0, newArray, 0, array.length);
    return newArray;
}

From source file:Main.java

/**
 * Concatenates two byte arrays (array1 and array2)
 * @param array1/* www.  j a v  a  2s . c  om*/
 * @param array2
 * @return the concatenated array
 */
public static byte[] concat(byte[] array1, byte[] array2) {
    byte[] concatArray = new byte[array1.length + array2.length];
    System.arraycopy(array1, 0, concatArray, 0, array1.length);
    System.arraycopy(array2, 0, concatArray, array1.length, array2.length);
    return concatArray;
}

From source file:Main.java

public static Object[] resizeArray(Object[] array, int newsize) {
    if (null == array) {
        return null;
    }//  w ww. j  a va 2 s . c o m
    Object[] newArr = new Object[newsize];
    if (newsize > array.length) {
        System.arraycopy(array, 0, newArr, 0, array.length);
    } else {
        System.arraycopy(array, 0, newArr, 0, newsize);
    }

    return newArr;
}

From source file:Main.java

public static byte[] unpadRight(byte[] tgt, byte padding) {
    int len = tgt.length;
    for (; len > 0; len--) {
        if (tgt[len - 1] != padding) {
            break;
        }//from   ww  w. ja  va2 s. c  om
    }
    if (len > 0) {
        byte[] rslt = new byte[len];
        System.arraycopy(tgt, 0, rslt, 0, len);
        return rslt;
    }
    return new byte[0];
}