From 7689eb675960754cdcc6dfd08167e6086a677f65 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 26 Jun 2014 06:59:02 +0200 Subject: [PATCH] Close all input streams after parsing templates. --- .../net/pterodactylus/sone/core/SoneInserter.java | 6 +++++- .../net/pterodactylus/sone/web/WebInterface.java | 24 +++++++--------------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index 7001be1..76903f0 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -21,6 +21,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static java.lang.System.currentTimeMillis; import static net.pterodactylus.sone.data.Album.NOT_EMPTY; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringWriter; import java.nio.charset.Charset; @@ -342,15 +343,18 @@ public class SoneInserter extends AbstractService { @SuppressWarnings("synthetic-access") private ManifestElement createManifestElement(String name, String contentType, String templateName) { InputStreamReader templateInputStreamReader = null; + InputStream templateInputStream = null; Template template; try { - templateInputStreamReader = new InputStreamReader(getClass().getResourceAsStream(templateName), utf8Charset); + templateInputStream = getClass().getResourceAsStream(templateName); + templateInputStreamReader = new InputStreamReader(templateInputStream, utf8Charset); template = TemplateParser.parse(templateInputStreamReader); } catch (TemplateException te1) { logger.log(Level.SEVERE, String.format("Could not parse template “%s”!", templateName), te1); return null; } finally { Closer.close(templateInputStreamReader); + Closer.close(templateInputStream); } TemplateContext templateContext = templateContextFactory.createTemplateContext(); diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index f21d256..8478976 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -324,11 +324,17 @@ public class WebInterface { } private Template parseTemplate(String resourceName) { - Reader reader = createReader(resourceName); + InputStream templateInputStream = null; + Reader reader = null; try { + templateInputStream = getClass().getResourceAsStream(resourceName); + reader = new InputStreamReader(templateInputStream, "UTF-8"); return parse(reader); + } catch (UnsupportedEncodingException uee1) { + throw new RuntimeException("UTF-8 not supported."); } finally { Closer.close(reader); + Closer.close(templateInputStream); } } @@ -747,22 +753,6 @@ public class WebInterface { } /** - * Creates a {@link Reader} from the {@link InputStream} for the resource - * with the given name. - * - * @param resourceName - * The name of the resource - * @return A {@link Reader} for the resource - */ - private Reader createReader(String resourceName) { - try { - return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8"); - } catch (UnsupportedEncodingException uee1) { - return null; - } - } - - /** * Returns all {@link Sone#isLocal() local Sone}s that are referenced by * {@link SonePart}s in the given text (after parsing it using * {@link SoneTextParser}). -- 2.7.4