3232import java .security .cert .CertificateException ;
3333import java .security .cert .CertificateFactory ;
3434
35+ /**
36+ * Utility class for loading X.509 certificates and Java KeyStores.
37+ */
3538public final class CertificateUtil {
3639
3740 private CertificateUtil () {
3841 }
3942
4043 /**
41- * Load Certificate from system file path
42- * @param filePath Path of certificate file
43- * @return Certificate
44+ * Loads an X.509 certificate from the given file path.
45+ * @param filePath Filepath of the certificate
46+ * @return instance of the certificate
4447 * @throws FileNotFoundException if file is not found
45- * @throws CertificateException if any error occurred while reading certificate
48+ * @throws CertificateException if any error occurred while reading the certificate
4649 */
4750 public static Certificate loadCertificate (String filePath ) throws FileNotFoundException , CertificateException {
4851 return loadCertificate (new FileInputStream (filePath ));
4952 }
5053
5154 /**
52- * Load Certificate from a input stream
53- * @param inputStream InputStream containing certificate data
54- * @return Certificate
55+ * Loads an X.509 certificate from an input stream.
56+ * @param inputStream InputStream containing the certificate.
57+ * @return instance of the certificate
5558 * @throws CertificateException if any error occurred while reading certificate from stream
5659 */
5760 public static Certificate loadCertificate (InputStream inputStream ) throws CertificateException {
@@ -60,15 +63,15 @@ public static Certificate loadCertificate(InputStream inputStream) throws Certif
6063 }
6164
6265 /**
63- * Load KeyStore from given path and type
66+ * Loads a KeyStore from the specified file path using the given type and password.
6467 * @param keyStorePath file path
6568 * @param keyStoreType keystore type (JKS/PKCS12 etc)
6669 * @param keyStorePassword keystore password
67- * @return
68- * @throws KeyStoreException
69- * @throws CertificateException
70- * @throws NoSuchAlgorithmException
71- * @throws IOException
70+ * @return the loaded KeyStore instance.
71+ * @throws KeyStoreException if the keystore type is invalid.
72+ * @throws CertificateException if a certificate in the keystore could not be loaded.
73+ * @throws NoSuchAlgorithmException if the algorithm for checking the keystore integrity cannot be found.
74+ * @throws IOException if there is an I/O or format problem with the keystore data.
7275 */
7376 public static KeyStore loadKeyStore (String keyStorePath , String keyStoreType , String keyStorePassword ) throws KeyStoreException , CertificateException , NoSuchAlgorithmException , IOException {
7477
@@ -90,9 +93,10 @@ public static KeyStore loadKeyStore(String keyStorePath, String keyStoreType, St
9093 throw new FileNotFoundException ("Keystore file not found at path " + keyStorePath );
9194 }
9295
93- FileInputStream inputStream = new FileInputStream (file );
94- keyStore .load (inputStream , password );
95- inputStream .close ();
96+ try (FileInputStream inputStream = new FileInputStream (file )) {
97+ keyStore .load (inputStream , password );
98+ }
99+
96100 return keyStore ;
97101 }
98102}
0 commit comments