提交 87309953 编写于 作者: M mullan

7054969: Null-check-in-finally pattern in java/security documentation

Reviewed-by: vinnie
上级 2c7f8030
...@@ -113,14 +113,8 @@ import javax.security.auth.callback.*; ...@@ -113,14 +113,8 @@ import javax.security.auth.callback.*;
* // get user password and file input stream * // get user password and file input stream
* char[] password = getPassword(); * char[] password = getPassword();
* *
* java.io.FileInputStream fis = null; * try (FileInputStream fis = new FileInputStream("keyStoreName")) {
* try {
* fis = new java.io.FileInputStream("keyStoreName");
* ks.load(fis, password); * ks.load(fis, password);
* } finally {
* if (fis != null) {
* fis.close();
* }
* } * }
* </pre> * </pre>
* *
...@@ -146,14 +140,8 @@ import javax.security.auth.callback.*; ...@@ -146,14 +140,8 @@ import javax.security.auth.callback.*;
* ks.setEntry("secretKeyAlias", skEntry, protParam); * ks.setEntry("secretKeyAlias", skEntry, protParam);
* *
* // store away the keystore * // store away the keystore
* java.io.FileOutputStream fos = null; * try (FileOutputStream fos = new FileOutputStream("newKeyStoreName")) {
* try {
* fos = new java.io.FileOutputStream("newKeyStoreName");
* ks.store(fos, password); * ks.store(fos, password);
* } finally {
* if (fos != null) {
* fos.close();
* }
* } * }
* </pre> * </pre>
* *
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -94,15 +94,9 @@ import sun.security.x509.X509CRLImpl; ...@@ -94,15 +94,9 @@ import sun.security.x509.X509CRLImpl;
* CRLs are instantiated using a certificate factory. The following is an * CRLs are instantiated using a certificate factory. The following is an
* example of how to instantiate an X.509 CRL: * example of how to instantiate an X.509 CRL:
* <pre><code> * <pre><code>
* InputStream inStream = null; * try (InputStream inStream = new FileInputStream("fileName-of-crl")) {
* try {
* inStream = new FileInputStream("fileName-of-crl");
* CertificateFactory cf = CertificateFactory.getInstance("X.509"); * CertificateFactory cf = CertificateFactory.getInstance("X.509");
* X509CRL crl = (X509CRL)cf.generateCRL(inStream); * X509CRL crl = (X509CRL)cf.generateCRL(inStream);
* } finally {
* if (inStream != null) {
* inStream.close();
* }
* } * }
* </code></pre> * </code></pre>
* *
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -89,15 +89,9 @@ import sun.security.x509.X509CertImpl; ...@@ -89,15 +89,9 @@ import sun.security.x509.X509CertImpl;
* Certificates are instantiated using a certificate factory. The following is * Certificates are instantiated using a certificate factory. The following is
* an example of how to instantiate an X.509 certificate: * an example of how to instantiate an X.509 certificate:
* <pre> * <pre>
* InputStream inStream = null; * try (InputStream inStream = new FileInputStream("fileName-of-cert")) {
* try {
* inStream = new FileInputStream("fileName-of-cert");
* CertificateFactory cf = CertificateFactory.getInstance("X.509"); * CertificateFactory cf = CertificateFactory.getInstance("X.509");
* X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream); * X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream);
* } finally {
* if (inStream != null) {
* inStream.close();
* }
* } * }
* </pre> * </pre>
* *
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -85,16 +85,10 @@ public interface X509Extension { ...@@ -85,16 +85,10 @@ public interface X509Extension {
* Here is sample code to get a Set of critical extensions from an * Here is sample code to get a Set of critical extensions from an
* X509Certificate and print the OIDs: * X509Certificate and print the OIDs:
* <pre><code> * <pre><code>
* InputStream inStrm = null;
* X509Certificate cert = null; * X509Certificate cert = null;
* try { * try (InputStream inStrm = new FileInputStream("DER-encoded-Cert")) {
* inStrm = new FileInputStream("DER-encoded-Cert");
* CertificateFactory cf = CertificateFactory.getInstance("X.509"); * CertificateFactory cf = CertificateFactory.getInstance("X.509");
* cert = (X509Certificate)cf.generateCertificate(inStrm); * cert = (X509Certificate)cf.generateCertificate(inStrm);
* } finally {
* if (inStrm != null) {
* inStrm.close();
* }
* }<p> * }<p>
* *
* Set<String> critSet = cert.getCriticalExtensionOIDs(); * Set<String> critSet = cert.getCriticalExtensionOIDs();
...@@ -120,23 +114,16 @@ public interface X509Extension { ...@@ -120,23 +114,16 @@ public interface X509Extension {
* Here is sample code to get a Set of non-critical extensions from an * Here is sample code to get a Set of non-critical extensions from an
* X509CRL revoked certificate entry and print the OIDs: * X509CRL revoked certificate entry and print the OIDs:
* <pre><code> * <pre><code>
* InputStream inStrm = null;
* CertificateFactory cf = null; * CertificateFactory cf = null;
* X509CRL crl = null; * X509CRL crl = null;
* try { * try (InputStream inStrm = new FileInputStream("DER-encoded-CRL")) {
* inStrm = new FileInputStream("DER-encoded-CRL");
* cf = CertificateFactory.getInstance("X.509"); * cf = CertificateFactory.getInstance("X.509");
* crl = (X509CRL)cf.generateCRL(inStrm); * crl = (X509CRL)cf.generateCRL(inStrm);
* } finally {
* if (inStrm != null) {
* inStrm.close();
* }
* }<p> * }<p>
* *
* byte[] certData = &lt;DER-encoded certificate data&gt; * byte[] certData = &lt;DER-encoded certificate data&gt;
* ByteArrayInputStream bais = new ByteArrayInputStream(certData); * ByteArrayInputStream bais = new ByteArrayInputStream(certData);
* X509Certificate cert = (X509Certificate)cf.generateCertificate(bais); * X509Certificate cert = (X509Certificate)cf.generateCertificate(bais);
* bais.close();
* X509CRLEntry badCert = * X509CRLEntry badCert =
* crl.getRevokedCertificate(cert.getSerialNumber());<p> * crl.getRevokedCertificate(cert.getSerialNumber());<p>
* *
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册