changeset 4416:f68d30c0a2e3

7054969: Null-check-in-finally pattern in java/security documentation Reviewed-by: vinnie
author mullan
date Wed, 06 Jul 2011 11:08:20 -0400
parents e88093d75e36
children 63be90976177
files src/share/classes/java/security/KeyStore.java src/share/classes/java/security/cert/X509CRL.java src/share/classes/java/security/cert/X509Certificate.java src/share/classes/java/security/cert/X509Extension.java
diffstat 4 files changed, 9 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/security/KeyStore.java	Tue Jul 05 15:25:10 2011 +0100
+++ b/src/share/classes/java/security/KeyStore.java	Wed Jul 06 11:08:20 2011 -0400
@@ -113,14 +113,8 @@
  *    // get user password and file input stream
  *    char[] password = getPassword();
  *
- *    java.io.FileInputStream fis = null;
- *    try {
- *        fis = new java.io.FileInputStream("keyStoreName");
+ *    try (FileInputStream fis = new FileInputStream("keyStoreName")) {
  *        ks.load(fis, password);
- *    } finally {
- *        if (fis != null) {
- *            fis.close();
- *        }
  *    }
  * </pre>
  *
@@ -146,14 +140,8 @@
  *    ks.setEntry("secretKeyAlias", skEntry, protParam);
  *
  *    // store away the keystore
- *    java.io.FileOutputStream fos = null;
- *    try {
- *        fos = new java.io.FileOutputStream("newKeyStoreName");
+ *    try (FileOutputStream fos = new FileOutputStream("newKeyStoreName")) {
  *        ks.store(fos, password);
- *    } finally {
- *        if (fos != null) {
- *            fos.close();
- *        }
  *    }
  * </pre>
  *
--- a/src/share/classes/java/security/cert/X509CRL.java	Tue Jul 05 15:25:10 2011 +0100
+++ b/src/share/classes/java/security/cert/X509CRL.java	Wed Jul 06 11:08:20 2011 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -94,15 +94,9 @@
  * CRLs are instantiated using a certificate factory. The following is an
  * example of how to instantiate an X.509 CRL:
  * <pre><code>
- * InputStream inStream = null;
- * try {
- *     inStream = new FileInputStream("fileName-of-crl");
+ * try (InputStream inStream = new FileInputStream("fileName-of-crl")) {
  *     CertificateFactory cf = CertificateFactory.getInstance("X.509");
  *     X509CRL crl = (X509CRL)cf.generateCRL(inStream);
- * } finally {
- *     if (inStream != null) {
- *         inStream.close();
- *     }
  * }
  * </code></pre>
  *
--- a/src/share/classes/java/security/cert/X509Certificate.java	Tue Jul 05 15:25:10 2011 +0100
+++ b/src/share/classes/java/security/cert/X509Certificate.java	Wed Jul 06 11:08:20 2011 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -89,15 +89,9 @@
  * Certificates are instantiated using a certificate factory. The following is
  * an example of how to instantiate an X.509 certificate:
  * <pre>
- * InputStream inStream = null;
- * try {
- *     inStream = new FileInputStream("fileName-of-cert");
+ * try (InputStream inStream = new FileInputStream("fileName-of-cert")) {
  *     CertificateFactory cf = CertificateFactory.getInstance("X.509");
  *     X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream);
- * } finally {
- *     if (inStream != null) {
- *         inStream.close();
- *     }
  * }
  * </pre>
  *
--- a/src/share/classes/java/security/cert/X509Extension.java	Tue Jul 05 15:25:10 2011 +0100
+++ b/src/share/classes/java/security/cert/X509Extension.java	Wed Jul 06 11:08:20 2011 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -85,16 +85,10 @@
      * Here is sample code to get a Set of critical extensions from an
      * X509Certificate and print the OIDs:
      * <pre><code>
-     * InputStream inStrm = null;
      * X509Certificate cert = null;
-     * try {
-     *     inStrm = new FileInputStream("DER-encoded-Cert");
+     * try (InputStream inStrm = new FileInputStream("DER-encoded-Cert")) {
      *     CertificateFactory cf = CertificateFactory.getInstance("X.509");
      *     cert = (X509Certificate)cf.generateCertificate(inStrm);
-     * } finally {
-     *     if (inStrm != null) {
-     *         inStrm.close();
-     *     }
      * }<p>
      *
      * Set<String> critSet = cert.getCriticalExtensionOIDs();
@@ -120,23 +114,16 @@
      * Here is sample code to get a Set of non-critical extensions from an
      * X509CRL revoked certificate entry and print the OIDs:
      * <pre><code>
-     * InputStream inStrm = null;
      * CertificateFactory cf = null;
      * X509CRL crl = null;
-     * try {
-     *     inStrm = new FileInputStream("DER-encoded-CRL");
+     * try (InputStream inStrm = new FileInputStream("DER-encoded-CRL")) {
      *     cf = CertificateFactory.getInstance("X.509");
      *     crl = (X509CRL)cf.generateCRL(inStrm);
-     * } finally {
-     *     if (inStrm != null) {
-     *         inStrm.close();
-     *     }
      * }<p>
      *
      * byte[] certData = &lt;DER-encoded certificate data&gt;
      * ByteArrayInputStream bais = new ByteArrayInputStream(certData);
      * X509Certificate cert = (X509Certificate)cf.generateCertificate(bais);
-     * bais.close();
      * X509CRLEntry badCert =
      *              crl.getRevokedCertificate(cert.getSerialNumber());<p>
      *