modules/jretools/src/main/java/org/apache/harmony/jretools/keytool/CertExporter.java [54:105]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    static void exportCert(KeytoolParameters param) throws KeyStoreException,
            IOException, KeytoolException, NoSuchAlgorithmException,
            CertificateException, NoSuchProviderException {
        KeyStore keyStore = param.getKeyStore();
        String alias = param.getAlias();
        if (keyStore.entryInstanceOf(alias, KeyStore.SecretKeyEntry.class)) {
            throw new KeytoolException("The alias <" + alias
                    + "> points to a secret key entry.\n"
                    + "It has no certificates.");
        }

        X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);
        byte[] encodedCert;
        try {
            encodedCert = cert.getEncoded();
        } catch (CertificateEncodingException e) {
            throw new CertificateEncodingException(
                    "Failed to encode the certificate", e);
        }

        OutputStream output;
        String fileName = param.getFileName();
        // if no file name is given, output to System.out
        if (fileName == null) {
            output = System.out;
        } else { // output to a file if the name is supplied
            File file = new File(fileName);
            // the file will be created if it doesn't already exist.
            // If it already exists and is not a file, then an IOException will
            // be thrown.
            file.createNewFile();

            output = new BufferedOutputStream(new FileOutputStream(file));
        }

        if (param.isRfc()) {
            output.write("-----BEGIN CERTIFICATE-----\n".getBytes());
            output.write(Base64.encode(encodedCert, "ISO-8859-1").getBytes());
            output.write("\n-----END CERTIFICATE-----\n".getBytes());
        } else {
            output.write(encodedCert);
        }
        output.flush();
        if (output != System.out) {
            output.close();

            if (param.isVerbose()) {
                System.out.println("The certificate is stored in file <"
                        + fileName + ">.");
            }
        }
    }
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



modules/jdktools/src/main/java/org/apache/harmony/tools/keytool/CertExporter.java [54:105]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    static void exportCert(KeytoolParameters param) throws KeyStoreException,
            IOException, KeytoolException, NoSuchAlgorithmException,
            CertificateException, NoSuchProviderException {
        KeyStore keyStore = param.getKeyStore();
        String alias = param.getAlias();
        if (keyStore.entryInstanceOf(alias, KeyStore.SecretKeyEntry.class)) {
            throw new KeytoolException("The alias <" + alias
                    + "> points to a secret key entry.\n"
                    + "It has no certificates.");
        }

        X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);
        byte[] encodedCert;
        try {
            encodedCert = cert.getEncoded();
        } catch (CertificateEncodingException e) {
            throw new CertificateEncodingException(
                    "Failed to encode the certificate", e);
        }

        OutputStream output;
        String fileName = param.getFileName();
        // if no file name is given, output to System.out
        if (fileName == null) {
            output = System.out;
        } else { // output to a file if the name is supplied
            File file = new File(fileName);
            // the file will be created if it doesn't already exist.
            // If it already exists and is not a file, then an IOException will
            // be thrown.
            file.createNewFile();

            output = new BufferedOutputStream(new FileOutputStream(file));
        }

        if (param.isRfc()) {
            output.write("-----BEGIN CERTIFICATE-----\n".getBytes());
            output.write(Base64.encode(encodedCert, "ISO-8859-1").getBytes());
            output.write("\n-----END CERTIFICATE-----\n".getBytes());
        } else {
            output.write(encodedCert);
        }
        output.flush();
        if (output != System.out) {
            output.close();

            if (param.isVerbose()) {
                System.out.println("The certificate is stored in file <"
                        + fileName + ">.");
            }
        }
    }
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



