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 byte[] concatenate(byte[] a, byte[] b, byte[] c, byte[] d) {
    if (a != null && b != null && c != null && d != null) {
        byte[] rv = new byte[a.length + b.length + c.length + d.length];

        System.arraycopy(a, 0, rv, 0, a.length);
        System.arraycopy(b, 0, rv, a.length, b.length);
        System.arraycopy(c, 0, rv, a.length + b.length, c.length);
        System.arraycopy(d, 0, rv, a.length + b.length + c.length, d.length);

        return rv;
    } else if (d == null) {
        return concatenate(a, b, c);
    } else if (c == null) {
        return concatenate(a, b, d);
    } else if (b == null) {
        return concatenate(a, c, d);
    } else {/* www .j ava2  s. c  om*/
        return concatenate(b, c, d);
    }
}

From source file:Main.java

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

From source file:Main.java

private static byte[] macWithPassphrase(byte[] macSalt, int iterations, byte[] data, String passphrase)
        throws GeneralSecurityException {
    Mac hmac = getMacForPassphrase(passphrase, macSalt, iterations);
    byte[] mac = hmac.doFinal(data);
    byte[] result = new byte[data.length + mac.length];

    System.arraycopy(data, 0, result, 0, data.length);
    System.arraycopy(mac, 0, result, data.length, mac.length);

    return result;
}

From source file:Main.java

/**
 * Gets the tag.//from w w w  .  jav a  2 s . c  om
 *
 * @param byteArray the byte array
 * @param lengthOfData the length of data
 * @param tag the tag
 * @param occurences the occurences
 * @return the tag
 */
public static byte[] getTag(byte[] byteArray, int lengthOfData, short tag, int occurences) {
    int timesFound = 0;
    int i;
    boolean skip = false;
    for (i = 0; i < lengthOfData;) {
        short twoByteTag = 0;
        if ((short) (byteArray[i] & 0xff) == tag) {
            if (occurences == timesFound) {
                byte[] dataObject = new byte[byteArray[i + 1] + 2];
                System.arraycopy(byteArray, i, dataObject, 0, dataObject.length);
                return dataObject;
            } else {
                timesFound++;
                skip = true;
            }
        }
        if ((byte) (byteArray[i] & (byte) 0x1f) == (byte) 0x1f) {
            i++;
            twoByteTag = (short) (((short) (byteArray[i - 1] & 0xff) << 8) + (short) (byteArray[i] & 0xff));
            if (twoByteTag == tag) {
                if (occurences == timesFound) {
                    byte[] dataObject = new byte[byteArray[i + 1] + 3];
                    System.arraycopy(byteArray, i - 1, dataObject, 0, dataObject.length);
                    return dataObject;
                } else {
                    timesFound++;
                    skip = true;
                }
            }
        }
        // this will search within a template. assumes that any template tag is only 1 byte. will not work for 2 byte tags
        if (twoByteTag == 0 && (byte) (byteArray[i] & (byte) 0x20) == (byte) 0x20 && !skip) {
            i = i + 2;
        } else {
            i = i + byteArray[i + 1] + 2;
            skip = false;
        }
    }
    return null;
}

From source file:FastMergeSort.java

@SuppressWarnings({ "unchecked" })
private static void mergeSort(Object src[], Object dest[], int low, int high, int off, Comparator c) {
    int length = high - low;

    // use insertion sort on smallest arrays
    if (length < 7) {
        for (int i = low; i < high; i++) {
            for (int j = i; j > low && c.compare(dest[j - 1], dest[j]) > 0; j--) {
                Object temp = dest[j];
                dest[j] = dest[j - 1];// ww w.j  a va 2  s  .com
                dest[j - 1] = temp;
            }
        }
        return;
    }

    // recursively sort halves of dest into src
    int destLow = low;
    int destHigh = high;
    low += off;
    high += off;
    int mid = (low + high) >> 1;
    mergeSort(dest, src, low, mid, -off, c);
    mergeSort(dest, src, mid, high, -off, c);

    // is list already sorted?
    if (c.compare(src[mid - 1], src[mid]) <= 0) {
        System.arraycopy(src, low, dest, destLow, length);
        return;
    }

    // merge sorted halves from src into dest
    for (int i = destLow, p = low, q = mid; i < destHigh; i++) {
        if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0) {
            dest[i] = src[p++];
        } else {
            dest[i] = src[q++];
        }
    }
}

From source file:Main.java

public static char[] combine(char[]... arrays) {
    int totalLength = 0;
    for (char[] chars : arrays) {
        totalLength += chars.length;//from   w  w w .ja v  a2 s  . c  o  m
    }
    char[] newArray = new char[totalLength];
    int pointer = 0;
    for (char[] chars : arrays) {
        System.arraycopy(chars, 0, newArray, pointer, chars.length);
        pointer += chars.length;
    }
    return newArray;
}

From source file:Main.java

/**
 * Appends an element to a copy of the array and returns the copy.
 * @param array The original array, or null to represent an empty array.
 * @param element The element to add.//w  ww .  j  a  va  2 s. c o m
 * @return A new array that contains all of the elements of the original array
 * with the specified element added at the end.
 */
@SuppressWarnings("unchecked")
public static <T> T[] appendElement(final Class<T> kind, final T[] array, final T element) {
    final T[] result;
    final int end;
    if (array != null) {
        end = array.length;
        result = (T[]) Array.newInstance(kind, end + 1);
        System.arraycopy(array, 0, result, 0, end);
    } else {
        end = 0;
        result = (T[]) Array.newInstance(kind, 1);
    }
    result[end] = element;
    return result;
}

From source file:Main.java

/**
 * Returns the vector [x y]/*from  www.j  a  v  a2s. c o m*/
 *
 * @deprecated use {@link org.apache.commons.lang.ArrayUtils.addAll} instead
 */
@Deprecated
public static float[] combine(float[] x, float[] y) {
    int len = 0;
    if (x != null)
        len += x.length;
    if (y != null)
        len += y.length;

    float[] z = null;

    if (len > 0) {
        z = new float[len];

        int currentPos = 0;
        if (x != null) {
            System.arraycopy(x, 0, z, currentPos, x.length);
            currentPos = x.length;
        }

        if (y != null)
            System.arraycopy(y, 0, z, currentPos, y.length);
    }

    return z;
}

From source file:ArrayUtils.java

/**
 * Creates new array of the same type as given array and adds specified member
 * to the end of new array./*from w  w w.j a  va 2  s.c o  m*/
 *  
 * @param array
 * @param member
 * @return new array with member appened
 */
public static Object[] addToArray(Object[] array, Object member) {
    Object[] newArray = (Object[]) Array.newInstance(array.getClass().getComponentType(), array.length + 1);
    System.arraycopy(array, 0, newArray, 0, array.length);
    newArray[array.length] = member;
    return newArray;
}