Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
Ensure closing of InputStream resources (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-burwig authored May 15, 2020
1 parent ef0a817 commit ac44b28
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -72,9 +73,8 @@ private static Certificate getCertificateFromBytes(final byte[] bytes)
*/
public PrivateKey getPrivateKey() {
if (this.privateKey == null) {
try {
InputStream privateKeyStream = resourceLoader.getResource(privateKeyPath)
.getInputStream();
Resource privateKeyResource = resourceLoader.getResource(privateKeyPath);
try (InputStream privateKeyStream = privateKeyResource.getInputStream()) {
this.privateKey = getPrivateKeyFromStream(privateKeyStream);
} catch (IOException e) {
logger.error("Failed to load private key from {}", privateKeyPath, e);
Expand All @@ -89,8 +89,8 @@ public PrivateKey getPrivateKey() {
*/
public Certificate getCertificate() {
if (this.certificate == null) {
try {
InputStream certStream = resourceLoader.getResource(certificatePath).getInputStream();
Resource certResource = resourceLoader.getResource(certificatePath);
try (InputStream certStream = certResource.getInputStream()) {
this.certificate = getCertificateFromStream(certStream);
} catch (IOException | CertificateException e) {
logger.error("Failed to load certificate from {}", certificatePath, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.io.InputStream;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.error.YAMLException;
import org.yaml.snakeyaml.introspector.BeanAccess;
Expand Down Expand Up @@ -47,7 +48,8 @@ public static RiskScoreParameters readFile(String path) throws UnableToLoadFileE
Yaml yaml = new Yaml(new YamlConstructorForProtoBuf());
yaml.setBeanAccess(BeanAccess.FIELD); /* no setters on RiskScoreParameters available */

try (InputStream inputStream = new ClassPathResource(path).getInputStream()) {
Resource riskScoreParametersResource = new ClassPathResource(path);
try (InputStream inputStream = riskScoreParametersResource.getInputStream()) {
Builder loaded = yaml.loadAs(inputStream, RiskScoreParameters.newBuilder().getClass());
if (loaded == null) {
throw new UnableToLoadFileException(path);
Expand Down

0 comments on commit ac44b28

Please sign in to comment.