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[] copyOf(byte[] original, int newLength) {
    byte[] copy = new byte[newLength];
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;/*www  .ja  v a  2  s  .  c  o m*/
}

From source file:Main.java

/**
 * //w  ww  .  j a v a  2s.  c  om
 * @param src
 * @param dst
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T[] instanceArray(T src[], T dst[]) {
    T ret[] = null;
    if (src != null) {

        if (dst == null || dst.length != src.length) {
            dst = (T[]) Array.newInstance(src.getClass().getComponentType(), src.length);
        }

        ret = dst;
        System.arraycopy(src, 0, ret, 0, src.length);
    }

    return ret;
}

From source file:Main.java

public static byte[] createPinEncryption(String pin, String pan) {
    byte[] decryption = new byte[8];
    byte[] bytePin = new byte[8];
    byte[] bytePan = new byte[8];
    Arrays.fill(bytePin, (byte) 0xFF);
    Arrays.fill(bytePan, (byte) 0x00);

    byte[] srcPin = HexStringToByteArray(pin);
    byte[] srcPan = HexStringToByteArray(pan);

    bytePin[0] = 0x06;/*from w w  w  .  ja  va2s.com*/
    System.arraycopy(srcPin, 0, bytePin, 1, srcPin.length);
    System.arraycopy(srcPan, 2, bytePan, 2, srcPan.length - 2);

    for (int i = 0; i < 8; i++) {
        decryption[i] = (byte) (bytePin[i] ^ bytePan[i]);
    }

    return decryption;
}

From source file:Main.java

public static int[] getBitmapPixels(@NonNull Bitmap bitmap, int x, int y, int width, int height) {
    int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), x, y, width, height);
    final int[] subsetPixels = new int[width * height];
    for (int row = 0; row < height; row++) {
        System.arraycopy(pixels, (row * bitmap.getWidth()), subsetPixels, row * width, width);
    }// w  ww  .  ja  va  2  s.c  o m
    return subsetPixels;
}

From source file:Main.java

public static byte[] wa_pbkdf2(byte[] password, byte[] salt)
        throws InvalidKeySpecException, NoSuchAlgorithmException {
    /*//from w  w  w .j av a 2s  . c  om
     * 2 iterations, 20 keylength
     */

    byte[] last = new byte[salt.length + 4];
    System.arraycopy(salt, 0, last, 0, salt.length);
    last[last.length - 1] = 1;
    byte[] xorsum = hmacSha1(last, password);
    last = xorsum.clone();

    for (int i = 1; i < 2; i++) {
        last = hmacSha1(last, password);

        for (int j = 0; j < xorsum.length; j++)
            xorsum[j] = (byte) ((xorsum[j] & 0xFF) ^ (last[j] & 0xFF));
    }

    byte[] ret = new byte[20];
    System.arraycopy(xorsum, 0, ret, 0, 20);
    return ret;
}

From source file:Main.java

public static byte[] calMacCode(byte[] mak, byte[] src) {
    int srcLen = src.length;
    int m_len = srcLen % 8;
    int count = srcLen + (m_len == 0 ? m_len : (8 - m_len));

    byte[] calData = new byte[count];
    byte[] _data8 = new byte[8];
    Arrays.fill(calData, (byte) 0);
    Arrays.fill(_data8, (byte) 0);
    System.arraycopy(src, 0, calData, 0, srcLen);
    System.arraycopy(calData, 0, _data8, 0, 8);

    for (int i = 1; i < count / 8; i++) {
        for (int j = 0; j < 8; j++) {
            _data8[j] = (byte) (_data8[j] ^ calData[i * 8 + j]);
        }//w  w w .j  a va 2  s . c  om
    }

    return TriDesEncryption(mak, _data8);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] remove(T[] array, int start, int len, Class<T> clazz) {
    T[] r = (T[]) Array.newInstance(clazz, array.length - len);
    System.arraycopy(array, 0, r, 0, start);
    System.arraycopy(array, start + len, r, start, array.length - start - len);
    return r;/*  www  . j a  va 2 s.c  o  m*/
}

From source file:Main.java

public static <T> T[] subArray(T[] x, int start, int len) {
    if (start + len > x.length)
        throw new IllegalArgumentException("length + start > x.length");
    T[] out = null;/*from w  ww.  j  a  va2  s. co m*/
    try {
        out = (T[]) Array.newInstance(x.getClass().getComponentType(), len);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    }
    System.arraycopy(x, start, out, 0, len);
    return out;
}

From source file:Main.java

public static int[] addAll(int[] array1, int[] array2) {
    if (array1 == null) {
        return clone(array2);
    } else if (array2 == null) {
        return clone(array1);
    }/*from  w w  w . ja  va 2  s.  c  o  m*/
    int[] joinedArray = new int[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

private static <T> T[] filterNull(T[] views) {
    int end = 0;/*  ww  w .jav  a2s .co  m*/
    int length = views.length;
    for (int i = 0; i < length; i++) {
        T view = views[i];
        if (view != null) {
            views[end++] = view;
        }
    }
    if (end == length) {
        return views;
    }
    //noinspection unchecked
    T[] newViews = (T[]) Array.newInstance(views.getClass().getComponentType(), end);
    System.arraycopy(views, 0, newViews, 0, end);
    return newViews;
}