From 5c9c13abcd709cc4936728f94b10fb49882c9087 Mon Sep 17 00:00:00 2001 From: soloturn Date: Sat, 2 Apr 2022 08:33:06 +0200 Subject: [PATCH] remove warnings in AbstractRipper AbstractHTMLRipper DownloadFileThread Utils --- .../ripme/ripper/AbstractHTMLRipper.java | 16 ++---- .../ripme/ripper/AbstractRipper.java | 10 +--- .../ripme/ripper/DownloadFileThread.java | 46 ++++++----------- .../java/com/rarchives/ripme/utils/Utils.java | 51 +++++++------------ 4 files changed, 40 insertions(+), 83 deletions(-) diff --git a/src/main/java/com/rarchives/ripme/ripper/AbstractHTMLRipper.java b/src/main/java/com/rarchives/ripme/ripper/AbstractHTMLRipper.java index 861410b7..45378bfa 100644 --- a/src/main/java/com/rarchives/ripme/ripper/AbstractHTMLRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/AbstractHTMLRipper.java @@ -309,7 +309,7 @@ public abstract class AbstractHTMLRipper extends AbstractRipper { */ public boolean addURLToDownload(URL url, Path saveAs, String referrer, Map cookies, Boolean getFileExtFromMIME) { // Only download one file if this is a test. - if (super.isThisATest() && (itemsCompleted.size() > 0 || itemsErrored.size() > 0)) { + if (isThisATest() && (itemsCompleted.size() > 0 || itemsErrored.size() > 0)) { stop(); itemsPending.clear(); return false; @@ -388,7 +388,7 @@ public abstract class AbstractHTMLRipper extends AbstractRipper { } @Override - /** + /* * Cleans up & tells user about failed download. */ public void downloadErrored(URL url, String reason) { @@ -436,8 +436,6 @@ public abstract class AbstractHTMLRipper extends AbstractRipper { * Sets directory to save all ripped files to. * @param url * URL to define how the working directory should be saved. - * @throws - * IOException */ @Override public void setWorkingDir(URL url) throws IOException { @@ -447,12 +445,7 @@ public abstract class AbstractHTMLRipper extends AbstractRipper { if (!path.endsWith(File.separator)) { path += File.separator; } - String title; - if (Utils.getConfigBoolean("album_titles.save", true)) { - title = getAlbumTitle(this.url); - } else { - title = super.getAlbumTitle(this.url); - } + String title = getAlbumTitle(this.url); LOGGER.debug("Using album title '" + title + "'"); title = Utils.filesystemSafe(title); @@ -485,12 +478,11 @@ public abstract class AbstractHTMLRipper extends AbstractRipper { */ @Override public String getStatusText() { - String sb = getCompletionPercentage() + + return getCompletionPercentage() + "% " + "- Pending: " + itemsPending.size() + ", Completed: " + itemsCompleted.size() + ", Errored: " + itemsErrored.size(); - return sb; } diff --git a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java index 407aa33a..1384b8f1 100644 --- a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java @@ -243,7 +243,7 @@ public abstract class AbstractRipper */ protected boolean addURLToDownload(URL url, Map options, Map cookies) { // Bit of a hack but this lets us pass a bool using a map - boolean useMIME = options.getOrDefault("getFileExtFromMIME", "false").toLowerCase().equals("true"); + boolean useMIME = options.getOrDefault("getFileExtFromMIME", "false").equalsIgnoreCase("true"); return addURLToDownload(url, options.getOrDefault("prefix", ""), options.getOrDefault("subdirectory", ""), options.getOrDefault("referrer", null), cookies, options.getOrDefault("fileName", null), options.getOrDefault("extension", null), useMIME); } @@ -455,15 +455,11 @@ public abstract class AbstractRipper public abstract void downloadCompleted(URL url, Path saveAs); /** * Notifies observers that a file could not be downloaded (includes a reason). - * @param url - * @param reason */ public abstract void downloadErrored(URL url, String reason); /** * Notify observers that a download could not be completed, * but was not technically an "error". - * @param url - * @param file */ public abstract void downloadExists(URL url, Path file); @@ -581,7 +577,6 @@ public abstract class AbstractRipper * The package name. * @return * List of constructors for all eligible Rippers. - * @throws Exception */ public static List> getRipperConstructors(String pkg) throws Exception { List> constructors = new ArrayList<>(); @@ -595,8 +590,7 @@ public abstract class AbstractRipper /** * Sends an update message to the relevant observer(s) on this ripper. - * @param status - * @param message + * @param status */ public void sendUpdate(STATUS status, Object message) { if (observer == null) { diff --git a/src/main/java/com/rarchives/ripme/ripper/DownloadFileThread.java b/src/main/java/com/rarchives/ripme/ripper/DownloadFileThread.java index 435523d8..562ac366 100644 --- a/src/main/java/com/rarchives/ripme/ripper/DownloadFileThread.java +++ b/src/main/java/com/rarchives/ripme/ripper/DownloadFileThread.java @@ -10,11 +10,9 @@ import java.nio.file.Paths; import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import java.util.ResourceBundle; import javax.net.ssl.HttpsURLConnection; -import com.rarchives.ripme.ui.MainWindow; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jsoup.HttpStatusException; @@ -32,12 +30,12 @@ class DownloadFileThread extends Thread { private String referrer = ""; private Map cookies = new HashMap<>(); - private URL url; + private final URL url; private File saveAs; - private String prettySaveAs; - private AbstractRipper observer; - private int retries; - private Boolean getFileExtFromMIME; + private final String prettySaveAs; + private final AbstractRipper observer; + private final int retries; + private final Boolean getFileExtFromMIME; private final int TIMEOUT; @@ -69,7 +67,7 @@ class DownloadFileThread extends Thread { saveAs = new File( saveAs.getParentFile().getAbsolutePath() + File.separator + Utils.sanitizeSaveAs(saveAs.getName())); long fileSize = 0; - int bytesTotal = 0; + int bytesTotal; int bytesDownloaded = 0; if (saveAs.exists() && observer.tryResumeDownload()) { fileSize = saveAs.length(); @@ -85,7 +83,7 @@ class DownloadFileThread extends Thread { && !observer.tryResumeDownload()) { if (Utils.getConfigBoolean("file.overwrite", false)) { logger.info("[!] " + Utils.getLocalizedString("deleting.existing.file") + prettySaveAs); - saveAs.delete(); + if (!saveAs.delete()) logger.error("could not delete existing file: " + saveAs.getAbsolutePath()); } else { logger.info("[!] " + Utils.getLocalizedString("skipping") + " " + url + " -- " + Utils.getLocalizedString("file.already.exists") + ": " + prettySaveAs); @@ -98,8 +96,6 @@ class DownloadFileThread extends Thread { int tries = 0; // Number of attempts to download do { tries += 1; - InputStream bis = null; - OutputStream fos = null; try { logger.info(" Downloading file: " + urlToDownload + (tries > 0 ? " Retry #" + tries : "")); observer.sendUpdate(STATUS.DOWNLOAD_STARTED, url.toExternalForm()); @@ -122,14 +118,14 @@ class DownloadFileThread extends Thread { huc.setRequestProperty("Referer", referrer); // Sic } huc.setRequestProperty("User-agent", AbstractRipper.USER_AGENT); - String cookie = ""; + StringBuilder cookie = new StringBuilder(); for (String key : cookies.keySet()) { - if (!cookie.equals("")) { - cookie += "; "; + if (!cookie.toString().equals("")) { + cookie.append("; "); } - cookie += key + "=" + cookies.get(key); + cookie.append(key).append("=").append(cookies.get(key)); } - huc.setRequestProperty("Cookie", cookie); + huc.setRequestProperty("Cookie", cookie.toString()); if (observer.tryResumeDownload()) { if (fileSize != 0) { huc.setRequestProperty("Range", "bytes=" + fileSize + "-"); @@ -187,6 +183,7 @@ class DownloadFileThread extends Thread { } // Save file + InputStream bis; bis = new BufferedInputStream(huc.getInputStream()); // Check if we should get the file ext from the MIME type @@ -212,6 +209,7 @@ class DownloadFileThread extends Thread { } } // If we're resuming a download we append data to the existing file + OutputStream fos = null; if (statusCode == 206) { fos = new FileOutputStream(saveAs, true); } else { @@ -240,7 +238,9 @@ class DownloadFileThread extends Thread { // not allow fos = Files.newOutputStream( Utils.shortenSaveAsWindows(saveAs.getParentFile().getPath(), saveAs.getName())); + assert fos != null: "After shortenSaveAsWindows: " + saveAs.getAbsolutePath(); } + assert fos != null: e.getStackTrace(); } } byte[] data = new byte[1024 * 256]; @@ -292,20 +292,6 @@ class DownloadFileThread extends Thread { Utils.getLocalizedString("failed.to.download") + " " + url.toExternalForm()); return; - }finally { - // Close any open streams - try { - if (bis != null) { - bis.close(); - } - } catch (IOException e) { - } - try { - if (fos != null) { - fos.close(); - } - } catch (IOException e) { - } } if (tries > this.retries) { logger.error("[!] " + Utils.getLocalizedString("exceeded.maximum.retries") + " (" + this.retries diff --git a/src/main/java/com/rarchives/ripme/utils/Utils.java b/src/main/java/com/rarchives/ripme/utils/Utils.java index 7323759e..f3b20a5f 100644 --- a/src/main/java/com/rarchives/ripme/utils/Utils.java +++ b/src/main/java/com/rarchives/ripme/utils/Utils.java @@ -20,13 +20,13 @@ import javax.sound.sampled.LineEvent; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.lang.reflect.Constructor; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.net.URLDecoder; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; @@ -62,7 +62,7 @@ public class Utils { private static final int SHORTENED_PATH_LENGTH = 12; private static PropertiesConfiguration config; - private static HashMap> cookieCache; + private static final HashMap> cookieCache; private static final HashMap magicHash = new HashMap<>(); private static ResourceBundle resourceBundle; @@ -256,11 +256,7 @@ public class Utils { */ private static boolean portableMode() { Path file = getJarDirectory().resolve(CONFIG_FILE); - if (Files.exists(file) && !Files.isDirectory(file)) { - return true; - } - - return false; + return Files.exists(file) && !Files.isDirectory(file); } /** @@ -326,8 +322,7 @@ public class Utils { * @return saveAs in relation to the CWD */ public static String removeCWD(Path saveAs) { - String prettySaveAs = saveAs.relativize(Paths.get(".").toAbsolutePath()).toString(); - return prettySaveAs; + return saveAs.relativize(Paths.get(".").toAbsolutePath()).toString(); } /** @@ -409,7 +404,7 @@ public class Utils { // Load from JAR try { String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", ""); - jarPath = URLDecoder.decode(jarPath, "UTF-8"); + jarPath = URLDecoder.decode(jarPath, StandardCharsets.UTF_8); JarFile jarFile = new JarFile(jarPath); Enumeration entries = jarFile.entries(); while (entries.hasMoreElements()) { @@ -662,18 +657,13 @@ public class Utils { String[] parts = query.split("&"); int pos; - try { - for (String part : parts) { - if ((pos = part.indexOf('=')) >= 0) { - res.put(URLDecoder.decode(part.substring(0, pos), "UTF-8"), - URLDecoder.decode(part.substring(pos + 1), "UTF-8")); - } else { - res.put(URLDecoder.decode(part, "UTF-8"), ""); - } + for (String part : parts) { + if ((pos = part.indexOf('=')) >= 0) { + res.put(URLDecoder.decode(part.substring(0, pos), StandardCharsets.UTF_8), + URLDecoder.decode(part.substring(pos + 1), StandardCharsets.UTF_8)); + } else { + res.put(URLDecoder.decode(part, StandardCharsets.UTF_8), ""); } - } catch (UnsupportedEncodingException e) { - // Shouldn't happen since UTF-8 is required to be supported - throw new RuntimeException(e); } return res; @@ -694,20 +684,15 @@ public class Utils { String[] parts = query.split("&"); int pos; - try { - for (String part : parts) { - if ((pos = part.indexOf('=')) >= 0) { - if (URLDecoder.decode(part.substring(0, pos), "UTF-8").equals(key)) { - return URLDecoder.decode(part.substring(pos + 1), "UTF-8"); - } - - } else if (URLDecoder.decode(part, "UTF-8").equals(key)) { - return ""; + for (String part : parts) { + if ((pos = part.indexOf('=')) >= 0) { + if (URLDecoder.decode(part.substring(0, pos), StandardCharsets.UTF_8).equals(key)) { + return URLDecoder.decode(part.substring(pos + 1), StandardCharsets.UTF_8); } + + } else if (URLDecoder.decode(part, StandardCharsets.UTF_8).equals(key)) { + return ""; } - } catch (UnsupportedEncodingException e) { - // Shouldn't happen since UTF-8 is required to be supported - throw new RuntimeException(e); } return null;