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 void main(String[] args) {
    long[] prime = new long[MAX];

    long stop = DEFAULT_STOP;
    stop = 100;/*  w w w  .j  a v a  2 s  .  co  m*/

    prime[1] = FP; // P1 (ignore prime[0])
    long n = FP + 1; // odd candidates
    int j = 1; // numberFound

    boolean isPrime = true; // for 3

    do {

        if (isPrime) {
            if (j == MAX - 1) {
                // Grow array dynamically if needed
                long[] np = new long[MAX * 2];
                System.arraycopy(prime, 0, np, 0, MAX);
                MAX *= 2;
                prime = np;
            }
            prime[++j] = n; // P2
            isPrime = false;
        }
        n += 2; // P4

        for (int k = 2; k <= j && k < MAX; k++) { // P5, P6, P8
            long q = n / prime[k];
            long r = n % prime[k];
            if (r == 0) {
                break;
            }
            if (q <= prime[k]) { // P7
                isPrime = true;
                break;
            }
        }

    } while (n < stop); // P3

    for (int i = 1; i <= j; i++)
        System.out.println(prime[i]);
}

From source file:Primes.java

public static void main(String[] args) {
    long[] prime = new long[MAX];

    long stop = DEFAULT_STOP;
    if (args.length == 1) {
        stop = Long.parseLong(args[0]);
    }//from ww  w . java 2 s .  c o m

    prime[1] = FP; // P1 (ignore prime[0])
    long n = FP + 1; // odd candidates
    int j = 1; // numberFound

    boolean isPrime = true; // for 3

    do {

        if (isPrime) {
            if (j == MAX - 1) {
                // Grow array dynamically if needed
                long[] np = new long[MAX * 2];
                System.arraycopy(prime, 0, np, 0, MAX);
                MAX *= 2;
                prime = np;
            }
            prime[++j] = n; // P2
            isPrime = false;
        }
        n += 2; // P4

        for (int k = 2; k <= j && k < MAX; k++) { // P5, P6, P8
            long q = n / prime[k];
            long r = n % prime[k];
            if (r == 0) {
                break;
            }
            if (q <= prime[k]) { // P7
                isPrime = true;
                break;
            }
        }

    } while (n < stop); // P3

    for (int i = 1; i <= j; i++)
        System.out.println(prime[i]);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    byte[] input = "input".getBytes();
    byte[] keyBytes = "input123".getBytes();
    byte[] ivBytes = "12345123".getBytes();

    SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
    IvParameterSpec ivSpec = new IvParameterSpec(new byte[8]);
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS7Padding", "BC");

    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);

    byte[] cipherText = new byte[cipher.getOutputSize(ivBytes.length + input.length)];

    int ctLength = cipher.update(ivBytes, 0, ivBytes.length, cipherText, 0);

    ctLength += cipher.update(input, 0, input.length, cipherText, ctLength);

    ctLength += cipher.doFinal(cipherText, ctLength);

    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);

    byte[] buf = new byte[cipher.getOutputSize(ctLength)];

    int bufLength = cipher.update(cipherText, 0, ctLength, buf, 0);

    bufLength += cipher.doFinal(buf, bufLength);

    byte[] plainText = new byte[bufLength - ivBytes.length];

    System.arraycopy(buf, ivBytes.length, plainText, 0, plainText.length);

    System.out.println("plain : " + new String(plainText));
}

From source file:edu.scripps.fl.pubchem.app.RelatedCurveProcessor.java

public static void main(String[] args) throws Exception {
    CommandLineHandler clh = new CommandLineHandler();
    args = clh.handle(args);//w ww. ja v a2s . c  o  m

    Integer[] list = (Integer[]) ConvertUtils.convert(args, Integer[].class);
    Integer referenceAid = list[0];
    Integer[] relatedAids = new Integer[list.length - 1];
    System.arraycopy(list, 1, relatedAids, 0, list.length - 1);

    URL url = PipelineUtils.class.getClassLoader()
            .getResource("edu/scripps/fl/pubchem/RelatedCurvesPipeline.xml");
    Pipeline pipeline = new PipelineUtils().createPipeline(url, Arrays.asList(relatedAids));
    List<Stage> stages = pipeline.getStages();
    BeanUtils.setProperty(stages.get(1), "referenceAID", referenceAid);
    pipeline.run();
    new PipelineUtils().logErrors(pipeline);
}

