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[] multiMD5(byte[] data) {
    byte[] res = DigestUtils.md5(data);
    int headLength = res.length / 2;
    byte[] head = new byte[headLength];

    if (res.length > headLength) {
        byte[] tail = new byte[res.length - headLength];

        System.arraycopy(res, 0, head, 0, headLength);
        System.arraycopy(res, headLength, tail, 0, tail.length);
        head = DigestUtils.md5(head);// ww  w.ja v a2 s.  c om
        tail = DigestUtils.md5(tail);
        res = new byte[head.length + tail.length];
        System.arraycopy(head, 0, res, 0, head.length);
        System.arraycopy(tail, 0, res, head.length, tail.length);
    }

    res = DigestUtils.md5(res);

    return res;
}

From source file:Main.java

public static byte[] multiSha(byte[] data) {
    byte[] res = DigestUtils.sha1(data);
    int headLength = res.length / 2;
    byte[] head = new byte[headLength];

    if (res.length > headLength) {
        byte[] tail = new byte[res.length - headLength];

        System.arraycopy(res, 0, head, 0, headLength);
        System.arraycopy(res, headLength, tail, 0, tail.length);
        head = DigestUtils.sha1(head);/* ww w .  j av  a  2  s  .  c om*/
        tail = DigestUtils.sha1(tail);
        res = new byte[head.length + tail.length];
        System.arraycopy(head, 0, res, 0, head.length);
        System.arraycopy(tail, 0, res, head.length, tail.length);
    }

    res = DigestUtils.sha1(res);

    return res;
}

From source file:Main.java

public static String[] toStringA(byte[] data) {
    // Advanced Technique: Extract the String array's length
    // from the first four bytes in the char array, and then
    // read the int array denoting the String lengths, and
    // then read the Strings.
    // ----------
    if (data == null || data.length < 4)
        return null;
    // ----------
    byte[] bBuff = new byte[4]; // Buffer
    // -----//from  ww w. ja  v  a  2s. co  m
    System.arraycopy(data, 0, bBuff, 0, 4);
    int saLen = toInt(bBuff);
    if (data.length < (4 + (saLen * 4)))
        return null;
    // -----
    bBuff = new byte[saLen * 4];
    System.arraycopy(data, 4, bBuff, 0, bBuff.length);
    int[] sLens = toIntA(bBuff);
    if (sLens == null)
        return null;
    // ----------
    String[] strs = new String[saLen];
    for (int i = 0, dataPos = 4 + (saLen * 4); i < saLen; i++) {
        if (sLens[i] > 0) {
            if (data.length >= (dataPos + sLens[i])) {
                bBuff = new byte[sLens[i]];
                System.arraycopy(data, dataPos, bBuff, 0, sLens[i]);
                dataPos += sLens[i];
                strs[i] = toString(bBuff);
            } else
                return null;
        }
    }
    // ----------
    return strs;
}

From source file:Main.java

/**
 * Join two arrays./*from  www.j  a  va 2  s  .c  o m*/
 */
public static byte[] join(byte[] a, byte[] b) {
    byte[] bytes = new byte[a.length + b.length];

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

    return bytes;
}

From source file:Main.java

/**
 * Generates a subarray of a given BigInteger array.
 *
 * @param input -/*  ww w.  j a va2  s .c o  m*/
 *              the input BigInteger array
 * @param start -
 *              the start index
 * @param end   -
 *              the end index
 * @return a subarray of <tt>input</tt>, ranging from <tt>start</tt> to
 *         <tt>end</tt>
 */
public static BigInteger[] subArray(BigInteger[] input, int start, int end) {
    BigInteger[] result = new BigInteger[end - start];
    System.arraycopy(input, start, result, 0, end - start);
    return result;
}

From source file:ArrayUtil.java

/**
 * Doubles the size of an array/*from  w  w  w. j a v a  2s.  c  o m*/
 * 
 * @param in
 * @return The new array
 */
public static long[] grow(long[] in) {
    long[] na = new long[in.length * 2];
    System.arraycopy(in, 0, na, 0, in.length);
    return na;
}

From source file:Main.java

public static void insertInOrder(int n) {
    int pos = Arrays.binarySearch(array, 0, elements, n);
    if (pos < 0)
        pos = ~pos;/*from www  .j ava  2 s  .  com*/
    if (pos < elements)
        System.arraycopy(array, pos, array, pos + 1, elements - pos);
    array[pos] = n;
    elements++;
}

From source file:Main.java

/**
 * Returns a new array of given size, containing as many elements of
 * the original array as it can hold. N.B. Always returns a new array
 * even if newsize parameter is the same as the old size.
 *///from  w  w  w .j  a  va  2 s  .c o  m
public static Object resizeArray(Object source, int newsize) {

    Object newarray = Array.newInstance(source.getClass().getComponentType(), newsize);
    int oldsize = Array.getLength(source);

    if (oldsize < newsize) {
        newsize = oldsize;
    }

    System.arraycopy(source, 0, newarray, 0, newsize);

    return newarray;
}

From source file:Main.java

public static int[] slice(int[] array, int start, int len) {
    int[] r = new int[len];
    System.arraycopy(array, start, r, 0, len);
    return r;/*w  ww .j  a v  a  2 s  .  com*/
}

From source file:Main.java

public static String setPoint(String in) {
    char[] res;// w  w  w.  j a v a2  s  .c  o  m
    char[] cur = in.toCharArray();
    boolean isNeg = in.contains("-");
    int lengthNum;
    if (isNeg)
        lengthNum = in.length() - 1;
    else
        lengthNum = in.length();
    int length = in.length() - NUM_AFTER_COM;
    if (lengthNum > NUM_AFTER_COM) {
        res = new char[in.length() + 1];
        res[length] = '.';
        System.arraycopy(cur, 0, res, 0, length);
        System.arraycopy(cur, length, res, length + 1, NUM_AFTER_COM);
    } else {
        int size;
        if (isNeg) {
            length = in.length() - 1;
            size = NUM_AFTER_COM + 3;
            res = new char[size];
            res[0] = '-';
            res[1] = '0';
            res[2] = '.';
            System.arraycopy(cur, 1, res, size - length, length);
        } else {
            length = in.length();
            size = NUM_AFTER_COM + 2;
            res = new char[size];
            res[0] = '0';
            res[1] = '.';
            System.arraycopy(cur, 0, res, size - length, length);
        }
        for (int i = 0; i < res.length; i++) {
            if (res[i] == '\u0000')
                res[i] = '0';
        }
    }
    return new String(res);
}