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

private static byte[] combineSecrets(byte[] encryptionSecret, byte[] macSecret) {
    byte[] combinedSecret = new byte[encryptionSecret.length + macSecret.length];
    System.arraycopy(encryptionSecret, 0, combinedSecret, 0, encryptionSecret.length);
    System.arraycopy(macSecret, 0, combinedSecret, encryptionSecret.length, macSecret.length);

    return combinedSecret;
}

From source file:Main.java

/**
 * Returns subarray.//from w  w w  .  jav a  2 s.  c o m
 */
public static String[] subarray(String[] buffer, int offset, int length) {
    String[] temp = new String[length];
    System.arraycopy(buffer, offset, temp, 0, length);
    return temp;
}

From source file:Main.java

public static int[][] cloneArray(int[][] src) {
    int[][] dst = new int[src.length][];
    for (int i = 0; i < src.length; i++) {
        int[] aMatrix = src[i];
        int aLength = aMatrix.length;
        dst[i] = new int[aLength];
        System.arraycopy(aMatrix, 0, dst[i], 0, aLength);
    }//from ww  w  . j  av a2 s .co  m
    return dst;
}

From source file:Main.java

/**
 * Returns the concatenation of the input arrays in a single array. For example,
 * {@code concat(new byte[] {a, b}, new byte[] {}, new byte[] {c}} returns the array
 * {@code {a, b, c}}./*from  www  .ja va  2 s. com*/
 *
 * @return a single array containing all the values from the source arrays, in order
 */
public static byte[] concat(byte[]... chunks) throws GeneralSecurityException {
    int length = 0;
    for (byte[] chunk : chunks) {
        if (length > Integer.MAX_VALUE - chunk.length) {
            throw new GeneralSecurityException("exceeded size limit");
        }
        length += chunk.length;
    }
    byte[] res = new byte[length];
    int pos = 0;
    for (byte[] chunk : chunks) {
        System.arraycopy(chunk, 0, res, pos, chunk.length);
        pos += chunk.length;
    }
    return res;
}

From source file:Main.java

/**
 *  @param bi non-negative// w w w  .jav a 2s .  c  o  m
 *  @return array of exactly len bytes
 */
public static byte[] rectify(BigInteger bi, int len) throws InvalidKeyException {
    byte[] b = bi.toByteArray();
    if (b.length == len) {
        // just right
        return b;
    }
    if (b.length > len + 1)
        throw new InvalidKeyException("key too big (" + b.length + ") max is " + (len + 1));
    byte[] rv = new byte[len];
    if (b.length == 0)
        return rv;
    if ((b[0] & 0x80) != 0)
        throw new InvalidKeyException("negative");
    if (b.length > len) {
        // leading 0 byte
        if (b[0] != 0)
            throw new InvalidKeyException("key too big (" + b.length + ") max is " + len);
        System.arraycopy(b, 1, rv, 0, len);
    } else {
        // smaller
        System.arraycopy(b, 0, rv, len - b.length, b.length);
    }
    return rv;
}

From source file:Main.java

public static byte[] combine(byte[]... arrays) {
    int totalLength = 0;
    for (byte[] bytes : arrays) {
        totalLength += bytes.length;//from www  .  j a v  a 2 s .  c o  m
    }
    byte[] newArray = new byte[totalLength];
    int pointer = 0;
    for (byte[] bytes : arrays) {
        System.arraycopy(bytes, 0, newArray, pointer, bytes.length);
        pointer += bytes.length;
    }
    return newArray;
}

From source file:Main.java

static byte[] copyOfRange(byte[] from, int start, int end) {
    int length = end - start;
    if (length > 0) {
        byte[] result = new byte[length];
        System.arraycopy(from, start, result, 0, length);
        return result;
    } else/*ww w.  j  ava  2  s  .c  o  m*/
        return new byte[] {};
}

From source file:Main.java

public static String[] copySortArray(String[] values) {
    if (values == null) {
        return null;
    }//from   w  ww  .j  av  a 2  s  . c  o m
    String[] copy = new String[values.length];
    System.arraycopy(values, 0, copy, 0, values.length);
    Arrays.sort(copy);
    return copy;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static final <T> T[] add(T[] array, T obj) {
    if (array == null) {
        throw new IllegalArgumentException("array");
    }//from   w  w w . j av a  2s .c o m
    T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length + 1);
    System.arraycopy(array, 0, newArray, 0, array.length);
    newArray[array.length] = obj;
    return newArray;
}

From source file:Main.java

public static final String readLine(InputStream in, String charset) throws IOException {
    int rd = in.read();
    if (rd == -1)
        return null;
    byte r = (byte) rd;
    int i = 0;//from   w  w  w .j av a 2  s. c  o m
    int l = 50;
    byte[] buf = new byte[l];
    while (r != '\n') {
        if (i >= l - 1) {
            l += 50;
            byte[] old = buf;
            buf = new byte[l];
            System.arraycopy(old, 0, buf, 0, old.length);
        }
        if (r != '\r')
            buf[i++] = r;
        rd = in.read();
        if (rd == -1)
            break;
        r = (byte) rd;
    }
    if (charset == null) {
        return new String(buf, 0, i);
    }
    return new String(buf, 0, i, charset);
}