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

/**
 * new byte[] { 8, 4, 2, 1 }, 0, 4, 2 => new byte[][]{{8,4}, {2,1}}
 *
 * @param src//w  w  w. j  av  a 2s .  c om
 * @param start
 * @param length
 * @param subArrayLength
 * @return
 */

public static byte[][] getCopyByteArrayArray(byte[] src, int start, int length, int subArrayLength) {
    byte[][] dest = new byte[length / subArrayLength][subArrayLength];
    byte[] temp = new byte[length];
    System.arraycopy(src, start, temp, 0, length);
    for (int i = 0; i < dest.length; i++) {
        System.arraycopy(temp, i * subArrayLength, dest[i], 0, subArrayLength);
    }

    return dest;
}

From source file:Main.java

public static String[] changeArray(String[] src, int fromIndex, int toIndex) {
    String[] dest = new String[src.length - (toIndex - fromIndex + 1)];
    System.arraycopy(src, 0, dest, 0, fromIndex);
    System.arraycopy(src, toIndex + 1, dest, fromIndex, dest.length - fromIndex);
    return dest;//from   w  ww . j  av a  2s . co  m
}

From source file:Main.java

public static <T> T[] concat(T[] a, T[] b) {
    final int alen = a.length;
    final int blen = b.length;
    @SuppressWarnings("unchecked")
    final T[] result = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), alen + blen);
    System.arraycopy(a, 0, result, 0, alen);
    System.arraycopy(b, 0, result, alen, blen);
    return result;
}

From source file:Main.java

/**
 * Transforms a number String to a byte array
 * @param in number String//w ww  .j  a  va2  s.  c om
 * @return
 */
public static byte[] NumStringToBytes(String in) {
    BigInteger num = new BigInteger(in);
    byte[] bytes = num.toByteArray();
    if (bytes.length > 0) {
        if (bytes[0] == 0) {
            byte[] cuttedByte = new byte[bytes.length - 1];
            System.arraycopy(bytes, 1, cuttedByte, 0, bytes.length - 1);
            return cuttedByte;
        }

    }
    return num.toByteArray();
}

From source file:Main.java

public static byte[] readFileContents(InputStream input) throws IOException {
    byte contents[] = new byte[10000], buf[] = new byte[1024];
    InputStream in = new BufferedInputStream(input);
    int bytes_read = 0;

    for (;;) {/*ww w.j  av a 2 s. com*/
        int tmp = in.read(buf, 0, buf.length);
        if (tmp == -1)
            break;
        System.arraycopy(buf, 0, contents, bytes_read, tmp);
        bytes_read += tmp;
    }

    byte[] retval = new byte[bytes_read];
    System.arraycopy(contents, 0, retval, 0, bytes_read);
    return retval;
}

From source file:Main.java

/**
 * Produces a copy of the given array;//from   ww w .j  a  v a2 s  .  c  o  m
 * @param array the array to copy.
 * @param <T> Any type
 * @return the copy of the given array;
 */
public static <T> T[] duplicateArray(T[] array) {
    @SuppressWarnings("unchecked")
    T[] copy = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length);
    System.arraycopy(array, 0, copy, 0, array.length);
    return copy;
}

From source file:Main.java

public static <T> T[] resize(T[] x, int size) {
    T[] out = null;/*from   www. j a  va  2s .  co m*/
    try {
        out = (T[]) Array.newInstance(x.getClass().getComponentType(), size);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    }
    int n = Math.min(x.length, size);
    System.arraycopy(x, 0, out, 0, n);
    return out;
}

From source file:Main.java

/**
 * Generate a subarray of a given byte array.
 *
 * @param input the input byte array//from  w  ww. j  ava 2s.com
 * @param start the start index
 * @param end   the end index
 * @return a subarray of <tt>input</tt>, ranging from <tt>start</tt>
 *         (inclusively) to <tt>end</tt> (exclusively)
 */
public static byte[] subArray(byte[] input, int start, int end) {
    byte[] result = new byte[end - start];
    System.arraycopy(input, start, result, 0, end - start);
    return result;
}

From source file:Main.java

private static byte[] expand(byte[] bytes, int skip) {
    byte[] newBytes = new byte[bytes.length - skip];
    Inflater inflater = new Inflater();

    inflater.setInput(bytes, skip, newBytes.length);
    try {/*from  w  w w.ja v  a2 s  . c  o m*/
        int outCount = inflater.inflate(newBytes);
        System.arraycopy(newBytes, 0, bytes, skip, outCount);
        Arrays.fill(bytes, skip + outCount, bytes.length, (byte) 0);
        return bytes;
    } catch (DataFormatException e) {
    }

    return null;
}

From source file:Main.java

public static <T> T[] concat(T[] first, T[] second) {
    if (first == null && second == null) {
        return null;
    } else if (first == null) {
        return Arrays.copyOf(second, second.length);
    } else if (second == null) {
        return Arrays.copyOf(first, first.length);
    } else {/*  w ww  . j a  va2  s.com*/
        T[] result = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }
}