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

/**
 * Concatenates two byte arrays.//ww w .j  a v a  2  s . co m
 * 
 * @param a
 *            the first array.
 * @param b
 *            the second array.
 * @return the concatenated array.
 */
public static byte[] concatenate(byte[] a, byte[] b) {
    int lengthA = a.length;
    int lengthB = b.length;

    byte[] concat = new byte[lengthA + lengthB];

    System.arraycopy(a, 0, concat, 0, lengthA);
    System.arraycopy(b, 0, concat, lengthA, lengthB);

    return concat;
}

From source file:Main.java

public static byte[] concat(byte[] array, byte single) {
    byte[] result = new byte[1 + array.length];
    System.arraycopy(array, 0, result, 0, array.length);
    result[array.length] = single;/*from   w ww.j  av  a2 s. c o m*/
    return result;
}

From source file:Main.java

public static void top_k_competition(float[][][] response_array, int k) {
    int depth = response_array.length;
    int height = response_array[0].length;
    int width = response_array[0][0].length;
    int find_min_flag = 0;

    float[] copy_of_array = new float[depth * height * width];
    for (int depth_index = 0; depth_index < depth; depth_index++) {
        for (int height_index = 0; height_index < height; height_index++) {
            System.arraycopy(response_array[depth_index][height_index], 0, copy_of_array,
                    (depth_index * height + height_index) * width, width);
        }//w w  w.  j a  v  a2 s  .  c  o m
    }

    Arrays.sort(copy_of_array);

    for (int depth_index = 0; depth_index < depth; depth_index++) {
        for (int height_index = 0; height_index < height; height_index++) {
            for (int width_index = 0; width_index < width; width_index++) {
                if (response_array[depth_index][height_index][width_index] < copy_of_array[depth * height
                        * width - k]) {
                    response_array[depth_index][height_index][width_index] = 0;
                } else if ((response_array[depth_index][height_index][width_index] == copy_of_array[depth
                        * height * width - k]) && find_min_flag == 1) {
                    response_array[depth_index][height_index][width_index] = 0;
                } else if ((response_array[depth_index][height_index][width_index] == copy_of_array[depth
                        * height * width - k]) && find_min_flag == 0) {
                    response_array[depth_index][height_index][width_index] = 1;
                    find_min_flag = 1;
                } else {
                    response_array[depth_index][height_index][width_index] = 1;
                }
            }
        }
    }
}

From source file:Util.java

/*********************************************************************
* Prepends an Object to an Object array.
*
* <p>/*from  w ww.java2s . com*/
* Example:
* <code>
* <pre>
* String [ ]  stringArray
*   = ( String [ ] ) ArrayLib.prepend ( new String [ ] { }, "" );
* </pre>
* </code>
* </p>
*
* @throws NullArgumentException
*
*   If either argument is null.
*
* @return
*
*   Returns a new array with the same component type as the old array.
*********************************************************************/
public static Object[] prepend(Object[] oldArray, Object o)
//////////////////////////////////////////////////////////////////////
{

    Object[] newArray = (Object[]) Array.newInstance(oldArray.getClass().getComponentType(),
            oldArray.length + 1);

    System.arraycopy(oldArray, 0, newArray, 1, oldArray.length);

    newArray[0] = o;

    return newArray;
}

From source file:Main.java

/**
 * A method for gluing byte lists./*from w w w .  j  av a2  s . c o  m*/
 *
 * @param first
 *            the first array.
 * @param rest
 *            the rest of the arrays.
 * @return
 */
public static byte[] arrCat(byte[] first, byte[]... rest) {
    int ttlLen = first.length;
    for (byte[] arr : rest) {
        ttlLen += arr.length;
    }
    byte[] result = Arrays.copyOf(first, ttlLen);
    int currOfst = first.length;
    for (byte[] arr : rest) {
        System.arraycopy(arr, 0, result, currOfst, arr.length);
        currOfst += arr.length;
    }

    return result;
}

