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

static Object doubleArray(Object original) {
    Object returnValue = null;/*from w  ww  . j  ava2 s  . c  om*/
    Class type = original.getClass();
    if (type.isArray()) {
        int length = Array.getLength(original);
        Class elementType = type.getComponentType();
        returnValue = Array.newInstance(elementType, length * 2);
        System.arraycopy(original, 0, returnValue, 0, length);
    }
    return returnValue;
}

From source file:Main.java

static public long[] resizeArray(long[] f, int newSize) {
    long[] newf = new long[newSize];
    System.arraycopy(f, 0, newf, 0, Math.min(f.length, newSize));
    return newf;//  w  w  w. ja  v a2s .  c  o m
}

From source file:Main.java

public static void setSSRC(byte[] buffer, byte[] ssrc) {
    System.arraycopy(ssrc, 0, buffer, 8, 4);
}

From source file:Main.java

/**
 * Puts the entire <tt>source</tt> array in the <tt>target</tt> array at offset <tt>offset</tt>.
 *//*w  w  w.  j ava2s. com*/
public static void put(byte[] source, byte[] target, int offset) {
    System.arraycopy(source, 0, target, offset, source.length);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] withoutFirst(T[] o) {
    final Class<?> memberClass = o.getClass().getComponentType();
    final T[] result = (T[]) Array.newInstance(memberClass, o.length - 1);
    System.arraycopy(o, 1, result, 0, result.length);
    return result;
}

From source file:Main.java

public static String[] resizeArray(String[] array, int newsize) {
    if (null == array) {
        return null;
    }//from  w  w w  .  j a v a  2  s .c  om
    String[] newArr = new String[newsize];
    if (newsize > array.length) {
        System.arraycopy(array, 0, newArr, 0, array.length);
    } else {
        System.arraycopy(array, 0, newArr, 0, newsize);
    }

    return newArr;
}

From source file:Main.java

/**
 * Retrieves the value from the byte array for the tag value specified. The
 * array should be of the form Tag - Length - Value triplet.
 * @param tag the tag to retrieve from the byte array
 * @param triplet the byte sequence containing the tag length value form
 * @return the value of the specified tag
 *//*from  w  ww  . ja v a 2 s  . c om*/
public static byte[] getTagValue(byte tag, byte[] triplet) {

    int index = findTag(tag, triplet);
    if (index == -1) {
        return null;
    }

    index++;
    int length = triplet[index] & 0xFF;

    byte[] result = new byte[length];
    index++;
    System.arraycopy(triplet, index, result, 0, length);

    return result;
}

From source file:Main.java

/**
 * Combine messages in an array and put a size header before each message
 * @param array/*from  w w w .j av a 2 s. c  o  m*/
 * @return
 */
public static byte[] compileMessages(ArrayList<byte[]> array) {

    int bufferSize = 0;
    for (int i = 0; i < array.size(); i++) {
        byte[] thisByte = array.get(i);
        bufferSize += (4 + thisByte.length);
    }

    byte[] buffer = new byte[bufferSize];

    int pointer = 0; // used to index the next empty byte to fill
    for (int i = 0; i < array.size(); i++) {
        int thisSize = array.get(i).length;
        System.arraycopy(ByteBuffer.allocate(4).putInt(thisSize).array(), 0, buffer, pointer, 4);
        System.arraycopy(array.get(i), 0, buffer, pointer + 4, thisSize);
        pointer += (4 + thisSize);
    }

    return buffer;
}

From source file:Main.java

public static byte[] toByta(short[] 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;/*from  w w w  .  ja va  2s . c  o m*/
}

From source file:Main.java

public static int[] copyOfRange(int[] data, int from, int to) {
    int newLength = getLength(from, to);

    int[] tmp = new int[newLength];

    if (data.length - from < newLength) {
        System.arraycopy(data, from, tmp, 0, data.length - from);
    } else {/*from   ww  w  . ja  v a  2  s  . c  om*/
        System.arraycopy(data, from, tmp, 0, newLength);
    }

    return tmp;
}