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

/**
 * Concatenates a list of double arrays into a single array.
 * //  ww  w.  ja v  a  2 s. c o m
 * @param arrays The arrays.
 * @return The concatenated array.
 * 
 * @see {@link http://stackoverflow.com/questions/80476/how-to-concatenate-two-arrays-in-java}
 */
public static double[] concatAllDouble(double[]... arrays) {
    int totalLength = 0;
    final int subArrayCount = arrays.length;
    for (int i = 0; i < subArrayCount; ++i) {
        totalLength += arrays[i].length;
    }
    double[] result = Arrays.copyOf(arrays[0], totalLength);
    int offset = arrays[0].length;
    for (int i = 1; i < subArrayCount; ++i) {
        System.arraycopy(arrays[i], 0, result, offset, arrays[i].length);
        offset += arrays[i].length;
    }
    return result;
}

From source file:Main.java

/**
 * Concatenates smaller fragments into entire buffers.
 * @param fragments An array of byte buffers (<code>byte[]</code>)
 * @return A byte buffer//from  w w  w.j  a v  a 2  s  .  c o m
 */
public static byte[] defragmentBuffer(byte[] fragments[]) {
    int total_length = 0;
    byte[] ret;
    int index = 0;

    if (fragments == null)
        return null;
    for (int i = 0; i < fragments.length; i++) {
        if (fragments[i] == null)
            continue;
        total_length += fragments[i].length;
    }
    ret = new byte[total_length];
    for (int i = 0; i < fragments.length; i++) {
        if (fragments[i] == null)
            continue;
        System.arraycopy(fragments[i], 0, ret, index, fragments[i].length);
        index += fragments[i].length;
    }
    return ret;
}

From source file:Main.java

public static byte[] copyOfRange(byte[] bytes, int offset, int len) {
    byte[] result = new byte[len];
    System.arraycopy(bytes, offset, result, 0, len);
    return result;
}

From source file:Main.java

public static String[] addAll(String[] array1, String[] array2) {
    if (array1 == null) {
        return clone(array2);
    } else if (array2 == null) {
        return clone(array1);
    }//from w ww  .j  a  v a 2 s. c o  m
    String[] joinedArray = (String[]) Array.newInstance(array1.getClass().getComponentType(),
            array1.length + array2.length);
    System.arraycopy(array1, 0, joinedArray, 0, array1.length);
    System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
    return joinedArray;
}

From source file:Main.java

/**
 * //from w w w.  j av  a 2s . c om
 * @param objs
 * @return
 */
private static String[] convertToStringArray(Object[] objs) {
    if (objs == null) {
        return null;
    }
    String[] result = new String[objs.length];
    System.arraycopy(objs, 0, result, 0, objs.length);
    return result;
}

From source file:Main.java

public static String[] copyOf(String[] source, int newSize) {
    String[] result = new String[newSize];
    newSize = Math.min(source.length, newSize);
    System.arraycopy(source, 0, result, 0, newSize);
    return result;
}

From source file:Main.java

public static byte[] concact(byte[] arrayOne, byte[] arrayTwo) {
    int combinedLength = arrayOne.length + arrayTwo.length;
    byte[] res = new byte[combinedLength];
    System.arraycopy(arrayOne, 0, res, 0, arrayOne.length);
    System.arraycopy(arrayTwo, 0, res, arrayOne.length, arrayTwo.length);
    return res;//from ww  w .j a  v a 2 s . c  om
}

From source file:Main.java

public static byte[] trim(byte[] input, int length) {
    byte[] result = new byte[length];
    System.arraycopy(input, 0, result, 0, result.length);

    return result;
}

From source file:Main.java

public static int[] addValue(int[] ints, int i) {
    int[] copy = new int[ints.length + 1];
    System.arraycopy(ints, 0, copy, 0, ints.length);
    copy[ints.length] = i;/*from  w  w w  . j a  v a2 s  . co m*/
    return copy;
}

From source file:Main.java

public static byte[] copyFrom(byte[] input) {
    byte[] output = new byte[input.length];
    System.arraycopy(input, 0, output, 0, output.length);

    return output;
}