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

/**
 * Joins arrays using provided component type.
 *///from w w  w. j a va  2 s.  co m
@SuppressWarnings({ "unchecked" })
public static <T> T[] join(Class<T> componentType, T[][] arrays) {
    if (arrays.length == 1) {
        return arrays[0];
    }
    int length = 0;
    for (T[] array : arrays) {
        length += array.length;
    }
    T[] result = (T[]) Array.newInstance(componentType, length);

    length = 0;
    for (T[] array : arrays) {
        System.arraycopy(array, 0, result, length, array.length);
        length += array.length;
    }
    return result;
}

From source file:Main.java

/**
 * Removes sub-array from <code>String</code> array.
 *///  ww  w .  ja v a2 s  . c  om
public static String[] remove(String[] buffer, int offset, int length) {
    int len2 = buffer.length - length;
    String[] temp = new String[len2];
    System.arraycopy(buffer, 0, temp, 0, offset);
    System.arraycopy(buffer, offset + length, temp, offset, len2 - offset);
    return temp;
}

From source file:Main.java

/**
 * Adds value to given array if not already present, providing set-like
 * behavior./*from www.ja v a2  s . c o  m*/
 */
public static long[] appendLong(long[] cur, long val) {
    if (cur == null) {
        return new long[] { val };
    }
    final int N = cur.length;
    for (int i = 0; i < N; i++) {
        if (cur[i] == val) {
            return cur;
        }
    }
    long[] ret = new long[N + 1];
    System.arraycopy(cur, 0, ret, 0, N);
    ret[N] = val;
    return ret;
}

From source file:Main.java

public static <T> T[] combineArrays(Class<T> clazz, T[]... arrays) {
    int length = 0;

    for (T[] array : arrays) {
        if (array == null)
            continue;
        length += array.length;//from w  ww.  j a  v  a2  s .  c o  m
    }

    T[] finalArray = (T[]) Array.newInstance(clazz, length);

    int i = 0;
    for (T[] array : arrays) {
        if (array == null)
            continue;
        System.arraycopy(array, 0, finalArray, i, array.length);
        i += array.length;
    }

    return finalArray;
}

From source file:Main.java

public static byte[] concatBytes(byte[]... byteArrays) {
    int totalLength = 0;
    for (byte[] byteArray : byteArrays) {
        if (byteArray != null) {
            totalLength += byteArray.length;
        }//w  w  w.  j av a2s . c  o m
    }

    final byte[] concatBuffer = new byte[totalLength];
    int copyCounter = 0;
    for (byte[] byteArray : byteArrays) {
        if (byteArray != null) {
            System.arraycopy(byteArray, 0, concatBuffer, copyCounter, byteArray.length);
            copyCounter += byteArray.length;
        }
    }
    return concatBuffer;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static final <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;/* ww  w. jav  a2 s.c  o  m*/
}

From source file:Main.java

public static ByteBuffer copyBinary(final ByteBuffer orig) {
    if (orig == null) {
        return null;
    }/*from  ww w .j  a  v a 2  s .co m*/
    ByteBuffer copy = ByteBuffer.wrap(new byte[orig.remaining()]);
    if (orig.hasArray()) {
        System.arraycopy(orig.array(), orig.arrayOffset() + orig.position(), copy.array(), 0, orig.remaining());
    } else {
        orig.slice().get(copy.array());
    }

    return copy;
}

From source file:Main.java

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

From source file:Main.java

/**
 * Copies the specified array, truncating or padding with nulls (if necessary)
 * so the copy has the specified length. For all indices that are valid in
 * both the original array and the copy, the two arrays will contain identical
 * values. For any indices that are valid in the copy but not the original,
 * the copy will contain null. Such indices will exist if and only if the
 * specified length is greater than that of the original array. The
 * resulting array is of exactly the same class as the original array.
 *
 * @param original the array to be copied
 * @param newLength the length of the copy to be returned
 * @return a copy of the original array, truncated or padded with nulls to
 * obtain the specified length//  w ww  .j ava  2 s .  c  om
 */
public static <T> T[] copyOf(T[] original, int newLength) {
    @SuppressWarnings({ "unchecked" })
    T[] copy = (T[]) Array.newInstance(original.getClass().getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;
}

From source file:Main.java

public static byte[] unpadLeft(byte[] tgt, byte padding) {
    int offset = 0;
    for (; offset < tgt.length; offset++) {
        if (tgt[offset] != padding) {
            break;
        }//from  w w  w.j a  v a  2s  .  c o m
    }
    if (offset < tgt.length) {
        byte[] rslt = new byte[tgt.length - offset];
        System.arraycopy(tgt, offset, rslt, 0, rslt.length);
        return rslt;
    }
    return new byte[0];
}