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:Util.java

/*********************************************************************
 * Appends an Object to an Object array.
 *
 * <p>//from  w  ww  . j  av  a2s.c  o  m
 * Example:
 * <code>
 * <pre>
 * String [ ]  stringArray
 *   = ( String [ ] ) ArrayLib.append ( 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[] append(Object[] oldArray, Object o)
//////////////////////////////////////////////////////////////////////
{
    Object[] newArray = (Object[]) Array.newInstance(oldArray.getClass().getComponentType(),
            oldArray.length + 1);

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

    newArray[oldArray.length] = o;

    return newArray;
}

From source file:Main.java

/**
 * Checks whether the byte array has XMP header. The XMP section contains
 * a fixed length header XMP_HEADER./*from  w  w w  . j  a v  a2s .  co m*/
 *
 * @param data Xmp metadata.
 */
private static boolean hasXMPHeader(byte[] data) {
    if (data.length < XMP_HEADER_SIZE) {
        return false;
    }
    try {
        byte[] header = new byte[XMP_HEADER_SIZE];
        System.arraycopy(data, 0, header, 0, XMP_HEADER_SIZE);
        if (new String(header, "UTF-8").equals(XMP_HEADER)) {
            return true;
        }
    } catch (UnsupportedEncodingException e) {
        return false;
    }
    return false;
}

From source file:Main.java

public static byte[] generatePlainClientResponse(String userName, String userPassword) throws Exception {
    byte[] password = userPassword.getBytes("UTF8");
    byte user[] = userName.getBytes("UTF8");
    byte response[] = new byte[password.length + user.length + 2];
    int size = 0;
    response[size++] = SEPARATOR;//w w  w . j  a va 2 s .  c  o  m
    System.arraycopy(user, 0, response, size, user.length);
    size += user.length;
    response[size++] = SEPARATOR;
    System.arraycopy(password, 0, response, size, password.length);
    return response;
}

From source file:Main.java

/**
 * Constructs a NAL unit consisting of the NAL start code followed by the specified data.
 *
 * @param data An array containing the data that should follow the NAL start code.
 * @param offset The start offset into {@code data}.
 * @param length The number of bytes to copy from {@code data}
 * @return The constructed NAL unit./*from  w  w w .  j ava 2 s.  c o  m*/
 */
public static byte[] buildNalUnit(byte[] data, int offset, int length) {
    byte[] nalUnit = new byte[length + NAL_START_CODE.length];
    System.arraycopy(NAL_START_CODE, 0, nalUnit, 0, NAL_START_CODE.length);
    System.arraycopy(data, offset, nalUnit, NAL_START_CODE.length, length);
    return nalUnit;
}

From source file:com.ad.mediasharing.tvmclient.AESEncryption.java

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

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

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

From source file:com.amazonaws.tvm.AESEncryption.java

public static String wrap(String clearText, String key) throws Exception {
    byte[] iv = getIv();

    byte[] cipherText = encrypt(clearText, key, iv);
    byte[] wrapped = new byte[iv.length + cipherText.length];
    System.arraycopy(iv, 0, wrapped, 0, iv.length);
    System.arraycopy(cipherText, 0, wrapped, 16, cipherText.length);

    return new String(Base64.encodeBase64(wrapped));
}

From source file:Main.java

/**
 * Removes the specified element from the given array.
 * @param array the array./*w  w w.  jav a 2 s .  com*/
 * @param size  the original size of the array.
 * @param index the index of the element to be removed.
 */
public static void remove(byte[] array, int size, int index) {
    System.arraycopy(array, index + 1, array, index, array.length - index - 1);

    array[size - 1] = 0;
}

From source file:Main.java

/**
 * Build an array from the provided contents.
 *
 * <p>/*from w  ww . ja v  a 2  s.c  o m*/
 * The resulting array will be the concatenation of <var>arrays</var> input arrays, in their
 * original order.
 * </p>
 *
 * @param arrays the arrays to concatenate
 * @return the newly constructed array
 */
public static String[] buildArray(String[]... arrays) {
    int length = 0;
    for (String[] array : arrays) {
        length += array.length;
    }
    String[] newArray = new String[length];
    int offset = 0;
    for (String[] array : arrays) {
        System.arraycopy(array, 0, newArray, offset, array.length);
        offset += array.length;
    }
    return newArray;
}

From source file:Main.java

public static void mergeArray(final Object dest, @NonNull final Object... arrays) {
    for (int i = 0, j = arrays.length, k = 0; i < j; i++) {
        final Object array = arrays[i];
        if (array == null)
            continue;
        final int length = Array.getLength(array);
        //noinspection SuspiciousSystemArraycopy
        System.arraycopy(array, 0, dest, k, length);
        k += length;/*from   www  . j a v a2s  .  co  m*/
    }
}

From source file:Main.java

public static byte[] concat(byte[] b1, byte[] b2) {
    byte[] ret = new byte[b1.length + b2.length];
    System.arraycopy(b1, 0, ret, 0, b1.length);
    System.arraycopy(b2, 0, ret, b1.length, b2.length);
    return ret;/*from   w w w  .  j  ava 2  s  .  c om*/
}