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 int[] appendToStart(int[] array, int val) {
    int len = 1;//from   www .  j  a  v a2 s  .c  o  m
    if (array != null)
        len += array.length;

    int[] arrayOut = new int[len];
    arrayOut[0] = val;
    if (array != null)
        System.arraycopy(array, 0, arrayOut, 1, array.length);

    return arrayOut;
}

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 = null;
    if (src_yuv == null)
        return dst_yuv;
    // simple implementation: copy the corner
    if (src_width == dst_width && src_height == dst_height) {
        dst_yuv = src_yuv;//from  ww w.  j  a  v  a 2  s  .c o 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

public static int[] appendToEnd(int[] array, int val) {
    int len = 1;//from w  w  w. j  a  va  2  s .  c  om
    if (array != null)
        len += array.length;

    int[] arrayOut = new int[len];
    arrayOut[len - 1] = val;
    if (array != null)
        System.arraycopy(array, 0, arrayOut, 0, array.length);

    return arrayOut;
}

From source file:Main.java

public static int[] addState(int[] states, int state) {
    for (int oldState : states) {
        if (oldState == state) {
            return states;
        }/*from w w  w  . ja  va  2  s. c  o  m*/
    }
    int[] newState = new int[states.length + 1];
    System.arraycopy(states, 0, newState, 1, states.length);
    newState[0] = state;
    return newState;
}

From source file:com.etesync.syncadapter.journalmanager.util.ByteUtil.java

public static byte[][] split(byte[] input, int firstLength, int secondLength) {
    byte[][] parts = new byte[2][];

    parts[0] = new byte[firstLength];
    System.arraycopy(input, 0, parts[0], 0, firstLength);

    parts[1] = new byte[secondLength];
    System.arraycopy(input, firstLength, parts[1], 0, secondLength);

    return parts;
}

From source file:Main.java

public static long fromBitString2Long(String str) {
    if (str.length() > 64) {
        throw new UnsupportedOperationException(
                "Strings needs to fit into long (8*8 bits) but length was " + str.length());
    }/*  w w w  .ja va  2  s . co m*/
    byte[] res = fromBitString(str);
    if (res.length < 8) {
        byte[] newBytes = new byte[8];
        System.arraycopy(res, 0, newBytes, 8 - res.length, res.length);
        res = newBytes;
    }
    return toLong(res);
}

From source file:Main.java

public static long[] subArray(final long[] array, final int start, final int end) {
    final int length = end - start;
    if (length < 0)
        throw new IllegalArgumentException();
    final long[] result = new long[length];
    System.arraycopy(array, start, result, 0, length);
    return result;
}

From source file:Main.java

/**
 * Create a new array by slicing a subset out of an existing one.
 *
 * @param <T>   the type of the array elements
 * @param array the array/*  w  w  w. j av a 2  s  . c o  m*/
 * @param off   the starting offset of the slice
 * @param len   the length of the slice.
 * @return a slice of the array starting at the given offset and with the specified length
 */
public static <T extends Object> T[] slice(T[] array, int off, int len) {
    len = Math.min(len, array.length - off);
    T[] res = (T[]) Array.newInstance(array.getClass().getComponentType(), len);
    System.arraycopy(array, off, res, 0, len);
    return res;
}

From source file:Main.java

public static int[] removeState(int[] states, int state) {
    for (int i = 0; i < states.length; i++) {
        if (states[i] == state) {
            int[] newState = new int[states.length - 1];
            if (i > 0) {
                System.arraycopy(states, 0, newState, 0, i);
            }//from w  w w. ja va  2s .  c o m
            System.arraycopy(states, i + 1, newState, i, states.length - i - 1);
            return newState;
        }
    }
    return states;
}

From source file:Main.java

/**
 * Removes value from given array if present, providing set-like behavior.
 *//*from   w w  w  .j  av a 2  s  . co m*/
public static long[] removeLong(long[] cur, long val) {
    if (cur == null) {
        return null;
    }
    final int N = cur.length;
    for (int i = 0; i < N; i++) {
        if (cur[i] == val) {
            long[] ret = new long[N - 1];
            if (i > 0) {
                System.arraycopy(cur, 0, ret, 0, i);
            }
            if (i < (N - 1)) {
                System.arraycopy(cur, i + 1, ret, i, N - i - 1);
            }
            return ret;
        }
    }
    return cur;
}