From d7374aa73fe332b214d9491ed1d69d30b3c3758b Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Thu, 21 Mar 2024 15:20:58 +0000 Subject: [PATCH] Return a separate `Closeable` implementation when appropriate. Avoids incorrect resource leak warnings when using `ofContent()`. These started appearing after the latest Eclipse update. --- .../com/sshtools/tinytemplate/Templates.java | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/sshtools/tinytemplate/Templates.java b/src/main/java/com/sshtools/tinytemplate/Templates.java index a023547..0af25c4 100644 --- a/src/main/java/com/sshtools/tinytemplate/Templates.java +++ b/src/main/java/com/sshtools/tinytemplate/Templates.java @@ -344,8 +344,24 @@ public final static Boolean evalAsCondition(Object val) { public final static Logger defaultStdOutLogger() { return LazyDefaultStdOutLogger.DEFAULT; } + + public final static class CloseableTemplateModel extends TemplateModel implements Closeable { + + public CloseableTemplateModel(Reader reader) { + super(reader); + } + + @Override + public void close() { + try { + xtext.close(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + } - public final static class TemplateModel implements Closeable { + public static class TemplateModel { public static TemplateModel ofPath(Path path) { try { @@ -364,20 +380,24 @@ public static TemplateModel ofContent(String content) { return new TemplateModel(new StringReader(content)); } - public static TemplateModel ofResource(String resource) { + public static CloseableTemplateModel ofReader(Reader resource) { + return new CloseableTemplateModel(resource); + } + + public static CloseableTemplateModel ofResource(String resource) { return ofResource(resource, Optional.empty()); } - public static TemplateModel ofResource(String resource, ClassLoader loader) { + public static CloseableTemplateModel ofResource(String resource, ClassLoader loader) { return ofResource(resource, Optional.of(loader)); } - public static TemplateModel ofResource(Class packageClass, String childResource) { + public static CloseableTemplateModel ofResource(Class packageClass, String childResource) { return ofResource(packageClass.getPackage().getName().replace('.', '/') + "/" + childResource, Optional.of(packageClass.getClassLoader())); } - public static TemplateModel ofResource(String resource, Optional loader) { + public static CloseableTemplateModel ofResource(String resource, Optional loader) { // Read resource content var ldr = loader.orElseGet(() -> Templates.class.getClassLoader()); try { @@ -387,7 +407,7 @@ public static TemplateModel ofResource(String resource, Optional lo throw new FileNotFoundException(resource); } var in = res.openStream(); - return new TemplateModel(new InputStreamReader(in)); + return new CloseableTemplateModel(new InputStreamReader(in)); } catch (IOException e) { throw new UncheckedIOException(e); } @@ -405,7 +425,8 @@ public static TemplateModel ofResource(String resource, Optional lo Optional> locale = Optional.empty(); Optional parent = Optional.empty(); - private final Reader xtext; + protected final Reader xtext; + private StringBuilder buffer = null; private Optional> instruction = Optional.empty(); @@ -459,15 +480,6 @@ public void close() throws IOException { } } - @Override - public void close() { - try { - xtext.close(); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - public Locale locale() { return locale.map(l -> l.get()).orElseGet(() -> Locale.getDefault()); }