🎨 Use try-with-resources instead of Closer
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 12 Feb 2020 20:40:15 +0000 (21:40 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 12 Feb 2020 20:40:15 +0000 (21:40 +0100)
src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java
src/main/java/net/pterodactylus/sone/core/SoneInserter.java
src/main/java/net/pterodactylus/sone/main/DefaultLoaders.java

index 0538c8b..43a87fb 100644 (file)
@@ -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;
        }
index ba13305..69480da 100644 (file)
@@ -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);
                        }
                }
 
index 2a2bd8c..5072970 100644 (file)
@@ -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);
                }
        }