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

public static int[] copyOfRange(int[] original, int start, int end) {
    if (start <= end) {
        if (original.length >= start && 0 <= start) {
            int length = end - start;
            int copyLength = Math.min(length, original.length - start);
            int[] copy = new int[length];
            System.arraycopy(original, start, copy, 0, copyLength);
            return copy;
        }/*  w ww.  j  av a2 s  .c o  m*/
        throw new ArrayIndexOutOfBoundsException();
    }
    throw new IllegalArgumentException();
}

From source file:Main.java

public static byte[] combBytes(byte[]... bytes) {
    int length = 0;
    for (int i = 0; i < bytes.length; i++) {
        length = length + (bytes[i] != null ? bytes[i].length : 0);
    }//from ww  w  .  jav  a 2 s. c o m
    byte[] combBytes = new byte[length];
    int position = 0;
    for (int i = 0; i < bytes.length; i++) {
        System.arraycopy(bytes[i], 0, combBytes, position, bytes[i].length);
        position = position + bytes[i].length;
    }
    return combBytes;
}

From source file:Main.java

public static byte[] arraycopy(byte[] from, byte[] to) {
    System.arraycopy(from, 0, to, 0, from.length);
    return to;/*from  w  w  w  . ja  v  a  2 s  .c  o m*/
}

From source file:Main.java

@SuppressWarnings("unchecked")
@SafeVarargs// www  .j a va  2 s  .  co m
public static <T> T[] appendToArrayEnd(T[] array, T element, T... elements) {

    Class<?> componentType = array.getClass().getComponentType();

    Object newArray = Array.newInstance(componentType, array.length + 1 + elements.length);

    System.arraycopy(array, 0, newArray, 0, array.length);
    Array.set(newArray, array.length, element);
    System.arraycopy(elements, 0, newArray, array.length + 1, elements.length);

    return (T[]) newArray;
}

From source file:Main.java

/***********************************************************************/
public static String locateFile(String fileLocation, String backupPaths[]) throws Exception {
    String[] newArray = new String[backupPaths.length + 1];
    System.arraycopy(backupPaths, 0, newArray, 1, backupPaths.length);
    newArray[0] = ".";
    backupPaths = newArray;//w  w  w .j  av a  2  s .c om
    for (int i = 0; i < backupPaths.length; i++) {
        String tfileLocation = backupPaths[i] + File.separator + fileLocation;
        File file = new File(tfileLocation);
        if (file.exists()) {
            return file.getAbsolutePath();
        }
    }
    throw new Error(String.format("Couldn't find '%s' from locations %s with current directory '%s'",
            fileLocation, Arrays.asList(backupPaths), new File(".").getAbsolutePath()));
}

From source file:Main.java

/**
 * Trims an array to a given size./*from   w ww . j  a v a2 s.  c  o  m*/
 *
 * @param f Array to be trimmed.
 * @return Trimmed array 
 */

static public Object trimArray(Object[] list, int newSize) {
    Class type = list.getClass().getComponentType();
    Object temp = Array.newInstance(type, newSize);
    System.arraycopy(list, 0, temp, 0, newSize);
    return temp;
}

From source file:Main.java

public static <T> T[] concatAll(T[] first, T[]... rest) {
    int totalLength = first.length;
    for (T[] array : rest) {
        totalLength += array.length;//from w  ww. j a  va2s .c  o  m
    }
    T[] result = Arrays.copyOf(first, totalLength);
    int offset = first.length;
    for (T[] array : rest) {
        System.arraycopy(array, 0, result, offset, array.length);
        offset += array.length;
    }
    return result;
}

From source file:Main.java

public static byte[] removeTailF(byte[] buffer) {
    int length = buffer.length;
    for (; length > 0; length--) {
        if (buffer[length - 1] != 'F')
            break;
    }/*from   w ww  .  j  a v  a  2 s  .c o m*/
    if (length == buffer.length) {
        return buffer;
    } else {
        byte[] destBuffer = new byte[length];
        System.arraycopy(buffer, 0, destBuffer, 0, length);
        return destBuffer;
    }
}

From source file:Main.java

/**
 * copy byte array from array.//from w  ww.  j  a  v a2  s.c  o  m
 * @param from - the orginal array
 * @param stratidx - the start index
 * @param ln - the length of the tagert array.
 * @return - the slice copied fron the original array
 */
public static byte[] extractBytesArray(byte[] from, int stratidx, int ln) {
    byte[] toReturn = new byte[ln];
    System.arraycopy(from, stratidx, toReturn, 0, toReturn.length);
    return toReturn;
}

From source file:Main.java

public static byte[] YV12toYUV420Planar(byte[] input, byte[] output, int width, int height) {
    /*//from ww  w . ja v a2 s. c om
     * COLOR_FormatYUV420Planar is I420 which is like YV12, but with U and V reversed.
     * So we just have to reverse U and V.
     */
    final int frameSize = width * height;
    final int qFrameSize = frameSize / 4;

    System.arraycopy(input, 0, output, 0, frameSize); // Y
    System.arraycopy(input, frameSize + qFrameSize, output, frameSize, qFrameSize); // Cb (U)
    System.arraycopy(input, frameSize, output, frameSize + qFrameSize, qFrameSize); // Cr (V)

    return output;
}