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

/**
 * Returns subarray./*w w w .j  av a2s  . com*/
 */
@SuppressWarnings({ "unchecked" })
public static <T> T[] subarray(T[] buffer, int offset, int length, Class<T> componentType) {
    T[] temp = (T[]) Array.newInstance(componentType, length);
    System.arraycopy(buffer, offset, temp, 0, length);
    return temp;
}

From source file:Main.java

private static void derivePKCS5S2Helper(Mac hMac, byte[] P, byte[] S, int c, byte[] iBuf, byte[] out,
        int outOff) throws GeneralSecurityException {
    byte[] state = new byte[hMac.getMacLength()];
    SecretKeySpec param = new SecretKeySpec(P, "SHA1");
    hMac.init(param);//from w  w w. j a v a2s.c  o m
    if (S != null) {
        hMac.update(S, 0, S.length);
    }
    hMac.update(iBuf, 0, iBuf.length);
    hMac.doFinal(state, 0);
    System.arraycopy(state, 0, out, outOff, state.length);
    if (c == 0) {
        throw new IllegalArgumentException("iteration count must be at least 1.");
    }
    for (int count = 1; count < c; count++) {
        hMac.init(param);
        hMac.update(state, 0, state.length);
        hMac.doFinal(state, 0);
        for (int j = 0; j != state.length; j++) {
            out[outOff + j] ^= state[j];
        }
    }
}

From source file:Main.java

public static Object expandBy(Object oldArray, int increment) {
    Object newArray = null;/*from   w  ww  . j  a v  a2s . c  om*/
    int oldLength = Array.getLength(oldArray);
    int newLength = oldLength + increment;
    Class<?> c = oldArray.getClass();
    newArray = Array.newInstance(c.getComponentType(), newLength);
    System.arraycopy(oldArray, 0, newArray, 0, oldLength);
    return newArray;
}

From source file:Main.java

public static <T> T[] append(T[] sourceArray, T extraElement, T[] targetArray) {
    System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);
    targetArray[targetArray.length - 1] = extraElement;
    return targetArray;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] removeAll(T[] a, T[] b, boolean usesEquals) {
    if (a.length == 0) {
        return a;
    } else {//from   www .j a  v a2 s  .c  o  m
        T[] result = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), a.length);
        int i = 0;
        for (T t : a) {
            if (!exists(b, t, usesEquals)) {
                result[i++] = t;
            }
        }
        if (i < result.length) {
            T[] res = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), i);
            System.arraycopy(result, 0, res, 0, i);
            return res;
        } else {
            return result;
        }
    }
}

From source file:Main.java

/**
 * Concatenates two byte arrays into one new byte array
 *
 * @param source1 the first byte array (will be at the beginning of the new array)
 * @param source2 the second byte array (will be at the end of the new array)
 * @return the concatenation of the two arrays
 * @throws ArrayIndexOutOfBoundsException if the value of <code>index</code> causes an illegal array access.
 *//*from w  w  w .  j  a  va2s  .  c  om*/
public static byte[] concatenate(final byte[] source1, final byte[] source2) {
    byte[] result = new byte[source1.length + source2.length];

    System.arraycopy(source1, 0, result, 0, source1.length);
    System.arraycopy(source2, 0, result, source1.length, source2.length);

    return result;
}

From source file:Main.java

static public Object trimArray(Object[] list, int start, int end) {
    int newSize = end - start;
    Class type = list.getClass().getComponentType();
    Object temp = Array.newInstance(type, newSize);
    System.arraycopy(list, start, temp, 0, newSize);
    return temp;/* ww  w. j  a  va 2  s .com*/
}

From source file:Main.java

public static byte[] postWork(byte[] ar, String type, int len) {
    byte[] res = new byte[len];
    for (int i = 0; i < len; i++)
        res[i] = 0;/*from  w w  w  .  j av  a  2s. co m*/
    int l = ar.length;
    if (type.equals("LONG"))
        for (int i = 0; i < l; i++)
            res[i] = ar[l - i - 1];
    if (type.equals("INT"))
        for (int i = 0; i < l; i++)
            res[i] = ar[l - i - 1];
    if (type.equals("STRING"))
        if (isUnicode(ar))
            for (int i = 1; i < l; i = i + 2)
                res[(i - 1) / 2] = ar[i];
        else
            System.arraycopy(ar, 0, res, 0, l);
    if (type.equals("DATE"))
        for (int i = 0; i < l; i++)
            res[i] = ar[l - i - 1];
    if (type.equals("CHR"))
        System.arraycopy(ar, 0, res, 0, l);
    if (type.equals("UCHR"))
        System.arraycopy(ar, 0, res, 0, l);
    if (type.equals("NUMERIC"))
        System.arraycopy(ar, 0, res, 0, l);

    return res;
}

From source file:Main.java

/**
 * Expands an array of Objects to double of its current size. Remember to use cast to turn the result into an array of the appropriate class, i.e:
 * //  w  ww. jav a2 s. c  o m
 *   <code>someArray=(SomeClass [])unlekker.util.Util.expandArray(someArray);
 * @param list Array to be expanded.
 * @return Array of Object [], must be cast to appropriate class.
 */
static public Object expandArray(Object[] list) {
    int newSize = list.length * 2;
    Class type = list.getClass().getComponentType();
    Object temp = Array.newInstance(type, newSize);
    System.arraycopy(list, 0, temp, 0, Math.min(Array.getLength(list), newSize));
    return temp;
}

From source file:Main.java

/**
 * Merged all arrays into a single array.
 *///from   w  ww . j a  v a 2 s  . c o  m
@SuppressWarnings("unchecked")
public static <T> T[] merge(final T[]... arrays) {
    // Compute total size
    Class<T> type = null;
    int size = 0;
    for (final T[] array : arrays) {
        size += array.length;
        if (type == null && array.length > 0) {
            type = (Class<T>) array[0].getClass();
        }
    }

    // Copy all arrays
    final T[] merged = (T[]) Array.newInstance(type, size);

    int index = 0;
    for (final T[] array : arrays) {
        System.arraycopy(array, 0, merged, index, array.length);
        index += array.length;
    }

    return merged;
}