Example usage for java.util Collection toArray

List of usage examples for java.util Collection toArray

Introduction

In this page you can find the example usage for java.util Collection toArray.

Prototype

Object[] toArray();

Source Link

Document

Returns an array containing all of the elements in this collection.

Usage

From source file:ImportKey.java

/**
 * <p>//from  w ww.  j  ava2  s .c o  m
 * Takes two file names for a key and the certificate for the key, and
 * imports those into a keystore. Optionally it takes an alias for the key.
 * <p>
 * The first argument is the filename for the key. The key should be in
 * PKCS8-format.
 * <p>
 * The second argument is the filename for the certificate for the key.
 * <p>
 * If a third argument is given it is used as the alias. If missing, the key
 * is imported with the alias importkey
 * <p>
 * The name of the keystore file can be controlled by setting the keystore
 * property (java -Dkeystore=mykeystore). If no name is given, the file is
 * named <code>keystore.ImportKey</code> and placed in your home directory.
 * 
 * @param args
 *            [0] Name of the key file, [1] Name of the certificate file [2]
 *            Alias for the key.
 **/
public static void main(String args[]) {

    // change this if you want another password by default
    String keypass = "password";

    // change this if you want another alias by default
    String defaultalias = "tomcat";

    // change this if you want another keystorefile by default
    String keystorename = null;

    // parsing command line input
    String keyfile = "";
    String certfile = "";
    if (args.length < 3 || args.length > 4) {
        System.out.println("Usage: java comu.ImportKey keystore keyfile certfile [alias]");
        System.exit(0);
    } else {
        keystorename = args[0];
        keyfile = args[1];
        certfile = args[2];
        if (args.length > 3)
            defaultalias = args[3];
    }

    try {
        // initializing and clearing keystore
        KeyStore ks = KeyStore.getInstance("JKS", "SUN");
        ks.load(null, keypass.toCharArray());
        System.out.println("Using keystore-file : " + keystorename);
        ks.store(new FileOutputStream(keystorename), keypass.toCharArray());
        ks.load(new FileInputStream(keystorename), keypass.toCharArray());

        // loading Key
        InputStream fl = fullStream(keyfile);
        byte[] key = new byte[fl.available()];
        KeyFactory kf = KeyFactory.getInstance("RSA");
        fl.read(key, 0, fl.available());
        fl.close();
        PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(key);
        PrivateKey ff = kf.generatePrivate(keysp);

        // loading CertificateChain
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        InputStream certstream = fullStream(certfile);

        Collection c = cf.generateCertificates(certstream);
        Certificate[] certs = new Certificate[c.toArray().length];

        if (c.size() == 1) {
            certstream = fullStream(certfile);
            System.out.println("One certificate, no chain.");
            Certificate cert = cf.generateCertificate(certstream);
            certs[0] = cert;
        } else {
            System.out.println("Certificate chain length: " + c.size());
            certs = (Certificate[]) c.toArray(new Certificate[c.size()]);
        }

        // storing keystore
        ks.setKeyEntry(defaultalias, ff, keypass.toCharArray(), certs);
        System.out.println("Key and certificate stored.");
        System.out.println("Alias:" + defaultalias + "  Password:" + keypass);
        ks.store(new FileOutputStream(keystorename), keypass.toCharArray());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.glaf.core.util.JsonUtils.java

public static void main(String[] args) throws Exception {
    Map<String, Object> dataMap = new java.util.HashMap<String, Object>();
    dataMap.put("key01", "");
    dataMap.put("key02", 12345);
    dataMap.put("key03", 789.85D);
    dataMap.put("date", new Date());
    Collection<Object> actorIds = new HashSet<Object>();
    actorIds.add("sales01");
    actorIds.add("sales02");
    actorIds.add("sales03");
    actorIds.add("sales04");
    actorIds.add("sales05");
    dataMap.put("actorIds", actorIds.toArray());
    dataMap.put("x_sale_actor_actorIds", actorIds);

    Map<String, Object> xxxMap = new java.util.HashMap<String, Object>();
    xxxMap.put("0", "--------");
    xxxMap.put("1", "?");
    xxxMap.put("2", "");
    xxxMap.put("3", "");

    dataMap.put("trans", xxxMap);

    String str = JsonUtils.encode(dataMap);
    System.out.println(str);//from   w  ww  .j a  va  2  s .c o m
    Map<?, ?> p = JsonUtils.decode(str);
    System.out.println(p);
    System.out.println(p.get("date").getClass().getName());

    String xx = "{name:\"trans\",nodeType:\"select\",children:{\"1\":\"?\",\"3\":\"\",\"2\":\"\",\"0\":\"--------\"}}";
    Map<String, Object> xMap = JsonUtils.decode(xx);
    System.out.println(xMap);
    Set<Entry<String, Object>> entrySet = xMap.entrySet();
    for (Entry<String, Object> entry : entrySet) {
        String key = entry.getKey();
        Object value = entry.getValue();
        System.out.println(key + " = " + value);
        System.out.println(key.getClass().getName() + "  " + value.getClass().getName());
        if (value instanceof JSONObject) {
            JSONObject json = (JSONObject) value;
            Iterator<?> iter = json.keySet().iterator();
            while (iter.hasNext()) {
                String kk = (String) iter.next();
                System.out.println(kk + " = " + json.get(kk));
            }
        }
    }
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static Object[] CollectionToArray(Collection collection) {
    return collection.toArray();
}

From source file:Main.java

public static <E> E randomElement(Collection<E> elements) {
    return (E) elements.toArray()[randomIndex(elements)];
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E last(Collection<E> collection) {
    return (E) collection.toArray()[collection.size() - 1];
}

From source file:Main.java

public static String toString(Collection<?> coll) {
    return Arrays.toString(coll.toArray());
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T get(Collection<T> values, int index) {
    return (T) values.toArray()[index];
}

From source file:Main.java

public static String[] toStringArray(Collection coll) {
    Object objects[] = coll.toArray();
    int length = objects.length;
    String result[] = new String[length];
    for (int i = 0; i < length; i++)
        result[i] = objects[i].toString();

    return result;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] convertToArray(Collection<T> items) {
    return (T[]) items.toArray();
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E get(Collection<E> collection, int index) {
    return (E) collection.toArray()[index];
}