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

/**
 *  http://download.oracle.com/javase/1.5.0/docs/guide/security/CryptoSpec.html
 *  Signature Format   ASN.1 sequence of two INTEGER values: r and s, in that order:
 *                                SEQUENCE ::= { r INTEGER, s INTEGER }
 *
 *  http://en.wikipedia.org/wiki/Abstract_Syntax_Notation_One
 *  30 -- tag indicating SEQUENCE//from w w w. j  a  v  a 2 s  .co m
 *  xx - length in octets
 *
 *  02 -- tag indicating INTEGER
 *  xx - length in octets
 *  xxxxxx - value
 *
 *  Convert to BigInteger and back so we have the minimum length representation, as required.
 *  r and s are always non-negative.
 *
 *  Only supports sigs up to about 252 bytes. See code to fix BER encoding for this before you
 *  add a SigType with bigger signatures.
 *
 *  @throws IllegalArgumentException if too big
 *  @since 0.8.7, moved to SigUtil in 0.9.9
 */
private static byte[] sigBytesToASN1(byte[] sig) {
    //System.out.println("pre TO asn1\n" + net.i2p.util.HexDump.dump(sig));
    int len = sig.length;
    int sublen = len / 2;
    byte[] tmp = new byte[sublen];

    System.arraycopy(sig, 0, tmp, 0, sublen);
    BigInteger r = new BigInteger(1, tmp);
    byte[] rb = r.toByteArray();
    if (rb.length > 127)
        throw new IllegalArgumentException("FIXME R length > 127");
    System.arraycopy(sig, sublen, tmp, 0, sublen);
    BigInteger s = new BigInteger(1, tmp);
    byte[] sb = s.toByteArray();
    if (sb.length > 127)
        throw new IllegalArgumentException("FIXME S length > 127");
    int seqlen = rb.length + sb.length + 4;
    if (seqlen > 255)
        throw new IllegalArgumentException("FIXME seq length > 255");
    int totlen = seqlen + 2;
    if (seqlen > 127)
        totlen++;
    byte[] rv = new byte[totlen];
    int idx = 0;

    rv[idx++] = 0x30;
    if (seqlen > 127)
        rv[idx++] = (byte) 0x81;
    rv[idx++] = (byte) seqlen;

    rv[idx++] = 0x02;
    rv[idx++] = (byte) rb.length;
    System.arraycopy(rb, 0, rv, idx, rb.length);
    idx += rb.length;

    rv[idx++] = 0x02;
    rv[idx++] = (byte) sb.length;
    System.arraycopy(sb, 0, rv, idx, sb.length);

    //System.out.println("post TO asn1\n" + net.i2p.util.HexDump.dump(rv));
    return rv;
}

From source file:com.amazonaws.cognito.sync.demo.client.server.AESEncryption.java

public static String unwrap(String cipherText, String key) throws Exception {
    byte[] dataToDecrypt = Base64.decodeBase64(cipherText.getBytes());
    byte[] initializationVector = new byte[16];
    byte[] data = new byte[dataToDecrypt.length - 16];

    System.arraycopy(dataToDecrypt, 0, initializationVector, 0, 16);
    System.arraycopy(dataToDecrypt, 16, data, 0, dataToDecrypt.length - 16);

    byte[] plainText = decrypt(data, key, initializationVector);
    return new String(plainText);
}

From source file:Main.java

/**
 * Truncates the given array to the request length.
 * /*from  w  w  w  .j  a v a  2  s .c om*/
 * @param array
 *            the array to be truncated.
 * @param newLength
 *            the new length in bytes.
 * @return the truncated array.
 */
public static byte[] truncate(byte[] array, int newLength) {
    if (array.length < newLength) {
        return array;
    } else {
        byte[] truncated = new byte[newLength];
        System.arraycopy(array, 0, truncated, 0, newLength);

        return truncated;
    }
}

From source file:ArrayUtil.java

/**
 * Copies the elements of the {@code oldArray} to a new array with extra space to 
 * append the given element {@code toAppend}.
 *//*w ww. j av  a  2 s .  c  o m*/
@SuppressWarnings("unchecked")
public static <T> T[] append(T[] oldArray, T toAppend) {
    Class<?> component = oldArray.getClass().getComponentType();
    T[] array = (T[]) Array.newInstance(component, oldArray.length + 1);
    System.arraycopy(oldArray, 0, array, 0, oldArray.length);
    array[oldArray.length] = toAppend;
    return array;
}

From source file:Main.java

/**
 *  See above.//from  w  w w .ja  v a2  s  . c o m
 *  Only supports sigs up to about 252 bytes. See code to fix BER encoding for bigger than that.
 *
 *  @return len bytes
 *  @since 0.8.7, moved to SigUtil in 0.9.9
 */
