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

/**
 * Returns subarray.// w  w  w  .  j a  v a2s . c  o m
 */
public static byte[] subarray(byte[] buffer, int offset, int length) {
    byte[] temp = new byte[length];
    System.arraycopy(buffer, offset, temp, 0, length);
    return temp;
}

From source file:Main.java

public static NdefRecord createTextRecord(String payload, boolean encodeInUtf8) {
    byte[] langBytes = Locale.getDefault().getLanguage().getBytes(Charset.forName("US-ASCII"));
    Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
    byte[] textBytes = payload.getBytes(utfEncoding);
    int utfBit = encodeInUtf8 ? 0 : (1 << 7);
    char status = (char) (utfBit + langBytes.length);
    byte[] data = new byte[1 + langBytes.length + textBytes.length];
    data[0] = (byte) status;
    System.arraycopy(langBytes, 0, data, 1, langBytes.length);
    System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
    NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
    return record;
}

From source file:Main.java

static byte[] concatenateBytesArrays(byte[] firstArray, byte[] secondArray) {
    if (firstArray == null && secondArray == null)
        return null;
    else if (firstArray == null)
        return secondArray;
    else if (secondArray == null)
        return firstArray;
    else {/*w w w  .j a  va2s  .  c o m*/
        byte[] both = new byte[firstArray.length + secondArray.length];
        System.arraycopy(firstArray, 0, both, 0, firstArray.length);
        System.arraycopy(secondArray, 0, both, firstArray.length, secondArray.length);
        return both;
    }
}

From source file:Main.java

public static byte[] toByta(char[] data) {
    if (data == null)
        return null;
    // ----------
    byte[] byts = new byte[data.length * 2];
    for (int i = 0; i < data.length; i++)
        System.arraycopy(toByta(data[i]), 0, byts, i * 2, 2);
    return byts;//w ww.  j ava2s  .  c o  m
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] grow(T[] array, int length, T fillElement) {
    Class<T> componentType = (Class<T>) array.getClass().getComponentType();
    T[] newArray = (T[]) Array.newInstance(componentType, length);
    System.arraycopy(array, 0, newArray, 0, array.length);
    Arrays.fill(newArray, array.length, newArray.length, fillElement);
    return newArray;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] remove(T[] values, T value) {
    if (values != null && value != null) {
        for (int i = 0; i < values.length; i++) {
            T v = values[i];//from ww w  .  j  a  v  a  2 s. c o  m
            if (value.equals(v)) {
                T[] newValues = (T[]) Array.newInstance(values.getClass().getComponentType(),
                        values.length - 1);
                if (i > 0) {
                    System.arraycopy(values, 0, newValues, 0, i);
                }
                if (i < values.length - 1) {
                    System.arraycopy(values, i + 1, newValues, i, values.length - 1 - i);
                }
                values = newValues;
                i--;
            }
        }
    }
    return values;
}

From source file:Main.java

/**
 * Extracts the NTLM challenge from the type 2 message as an 8 byte array.
 * //  w ww.j a v a2  s  . c o  m
 * @param msg the type 2 message byte array
 * @return the challenge
 */
public final static byte[] extractChallengeFromType2Message(byte[] msg) {
    byte[] challenge = new byte[8];
    System.arraycopy(msg, 24, challenge, 0, 8);

    return challenge;
}

From source file:Main.java

public static Object arrayExpandAddSingle(Object array, Object elementsToAdd) {
    Class cl = array.getClass();/*from  w  w w  .  ja va  2s . c o  m*/
    if (!cl.isArray())
        return null;
    int length = Array.getLength(array);
    int newLength = length + 1;
    Class componentType = array.getClass().getComponentType();
    Object newArray = Array.newInstance(componentType, newLength);
    System.arraycopy(array, 0, newArray, 0, length);
    Array.set(newArray, length, elementsToAdd);
    return newArray;
}

From source file:Main.java

public static float[] copyOf(float[] original, int newLength) {
    float[] copy = new float[newLength];
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;// w  w w .  j  a  v  a 2s  .com
}

From source file:Main.java

/**
 * Removes value from given array if present, providing set-like behavior.
 *///from   w  ww  . ja v  a2 s .c  om
public static String[] removeString(String[] cur, String val) {
    if (cur == null) {
        return null;
    }
    final int N = cur.length;
    for (int i = 0; i < N; i++) {
        if (Objects.equals(cur[i], val)) {
            String[] ret = new String[N - 1];
            if (i > 0) {
                System.arraycopy(cur, 0, ret, 0, i);
            }
            if (i < (N - 1)) {
                System.arraycopy(cur, i + 1, ret, i, N - i - 1);
            }
            return ret;
        }
    }
    return cur;
}