From 3380cd5d5052c50aacbdc3b767c3ed861a3ea81f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 12 Feb 2020 21:40:15 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Use=20try-with-resources=20instea?= =?utf8?q?d=20of=20Closer?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/core/SoneDownloaderImpl.java | 11 +++----- .../net/pterodactylus/sone/core/SoneInserter.java | 29 ++++++---------------- .../pterodactylus/sone/main/DefaultLoaders.java | 19 ++++---------- 3 files changed, 16 insertions(+), 43 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java index 0538c8b..43a87fb 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java @@ -17,7 +17,6 @@ package net.pterodactylus.sone.core; -import static freenet.support.io.Closer.close; import static java.lang.String.format; import static java.lang.System.currentTimeMillis; import static java.util.concurrent.TimeUnit.DAYS; @@ -185,11 +184,8 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade private Sone parseSone(Sone originalSone, FetchResult fetchResult, FreenetURI requestUri) { logger.finest(() -> format("Parsing FetchResult (%d bytes, %s) for %s…", fetchResult.size(), fetchResult.getMimeType(), originalSone)); Bucket soneBucket = fetchResult.asBucket(); - InputStream soneInputStream = null; - try { - soneInputStream = soneBucket.getInputStream(); - Sone parsedSone = soneParser.parseSone(originalSone, - soneInputStream); + try (InputStream soneInputStream = soneBucket.getInputStream()) { + Sone parsedSone = soneParser.parseSone(originalSone, soneInputStream); if (parsedSone != null) { logger.finer(() -> format("Sone %s was successfully parsed.", parsedSone)); parsedSone.setLatestEdition(requestUri.getEdition()); @@ -198,8 +194,7 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade } catch (Exception e1) { logger.log(Level.WARNING, e1, () -> format("Could not parse Sone from %s!", requestUri)); } finally { - close(soneInputStream); - close(soneBucket); + soneBucket.free(); } return null; } diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index ba13305..69480da 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -23,10 +23,7 @@ import static java.util.concurrent.TimeUnit.*; import static java.util.logging.Logger.getLogger; import static net.pterodactylus.sone.data.Album.NOT_EMPTY; -import java.io.Closeable; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.StringWriter; +import java.io.*; import java.nio.charset.Charset; import java.util.HashMap; import java.util.HashSet; @@ -49,7 +46,6 @@ import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.SoneStatus; import net.pterodactylus.sone.main.SonePlugin; -import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.service.AbstractService; import net.pterodactylus.util.template.HtmlFilter; import net.pterodactylus.util.template.ReflectionAccessor; @@ -378,19 +374,13 @@ public class SoneInserter extends AbstractService { } public ManifestElement createManifestElement(String name, String contentType, String templateName) { - InputStreamReader templateInputStreamReader = null; - InputStream templateInputStream = null; Template template; - try { - templateInputStream = getClass().getResourceAsStream(templateName); - templateInputStreamReader = new InputStreamReader(templateInputStream, utf8Charset); + try (InputStream templateInputStream = getClass().getResourceAsStream(templateName); + InputStreamReader 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); + } catch (IOException | TemplateException e1) { + logger.log(Level.SEVERE, String.format("Could not parse template “%s”!", templateName), e1); return null; - } finally { - Closer.close(templateInputStreamReader); - Closer.close(templateInputStream); } TemplateContext templateContext = templateContextFactory.createTemplateContext(); @@ -398,17 +388,14 @@ public class SoneInserter extends AbstractService { templateContext.set("currentSone", soneProperties); templateContext.set("currentEdition", core.getUpdateChecker().getLatestEdition()); templateContext.set("version", SonePlugin.getPluginVersion()); - StringWriter writer = new StringWriter(); - try { + try (StringWriter writer = new StringWriter()) { template.render(templateContext, writer); RandomAccessBucket bucket = new ArrayBucket(writer.toString().getBytes(Charsets.UTF_8)); buckets.add(bucket); return new ManifestElement(name, bucket, contentType, bucket.size()); - } catch (TemplateException te1) { - logger.log(Level.SEVERE, String.format("Could not render template “%s”!", templateName), te1); + } catch (IOException | TemplateException e1) { + logger.log(Level.SEVERE, String.format("Could not render template “%s”!", templateName), e1); return null; - } finally { - Closer.close(writer); } } diff --git a/src/main/java/net/pterodactylus/sone/main/DefaultLoaders.java b/src/main/java/net/pterodactylus/sone/main/DefaultLoaders.java index 2a2bd8c..5072970 100644 --- a/src/main/java/net/pterodactylus/sone/main/DefaultLoaders.java +++ b/src/main/java/net/pterodactylus/sone/main/DefaultLoaders.java @@ -1,13 +1,10 @@ package net.pterodactylus.sone.main; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.io.UnsupportedEncodingException; +import java.io.*; + import javax.annotation.Nonnull; import net.pterodactylus.sone.web.WebInterface; -import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.template.ClassPathTemplateProvider; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateProvider; @@ -25,17 +22,11 @@ public class DefaultLoaders implements Loaders { @Nonnull @Override public Template loadTemplate(@Nonnull String path) { - InputStream templateInputStream = null; - Reader reader = null; - try { - templateInputStream = getClass().getResourceAsStream(path); - reader = new InputStreamReader(templateInputStream, "UTF-8"); + try (InputStream templateInputStream = getClass().getResourceAsStream(path); + Reader reader = new InputStreamReader(templateInputStream, "UTF-8");) { return parse(reader); - } catch (UnsupportedEncodingException uee1) { + } catch (IOException ioe1) { throw new RuntimeException("UTF-8 not supported."); - } finally { - Closer.close(reader); - Closer.close(templateInputStream); } } -- 2.7.4