From 9a71a0c6c914b15183b53b94e165aa4b63f8330e Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 1 Dec 2014 16:02:42 +0100 Subject: [PATCH] YamlProcessor defensively closes InputStream Issue: SPR-12487 --- .../beans/factory/config/YamlProcessor.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/YamlProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/YamlProcessor.java index 9e0e58ba36..1b3d2cb201 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/YamlProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/YamlProcessor.java @@ -17,6 +17,7 @@ package org.springframework.beans.factory.config; import java.io.IOException; +import java.io.InputStream; import java.util.AbstractMap; import java.util.Arrays; import java.util.Collection; @@ -146,17 +147,23 @@ public abstract class YamlProcessor { if (this.logger.isDebugEnabled()) { this.logger.debug("Loading from YAML: " + resource); } - for (Object object : yaml.loadAll(resource.getInputStream())) { - if (object != null && process(asMap(object), callback)) { - count++; - if (this.resolutionMethod == ResolutionMethod.FIRST_FOUND) { - break; + InputStream stream = resource.getInputStream(); + try { + for (Object object : yaml.loadAll(stream)) { + if (object != null && process(asMap(object), callback)) { + count++; + if (this.resolutionMethod == ResolutionMethod.FIRST_FOUND) { + break; + } } } + if (this.logger.isDebugEnabled()) { + this.logger.debug("Loaded " + count + " document" + (count > 1 ? "s" : "") + + " from YAML resource: " + resource); + } } - if (this.logger.isDebugEnabled()) { - this.logger.debug("Loaded " + count + " document" + (count > 1 ? "s" : "") + - " from YAML resource: " + resource); + finally { + stream.close(); } } catch (IOException ex) {