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

/**
 * (Partial) replacement for Arrays.copyOfRange, which is only available in JDK6.
 *//*from  ww w  .  java 2  s .  co m*/
public static Object[] copyArray(Object[] array, int from, int to) {
    Object[] result = new Object[to - from];
    System.arraycopy(array, from, result, 0, to - from);
    return result;
}

From source file:Main.java

/**
 * Split a byte array <tt>input</tt> into two arrays at <tt>index</tt>,
 * i.e. the first array will have the lower <tt>index</tt> bytes, the
 * second one the higher <tt>input.length - index</tt> bytes.
 *
 * @param input the byte array to be split
 * @param index the index where the byte array is split
 * @return the splitted input array as an array of two byte arrays
 * @throws ArrayIndexOutOfBoundsException if <tt>index</tt> is out of bounds
 *//*w  w  w .  ja v a  2s .c o m*/
public static byte[][] split(byte[] input, int index) throws ArrayIndexOutOfBoundsException {
    if (index > input.length) {
        throw new ArrayIndexOutOfBoundsException();
    }
    byte[][] result = new byte[2][];
    result[0] = new byte[index];
    result[1] = new byte[input.length - index];
    System.arraycopy(input, 0, result[0], 0, index);
    System.arraycopy(input, index, result[1], 0, input.length - index);
    return result;
}

From source file:Main.java

/**
 * Return the value of <tt>big</tt> as a byte array. Although BigInteger
 * has such a method, it uses an extra bit to indicate the sign of the
 * number. For elliptic curve cryptography, the numbers usually are
 * positive. Thus, this helper method returns a byte array of minimal
 * length, ignoring the sign of the number.
 *
 * @param value the <tt>BigInteger</tt> value to be converted to a byte
 *              array//from w w w .j  ava 2 s  . c  o  m
 * @return the value <tt>big</tt> as byte array
 */
public static byte[] toMinimalByteArray(BigInteger value) {
    byte[] valBytes = value.toByteArray();
    if ((valBytes.length == 1) || (value.bitLength() & 0x07) != 0) {
        return valBytes;
    }
    byte[] result = new byte[value.bitLength() >> 3];
    System.arraycopy(valBytes, 1, result, 0, result.length);
    return result;
}

From source file:Main.java

public static String encryptMD5ToString(byte[] data, byte[] salt) {
    if (data == null || salt == null)
        return null;
    byte[] dataSalt = new byte[data.length + salt.length];
    System.arraycopy(data, 0, dataSalt, 0, data.length);
    System.arraycopy(salt, 0, dataSalt, data.length, salt.length);
    return bytes2HexString(encryptMD5(dataSalt));
}

From source file:Main.java

public static byte[] decode(String data) {
    char[] ibuf = new char[4];
    int ibufcnt = 0;
    byte[] obuf = new byte[data.length() / 4 * 3 + 3];
    int obufcnt = 0;
    for (int i = 0; i < data.length(); i++) {
        char ch = data.charAt(i);
        if (ch == BASE64PAD || ch < DECODETABLE.length && DECODETABLE[ch] != Byte.MAX_VALUE) {
            ibuf[ibufcnt++] = ch;/*from  w  ww  .ja v  a 2s  .  c  o  m*/
            if (ibufcnt == ibuf.length) {
                ibufcnt = 0;
                obufcnt += _decode(ibuf, obuf, obufcnt);
            }
        }
    }
    if (obufcnt == obuf.length)
        return obuf;
    byte[] ret = new byte[obufcnt];
    System.arraycopy(obuf, 0, ret, 0, obufcnt);
    return ret;
}

From source file:Main.java

public static byte[] subset(byte[] array, int start, int length) {
    byte[] result = new byte[length];
    System.arraycopy(array, start, result, 0, length);
    return result;
}

From source file:Main.java

public static byte[] concat(byte[] first, byte[]... rest) {
    int totalLength = first.length;
    for (byte[] arr : rest) {
        totalLength += arr.length;/*from w  ww .jav a2  s  .  c  o m*/
    }

    byte[] result = new byte[totalLength];
    System.arraycopy(first, 0, result, 0, first.length);
    int pos = first.length;

    for (byte[] arr : rest) {
        System.arraycopy(arr, 0, result, pos, arr.length);
        pos += arr.length;
    }

    return result;
}

From source file:Main.java

public static <T> T[] concat(T[] A, T[] B) {
    final Class<?> typeofA = A.getClass().getComponentType();
    @SuppressWarnings("unchecked")
    T[] C = (T[]) Array.newInstance(typeofA, A.length + B.length);
    System.arraycopy(A, 0, C, 0, A.length);
    System.arraycopy(B, 0, C, A.length, B.length);

    return C;/*from   www. j av  a 2s  .co m*/
}

From source file:Main.java

public static byte[] copyOfRange(byte[] original, int from, int to) {
    int newLength = to - from;
    if (newLength < 0)
        throw new IllegalArgumentException(from + " > " + to);
    byte[] copy = new byte[newLength];
    System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
    return copy;/* ww w.ja  v  a  2  s.  co m*/
}

From source file:Main.java

public static String getQETAG(String path) {
    byte[] SHA1Byte;
    try {//  w  w w.jav a  2s. c  o  m
        SHA1Byte = getFileSHA1(path);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    if (null == SHA1Byte) {
        throw new IllegalArgumentException("SHA1 must not be empty!");
    }

    if (SHA1Byte.length != 20) {
        throw new IllegalArgumentException("SHA1 length must be 20! Current length:" + SHA1Byte.length);
    }

    byte[] QETAGByte = new byte[21];
    QETAGByte[0] = 0x16;

    System.arraycopy(SHA1Byte, 0, QETAGByte, 1, 20);

    return Base64.encodeToString(QETAGByte, Base64.URL_SAFE | Base64.NO_WRAP);
}