From 9b62af67f992b2a5862547da2e9aec89b11037f1 Mon Sep 17 00:00:00 2001 From: Xiaoyan Lin Date: Tue, 22 Dec 2015 16:28:03 -0800 Subject: [PATCH] Close FileInputStream after consuming it in SelfSignedCertificate Motivation: FileInputStream opened by SelfSignedCertificate wasn't closed. Modifications: Use a try-finally to close the opened FileInputStream. Result: FileInputStream will be closed properly. --- .../handler/ssl/util/SelfSignedCertificate.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/handler/src/main/java/io/netty/handler/ssl/util/SelfSignedCertificate.java b/handler/src/main/java/io/netty/handler/ssl/util/SelfSignedCertificate.java index 770d22f161..d436473772 100644 --- a/handler/src/main/java/io/netty/handler/ssl/util/SelfSignedCertificate.java +++ b/handler/src/main/java/io/netty/handler/ssl/util/SelfSignedCertificate.java @@ -162,11 +162,20 @@ public final class SelfSignedCertificate { certificate = new File(paths[0]); privateKey = new File(paths[1]); key = keypair.getPrivate(); + FileInputStream certificateInput = null; try { - cert = (X509Certificate) CertificateFactory.getInstance("X509").generateCertificate( - new FileInputStream(certificate)); + certificateInput = new FileInputStream(certificate); + cert = (X509Certificate) CertificateFactory.getInstance("X509").generateCertificate(certificateInput); } catch (Exception e) { throw new CertificateEncodingException(e); + } finally { + if (certificateInput != null) { + try { + certificateInput.close(); + } catch (IOException e) { + logger.warn("Failed to close a file: " + certificate, e); + } + } } }