Skip to content

fix memory leak #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public static PrivateKey loadPrivateKey(String privateKey) {
}

public static PrivateKey loadPrivateKey(InputStream inputStream) {
if(inputStream == null){
throw new IllegalArgumentException("无效文件流");
}
ByteArrayOutputStream os = new ByteArrayOutputStream(2048);
byte[] buffer = new byte[1024];
String privateKey;
Expand All @@ -48,11 +51,21 @@ public static PrivateKey loadPrivateKey(InputStream inputStream) {
privateKey = os.toString("UTF-8");
} catch (IOException e) {
throw new IllegalArgumentException("无效的密钥", e);
}finally {
try {
inputStream.close();
os.close();
} catch (IOException e) {
// ignore
}
}
return loadPrivateKey(privateKey);
}

public static X509Certificate loadCertificate(InputStream inputStream) {
if(inputStream == null){
throw new IllegalArgumentException("无效文件流");
}
try {
CertificateFactory cf = CertificateFactory.getInstance("X509");
X509Certificate cert = (X509Certificate) cf.generateCertificate(inputStream);
Expand All @@ -64,6 +77,12 @@ public static X509Certificate loadCertificate(InputStream inputStream) {
throw new RuntimeException("证书尚未生效", e);
} catch (CertificateException e) {
throw new RuntimeException("无效的证书", e);
}finally {
try {
inputStream.close();
} catch (IOException e) {
// ignore
}
}
}

Expand Down