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 <T> T[] ensureExactLength(T[] array, int length) {

    if (array.length == length) {
        return array;
    }//from   ww w.  j a v a 2 s. c  om

    T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), length);
    if (array.length > 0) {
        System.arraycopy(array, 0, newArray, 0, length);
    }

    return newArray;
}

From source file:Main.java

public static byte[] decodeFile(byte[] key, byte[] fileData, boolean isPlainText) throws Exception {
    byte[] decrypted;
    if (!isPlainText) {
        decrypted = new byte[fileData.length - key.length * 2];
        System.arraycopy(fileData, key.length, decrypted, 0, decrypted.length);
    } else {/*from w  w w.  j a v  a  2 s .co m*/
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);

        decrypted = cipher.doFinal(fileData);
    }
    return decrypted;
}

From source file:Main.java

private static byte[] copyArray(byte[] original, int start, int length) {
    byte[] copy = new byte[length];
    System.arraycopy(original, start, copy, 0, Math.min(original.length - start, length));
    return copy;//  w ww  . j  a  v  a  2s .c om
}

From source file:Main.java

public static <T> T[] concatenate(T[] a, T[] b) {
    int aLen = a.length;
    int bLen = b.length;

    @SuppressWarnings("unchecked")
    T[] c = (T[]) Array.newInstance(a.getClass().getComponentType(), aLen + bLen);
    System.arraycopy(a, 0, c, 0, aLen);
    System.arraycopy(b, 0, c, aLen, bLen);

    return c;//from  w w w.j a v  a 2s  .c o  m
}

From source file:Main.java

public static int[] removeInt(int[] cur, int val) {
    if (cur == null) {
        return null;
    }/*from  www .  j  ava2 s .  co  m*/
    final int N = cur.length;
    for (int i = 0; i < N; i++) {
        if (cur[i] == val) {
            int[] ret = new int[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;
}

From source file:Main.java

public static <T> T[] mergeArrays(T[]... arr) {
    int size = 0;
    for (T[] ts : arr) {
        size += ts.length;/*from www.  ja v  a2 s .c o  m*/
    }
    T[] ret = (T[]) Array.newInstance(arr[0].getClass().getComponentType(), size);
    int off = 0;
    for (T[] ts : arr) {
        System.arraycopy(ts, 0, ret, off, ts.length);
        off += ts.length;
    }
    return ret;
}

From source file:Main.java

/**
 * Resizes an array of ints to a specified size.
 *
 * @param list Array to be resized./*  w w  w  .j  a  v a 2s.  co m*/
 * @param newSize Array size after resizing
 * @return Array of ints with the specified size
 */
static public int[] resizeArrayInt(int[] val, int newSize) {
    int[] newval = new int[newSize];
    System.arraycopy(val, 0, newval, 0, Math.min(val.length, newSize));
    return newval;
}

From source file:Main.java

public static int[] remove(int[] array, int index) {
    if (array == null || array.length == 0) {
        throw new IllegalArgumentException();
    }//from  w ww.  j a  v a 2s .  c  om
    if (index > array.length || index <= 0) {
        throw new IllegalArgumentException();
    }
    int[] dest = new int[array.length - 1];
    System.arraycopy(array, 0, dest, 0, index - 1);
    System.arraycopy(array, index, dest, index - 1, array.length - index);
    return dest;
}

From source file:Main.java

public static <M extends Object> M[] subArray(M[] array, int start, int end) {
    end = Math.min(array.length, end);
    if (start >= end)
        return null;

    M[] result = (M[]) Array.newInstance(array[0].getClass(), end - start);
    System.arraycopy(array, start, result, 0, result.length);

    return result;
}

From source file:Main.java

@Nullable
public static <T> T[] toArrayOfType(Class<T> type, @Nullable Parcelable[] original) {
    if (original == null)
        return null;
    T[] newArray = (T[]) Array.newInstance(type, original.length);
    System.arraycopy(original, 0, newArray, 0, original.length);
    return newArray;
}