From source file:Main.java

public static Object arrayExpandAddElements(Object array, Object[] elementsToAdd) {
    Class cl = array.getClass();//from  w ww .  ja  v a  2 s  .c o m
    if (!cl.isArray())
        return null;
    int length = Array.getLength(array);
    int newLength = length + elementsToAdd.length;
    Class componentType = array.getClass().getComponentType();
    Object newArray = Array.newInstance(componentType, newLength);
    System.arraycopy(array, 0, newArray, 0, length);
    for (int i = 0; i < elementsToAdd.length; i++) {
        Array.set(newArray, length + i, elementsToAdd[i]);
    }
    return newArray;
}

From source file:Main.java

public static String hexDump(byte[] bytes, int offset, int length) {
    StringBuilder sb = new StringBuilder();
    sb.append(String.format("HEXDUMP %d bytes\n", length));
    for (int i = 0; i < length; i += 16) {
        int rowSize = length - i;
        if (rowSize > 16) {
            rowSize = 16;//www  .j  a v  a  2s  .com
        }
        byte[] row = new byte[rowSize];
        System.arraycopy(bytes, offset + i, row, 0, rowSize);
        hexDumpRow(sb, row, i);
    }
    return sb.toString();
}

From source file:Main.java

public static String byteArrayToHexString(byte[] bytes) {
    int oldLength = bytes.length;
    byte[] positiveBytes = new byte[oldLength + 1];
    System.arraycopy(bytes, 0, positiveBytes, 1, oldLength);
    positiveBytes[0] = 1;/*from   w ww.  jav a  2s .c o  m*/
    BigInteger bigInt = new BigInteger(positiveBytes);
    String hex = bigInt.toString(16);
    return hex.substring(1);
}

From source file:Main.java

/**
 * Returns a new byte array: c = a + b.//from  w  w w .  j  av  a 2 s .c  o  m
 * 
 * The array a will be towards the 0 index, the array b right after.
 * 
 * c.length = a.length + b.length
 * 
 * @param a the first array
 * @param b the second array
 * @return the final array
 */
public static byte[] appendByteArrays(byte[] a, byte[] b) {
    byte[] c = new byte[a.length + b.length];

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

    return c;
}

From source file:Main.java

public static void sensorRotationVector2Matrix(SensorEvent event, int rotation, float[] output) {
    if (!sIsTruncated) {
        try {//from   w  ww  .  j  a  v  a 2 s .  com
            SensorManager.getRotationMatrixFromVector(sUIThreadTmp, event.values);
        } catch (Exception e) {
            // On some Samsung devices, SensorManager#getRotationMatrixFromVector throws an exception
            // if the rotation vector has more than 4 elements. Since only the four first elements are used,
            // we can truncate the vector without losing precision.
            Log.e(TAG, "maybe Samsung bug, will truncate vector");
            sIsTruncated = true;
        }
    }

    if (sIsTruncated) {
        System.arraycopy(event.values, 0, sTruncatedVector, 0, 4);
        SensorManager.getRotationMatrixFromVector(sUIThreadTmp, sTruncatedVector);
    }

    float[] values = event.values;
    switch (rotation) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_180: /* Notice: not supported for ROTATION_180! */
        SensorManager.getRotationMatrixFromVector(output, values);
        break;
    case Surface.ROTATION_90:
        SensorManager.getRotationMatrixFromVector(sUIThreadTmp, values);
        SensorManager.remapCoordinateSystem(sUIThreadTmp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X,
                output);
        break;
    case Surface.ROTATION_270:
        SensorManager.getRotationMatrixFromVector(sUIThreadTmp, values);
        SensorManager.remapCoordinateSystem(sUIThreadTmp, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X,
                output);
        break;
    }
    Matrix.rotateM(output, 0, 90.0F, 1.0F, 0.0F, 0.0F);
}