From a59313df499a77b7ed2c759b4e23478e36d4d750 Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Sat, 28 Dec 2019 15:38:33 +0100 Subject: [PATCH] Code cleanup --- common/src/main/java/ctbrec/Config.java | 26 ++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/common/src/main/java/ctbrec/Config.java b/common/src/main/java/ctbrec/Config.java index ad00b3df..7ae551f6 100644 --- a/common/src/main/java/ctbrec/Config.java +++ b/common/src/main/java/ctbrec/Config.java @@ -3,8 +3,8 @@ package ctbrec; import static java.nio.file.StandardOpenOption.*; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.text.SimpleDateFormat; @@ -28,7 +28,7 @@ import ctbrec.sites.Site; public class Config { - private static final transient Logger LOG = LoggerFactory.getLogger(Config.class); + private static final Logger LOG = LoggerFactory.getLogger(Config.class); private static Config instance; private Settings settings; @@ -37,7 +37,7 @@ public class Config { private File configDir; public static final String RECORDING_DATE_FORMAT = "yyyy-MM-dd_HH-mm-ss_SSS"; - private Config(List sites) throws FileNotFoundException, IOException { + private Config(List sites) { this.sites = sites; if(System.getProperty("ctbrec.config.dir") != null) { configDir = new File(System.getProperty("ctbrec.config.dir")); @@ -52,14 +52,14 @@ public class Config { } } - private void load() throws FileNotFoundException, IOException { + private void load() throws IOException { Moshi moshi = new Moshi.Builder() .add(Model.class, new ModelJsonAdapter(sites)) .build(); JsonAdapter adapter = moshi.adapter(Settings.class).lenient(); File configFile = new File(configDir, filename); LOG.debug("Loading config from {}", configFile.getAbsolutePath()); - if(configFile.exists()) { + if (configFile.exists()) { try { byte[] fileContent = Files.readAllBytes(configFile.toPath()); if (fileContent[0] == -17 && fileContent[1] == -69 && fileContent[2] == -65) { @@ -69,13 +69,13 @@ public class Config { fileContent[1] = ' '; fileContent[2] = ' '; } - String json = new String(fileContent, "UTF-8").trim(); + String json = new String(fileContent, StandardCharsets.UTF_8.name()).trim(); settings = adapter.fromJson(json); settings.httpTimeout = Math.max(settings.httpTimeout, 10_000); if (settings.recordingsDir.endsWith("/")) { settings.recordingsDir = settings.recordingsDir.substring(0, settings.recordingsDir.length() - 1); } - } catch(Throwable e) { + } catch (Exception e) { settings = OS.getDefaultSettings(); for (Site site : sites) { site.setEnabled(!settings.disabledSites.contains(site.getName())); @@ -99,20 +99,20 @@ public class Config { String backup = source.getName() + '.' + timestamp; File target = new File(source.getParentFile(), backup); Files.copy(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); - } catch(Throwable e) { + } catch (Exception e) { LOG.error("Couldn't create backup of settings file", e); } } - public static synchronized void init(List sites) throws FileNotFoundException, IOException { - if(instance == null) { + public static synchronized void init(List sites) throws IOException { + if (instance == null) { instance = new Config(sites); instance.load(); } } public static synchronized Config getInstance() { - if(instance == null) { + if (instance == null) { throw new IllegalStateException("Config not initialized, call init() first"); } return instance; @@ -156,7 +156,7 @@ public class Config { } private File getDirForRecording(Model model, String formattedDate) { - switch(getSettings().recordingsDirStructure) { + switch (getSettings().recordingsDirStructure) { case ONE_PER_MODEL: return new File(getSettings().recordingsDir, model.getSanitizedNamed()); case ONE_PER_RECORDING: @@ -170,7 +170,7 @@ public class Config { public String getServerUrl() { String scheme = getSettings().transportLayerSecurity ? "https" : "http"; - //int port = getSettings().transportLayerSecurity ? getSettings().httpSecurePort : getSettings().httpPort; + // int port = getSettings().transportLayerSecurity ? getSettings().httpSecurePort : getSettings().httpPort; int port = getSettings().httpPort; String baseUrl = scheme + "://" + getSettings().httpServer + ":" + port + getContextPath(); return baseUrl;