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;
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());
} 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;
}
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;
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;
}
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();
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);
}
}
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;
@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);
}
}