From source file:Snippet151.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.BORDER | SWT.VIRTUAL);
    table.addListener(SWT.SetData, new Listener() {
        public void handleEvent(Event e) {
            TableItem item = (TableItem) e.item;
            int index = table.indexOf(item);
            item.setText("Item " + data[index]);
        }//from   w  ww . j  ava  2s.c o  m
    });
    Thread thread = new Thread() {
        public void run() {
            int count = 0;
            Random random = new Random();
            while (count++ < 500) {
                if (table.isDisposed())
                    return;
                // add 10 random numbers to array and sort
                int grow = 10;
                int[] newData = new int[data.length + grow];
                System.arraycopy(data, 0, newData, 0, data.length);
                int index = data.length;
                data = newData;
                for (int j = 0; j < grow; j++) {
                    data[index++] = random.nextInt();
                }
                Arrays.sort(data);
                display.syncExec(new Runnable() {
                    public void run() {
                        if (table.isDisposed())
                            return;
                        table.setItemCount(data.length);
                        table.clearAll();
                    }
                });
                try {
                    Thread.sleep(500);
                } catch (Throwable t) {
                }
            }
        }
    };
    thread.start();
    shell.open();
    while (!shell.isDisposed() || thread.isAlive()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:ArrayCopyDemo.java

public static void main(String[] args) {
    char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd' };
    char[] copyTo = new char[7];

    System.arraycopy(copyFrom, 2, copyTo, 0, 7);
    System.out.println(new String(copyTo));
}

From source file:com.example.java.collections.ArrayExample.java

public static void main(String[] args) {

    /* ########################################################### */
    // Initializing an Array
    String[] creatures = { "goldfish", "oscar", "guppy", "minnow" };
    int[] numbers = new int[10];
    int counter = 0;
    while (counter < numbers.length) {
        numbers[counter] = counter;//from   w  w w .  j a  v a 2 s  .c  o m
        System.out.println("number[" + counter + "]: " + counter);
        counter++;
    }
    for (int theInt : numbers) {
        System.out.println(theInt);
    }
    //System.out.println(numbers[numbers.length]);
    /* ########################################################### */

    /* ########################################################### */
    // Using Charecter Array
    String name = "Michael";
    // Charecter Array
    char[] charName = name.toCharArray();
    System.out.println(charName);

    // Array Fuctions
    char[] html = new char[] { 'M', 'i', 'c', 'h', 'a', 'e', 'l' };
    char[] lastFour = new char[4];
    System.arraycopy(html, 3, lastFour, 0, lastFour.length);
    System.out.println(lastFour);
    /* ########################################################### */

    /* ########################################################### */
    // Using Arrays of Other Types
    Object[] person = new Object[] { "Michael", new Integer(94), new Integer(1), new Date() };

    String fname = (String) person[0]; //ok
    Integer age = (Integer) person[1]; //ok
    Date start = (Date) person[2]; //oops!
    /* ########################################################### */

    /* ########################################################### */
    // Muti Dimestional Array
    String[][] bits = { { "Michael", "Ernest", "MFE" }, { "Ernest", "Friedman-Hill", "EFH" },
            { "Kathi", "Duggan", "KD" }, { "Jeff", "Kellum", "JK" } };

    bits[0] = new String[] { "Rudy", "Polanski", "RP" };
    bits[1] = new String[] { "Rudy", "Washington", "RW" };
    bits[2] = new String[] { "Rudy", "O'Reilly", "RO" };
    /* ########################################################### */

    /* ########################################################### */
    //Create ArrayList from array
    String[] stringArray = { "a", "b", "c", "d", "e" };
    ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
    System.out.println(arrayList);
    // [a, b, c, d, e]
    /* ########################################################### */

    /* ########################################################### */
    //Check if an array contains a certain value
    String[] stringArray1 = { "a", "b", "c", "d", "e" };
    boolean b = Arrays.asList(stringArray).contains("a");
    System.out.println(b);
    // true
    /* ########################################################### */

    /* ########################################################### */
    //Concatenate two arrays
    int[] intArray = { 1, 2, 3, 4, 5 };
    int[] intArray2 = { 6, 7, 8, 9, 10 };
    // Apache Commons Lang library
    int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
    /* ########################################################### */

    /* ########################################################### */
    //Joins the elements of the provided array into a single String
    // Apache common lang
    String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
    System.out.println(j);
    // a, b, c
    /* ########################################################### */

    /* ########################################################### */
    //Covnert ArrayList to Array
    String[] stringArray3 = { "a", "b", "c", "d", "e" };
    ArrayList<String> arrayList1 = new ArrayList<String>(Arrays.asList(stringArray));
    String[] stringArr = new String[arrayList.size()];
    arrayList.toArray(stringArr);
    for (String s : stringArr) {
        System.out.println(s);
    }
    /* ########################################################### */

    /* ########################################################### */
    //Convert Array to Set
    Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
    System.out.println(set);
    //[d, e, b, c, a]
    /* ########################################################### */

    /* ########################################################### */
    //Reverse an array
    int[] intArray1 = { 1, 2, 3, 4, 5 };
    ArrayUtils.reverse(intArray1);
    System.out.println(Arrays.toString(intArray1));
    //[5, 4, 3, 2, 1]
    /* ########################################################### */

    /* ########################################################### */
    // Remove element of an array
    int[] intArray3 = { 1, 2, 3, 4, 5 };
    int[] removed = ArrayUtils.removeElement(intArray3, 3);//create a new array
    System.out.println(Arrays.toString(removed));
    /* ########################################################### */

    /* ########################################################### */
    byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
    for (byte t : bytes) {
        System.out.format("0x%x ", t);
    }
    /* ########################################################### */

}

From source file:JarClassLoader.java

public static void main(String[] args) throws Exception {
    URL url = new URL(args[0]);
    JarClassLoader cl = new JarClassLoader(url);
    String name = null;//from w w  w  . j a  v a2  s  . c  om
    name = cl.getMainClassName();
    if (name == null) {
        fatal("Specified jar file does not contain a 'Main-Class'" + " manifest attribute");
    }
    String[] newArgs = new String[args.length - 1];
    System.arraycopy(args, 1, newArgs, 0, newArgs.length);
    cl.invokeClass(name, newArgs);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    SecureRandom random = new SecureRandom();
    IvParameterSpec ivSpec = createCtrIvForAES();
    Key key = createKeyForAES(256, random);
    Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC");
    String input = "12345678";
    Mac mac = Mac.getInstance("DES", "BC");
    byte[] macKeyBytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
    Key macKey = new SecretKeySpec(macKeyBytes, "DES");

    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);

    byte[] cipherText = new byte[cipher.getOutputSize(input.length() + mac.getMacLength())];

    int ctLength = cipher.update(input.getBytes(), 0, input.length(), cipherText, 0);

    mac.init(macKey);/*from  ww  w.ja va  2 s .co m*/
    mac.update(input.getBytes());

    ctLength += cipher.doFinal(mac.doFinal(), 0, mac.getMacLength(), cipherText, ctLength);

    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);

    byte[] plainText = cipher.doFinal(cipherText, 0, ctLength);
    int messageLength = plainText.length - mac.getMacLength();

    mac.init(macKey);
    mac.update(plainText, 0, messageLength);

    byte[] messageHash = new byte[mac.getMacLength()];
    System.arraycopy(plainText, messageLength, messageHash, 0, messageHash.length);

    System.out.println("plain : " + new String(plainText) + " verified: "
            + MessageDigest.isEqual(mac.doFinal(), messageHash));

}

From source file:Snippet80.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER | SWT.MULTI);
    for (int i = 0; i < 2; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("item " + i);
        for (int j = 0; j < 2; j++) {
            TreeItem subItem = new TreeItem(item, SWT.NONE);
            subItem.setText("item " + j);
            for (int k = 0; k < 2; k++) {
                TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
                subsubItem.setText("item " + k);
            }//from  ww w.j a  v  a2 s  .  c o m
        }
    }

    tree.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            TreeItem[] selection = tree.getSelection();
            TreeItem[] revisedSelection = new TreeItem[0];
            for (int i = 0; i < selection.length; i++) {
                String text = selection[i].getText();
                if (text.indexOf("1") > 0) {
                    TreeItem[] newSelection = new TreeItem[revisedSelection.length + 1];
                    System.arraycopy(revisedSelection, 0, newSelection, 0, revisedSelection.length);
                    newSelection[revisedSelection.length] = selection[i];
                    revisedSelection = newSelection;
                }
            }
            tree.setSelection(revisedSelection);
        }
    });

    shell.setSize(300, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}