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 byte[] cutOut(byte[] bytes, int off, int length) {
    byte[] bytess = new byte[length];
    System.arraycopy(bytes, off, bytess, 0, length);
    return bytess;
}

From source file:Main.java

public static byte[] getMid(byte[] array, int start, int length) {
    byte[] data = new byte[length];
    System.arraycopy(array, start, data, 0, length);
    return data;//  w ww. j av  a  2 s.  c o m
}

From source file:Main.java

public static byte[] add(byte[] a, byte[] b) {
    byte[] r = new byte[a.length + b.length];
    System.arraycopy(a, 0, r, 0, a.length);
    System.arraycopy(b, 0, r, a.length, b.length);
    return r;/*from ww w .  j  av  a2  s  .c om*/
}

From source file:Main.java

public static String[] concat(String[] array, String value) {
    String[] result = new String[array.length + 1];
    System.arraycopy(array, 0, result, 0, array.length);
    result[result.length - 1] = value;//from w w  w.  j  av  a2  s . c o  m
    return result;
}

From source file:Main.java

/**
 * Concatenates a list of int arrays into a single array.
 * //from  w ww .  j a  v  a2s  . 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 int[] concatAllInt(int[]... arrays) {
    int totalLength = 0;
    final int subArrayCount = arrays.length;
    for (int i = 0; i < subArrayCount; ++i) {
        totalLength += arrays[i].length;
    }
    int[] 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

public static byte[] copy(byte[] b, int newSize) {
    byte[] r = new byte[newSize];
    System.arraycopy(b, 0, r, 0, Math.min(b.length, r.length));
    return r;/*from  w w  w  .  j  a v a  2 s  . c  o m*/
}

From source file:Main.java

public static byte[] concatenate(byte[] a, byte[] b, byte[] c) {
    if (a != null && b != null && c != null) {
        byte[] rv = new byte[a.length + b.length + c.length];

        System.arraycopy(a, 0, rv, 0, a.length);
        System.arraycopy(b, 0, rv, a.length, b.length);
        System.arraycopy(c, 0, rv, a.length + b.length, c.length);

        return rv;
    } else if (b == null) {
        return concatenate(a, c);
    } else {//from w  ww.j  a  v a 2  s .  c  o  m
        return concatenate(a, b);
    }
}

From source file:com.opengamma.maths.lowlevelapi.functions.utilities.Permute.java

/**
 * Applies permutation p inplace to vector v.
 * @param v the vector to permute/*from  w ww .j  a va2  s. c o m*/
 * @param p the permutation
 */
public static void inPlace(int[] v, int[] p) {
    inputValidator(v, p);
    final int[] tmp = permuter(v, p);
    System.arraycopy(tmp, 0, v, 0, v.length);
    return;
}

From source file:Main.java

/**
 * Concatenates a list of float arrays into a single array.
 * //from   ww w .  j  av 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 float[] concatAllFloat(float[]... arrays) {
    int totalLength = 0;
    final int subArrayCount = arrays.length;
    for (int i = 0; i < subArrayCount; ++i) {
        totalLength += arrays[i].length;
    }
    float[] 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

public static void sendWoLMagicPacket(final String broadcastIp, final String macAddress) {
    new Thread(new Runnable() {
        @Override/*from w  w  w.  j  ava  2  s. com*/
        public void run() {
            try {
                byte[] macBytes = getMacBytes(macAddress);
                byte[] bytes = new byte[6 + 16 * macBytes.length];
                for (int i = 0; i < 6; i++) {
                    bytes[i] = (byte) 0xff;
                }
                for (int i = 6; i < bytes.length; i += macBytes.length) {
                    System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
                }

                InetAddress address = InetAddress.getByName(broadcastIp);
                DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, 9);
                DatagramSocket socket = new DatagramSocket();
                socket.send(packet);
                socket.close();

                packet = new DatagramPacket(bytes, bytes.length, address, 7);
                socket = new DatagramSocket();
                socket.send(packet);
                socket.close();

                Log.e("WAKE_UP", "Wake-on-LAN packet sent.");
                final String output = getStringFromBytes(bytes);
                Log.i("WAKE_UP", output);
            } catch (Exception e) {
                Log.e("WAKE_UP", "Failed to send Wake-on-LAN packet: " + e);

            }
        }
    }).start();
}