private static byte[] aSN1ToSigBytes(byte[] asn, int len) throws SignatureException {
    //System.out.println("pre from asn1 len=" + len + "\n" + net.i2p.util.HexDump.dump(asn));
    if (asn[0] != 0x30)
        throw new SignatureException("asn[0] = " + (asn[0] & 0xff));
    // handles total len > 127
    int idx = 2;
    if ((asn[1] & 0x80) != 0)
        idx += asn[1] & 0x7f;
    if (asn[idx] != 0x02)
        throw new SignatureException("asn[2] = " + (asn[idx] & 0xff));
    byte[] rv = new byte[len];
    int sublen = len / 2;
    int rlen = asn[++idx];
    if ((rlen & 0x80) != 0)
        throw new SignatureException("FIXME R length > 127");
    if ((asn[++idx] & 0x80) != 0)
        throw new SignatureException("R is negative");
    if (rlen > sublen + 1)
        throw new SignatureException("R too big " + rlen);
    if (rlen == sublen + 1)
        System.arraycopy(asn, idx + 1, rv, 0, sublen);
    else
        System.arraycopy(asn, idx, rv, sublen - rlen, rlen);
    idx += rlen;
    int slenloc = idx + 1;
    if (asn[idx] != 0x02)
        throw new SignatureException("asn[s] = " + (asn[idx] & 0xff));
    int slen = asn[slenloc];
    if ((slen & 0x80) != 0)
        throw new SignatureException("FIXME S length > 127");
    if ((asn[slenloc + 1] & 0x80) != 0)
        throw new SignatureException("S is negative");
    if (slen > sublen + 1)
        throw new SignatureException("S too big " + slen);
    if (slen == sublen + 1)
        System.arraycopy(asn, slenloc + 2, rv, sublen, sublen);
    else
        System.arraycopy(asn, slenloc + 1, rv, len - slen, slen);
    //System.out.println("post from asn1\n" + net.i2p.util.HexDump.dump(rv));
    return rv;
}

From source file:Main.java

public static byte[] createCopy(byte[] array, int dataSize, int newLength) {
    byte[] newArray = new byte[newLength];
    if (dataSize > 0) {
        System.arraycopy(array, 0, newArray, 0, dataSize);
    }// w w  w  . j  a  va2s . com
    return newArray;
}

From source file:Main.java

public static byte[] getGrpprl(List<byte[]> sprmList, int size) {
    // spit out the final grpprl
    byte[] grpprl = new byte[size];
    int listSize = sprmList.size() - 1;
    int index = 0;
    for (; listSize >= 0; listSize--) {
        byte[] sprm = sprmList.remove(0);
        System.arraycopy(sprm, 0, grpprl, index, sprm.length);
        index += sprm.length;/*from  w w  w. j  a  va  2  s .  com*/
    }

    return grpprl;

}

From source file:Main.java

@TargetApi(21)
public static AdvertiseData getFatBeaconAdvertisementData(byte[] fatBeaconAdvertisement) {

    // Manually build the advertising info
    int length = Math.min(fatBeaconAdvertisement.length, 17);
    byte[] beaconData = new byte[length + 3];
    System.arraycopy(fatBeaconAdvertisement, 0, beaconData, 3, length);
    beaconData[0] = URL_FRAME_TYPE;
    beaconData[1] = (byte) 0xBA;
    beaconData[2] = FAT_BEACON;/*from ww  w .  j a v  a2 s . co m*/
    return new AdvertiseData.Builder().setIncludeTxPowerLevel(false) // reserve advertising space for URI
            .addServiceData(EDDYSTONE_BEACON_UUID, beaconData)
            // Adding 0xFEAA to the "Service Complete List UUID 16" (0x3) for iOS compatibility
            .addServiceUuid(EDDYSTONE_BEACON_UUID).build();
}

From source file:Main.java

public static Object copyOf(Object src) {
    int srcLength = Array.getLength(src);
    Class<?> srcComponentType = src.getClass().getComponentType();
    Object dest = Array.newInstance(srcComponentType, srcLength);
    if (srcComponentType.isArray()) {
        for (int i = 0; i < Array.getLength(src); i++) {
            Array.set(dest, i, copyOf(Array.get(src, i)));
        }//from  w w w.  java2s . c  o  m
    } else {
        System.arraycopy(src, 0, dest, 0, srcLength);
    }
    return dest;
}

From source file:Main.java

protected static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    T[] copy = ((Object) newType == (Object) Object[].class) ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;/*from   ww  w.ja v  a2 s.  c  o  m*/
}