Merge pull request #631 from Gaboso/master

Added try-with-resources blocks
This commit is contained in:
Kevin Jiang 2018-05-29 18:46:22 -04:00 committed by GitHub
commit d58a80b5e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 20 deletions

View File

@ -35,7 +35,7 @@ import com.rarchives.ripme.utils.Utils;
* Entry point to application. * Entry point to application.
* This is where all the fun happens, with the main method. * This is where all the fun happens, with the main method.
* Decides to display UI or to run silently via command-line. * Decides to display UI or to run silently via command-line.
* *
* As the "controller" to all other classes, it parses command line parameters and loads the history. * As the "controller" to all other classes, it parses command line parameters and loads the history.
*/ */
public class App { public class App {
@ -46,7 +46,7 @@ public class App {
/** /**
* Where everything starts. Takes in, and tries to parse as many commandline arguments as possible. * Where everything starts. Takes in, and tries to parse as many commandline arguments as possible.
* Otherwise, it launches a GUI. * Otherwise, it launches a GUI.
* *
* @param args Array of command line arguments. * @param args Array of command line arguments.
*/ */
public static void main(String[] args) throws MalformedURLException { public static void main(String[] args) throws MalformedURLException {
@ -84,7 +84,7 @@ public class App {
* Creates an abstract ripper and instructs it to rip. * Creates an abstract ripper and instructs it to rip.
* @param url URL to be ripped * @param url URL to be ripped
* @throws Exception Nothing too specific here, just a catch-all. * @throws Exception Nothing too specific here, just a catch-all.
* *
*/ */
private static void rip(URL url) throws Exception { private static void rip(URL url) throws Exception {
AbstractRipper ripper = AbstractRipper.getRipper(url); AbstractRipper ripper = AbstractRipper.getRipper(url);
@ -217,9 +217,9 @@ public class App {
//Read URLs from File //Read URLs from File
if (cl.hasOption('f')) { if (cl.hasOption('f')) {
String filename = cl.getOptionValue('f'); String filename = cl.getOptionValue('f');
try {
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String url; String url;
BufferedReader br = new BufferedReader(new FileReader(filename));
while ((url = br.readLine()) != null) { while ((url = br.readLine()) != null) {
if (url.startsWith("//") || url.startsWith("#")) { if (url.startsWith("//") || url.startsWith("#")) {
logger.debug("Skipping over line \"" + url + "\"because it is a comment"); logger.debug("Skipping over line \"" + url + "\"because it is a comment");

View File

@ -129,8 +129,8 @@ public abstract class AbstractRipper
private boolean hasDownloadedURL(String url) { private boolean hasDownloadedURL(String url) {
File file = new File(URLHistoryFile); File file = new File(URLHistoryFile);
url = normalizeUrl(url); url = normalizeUrl(url);
try {
Scanner scanner = new Scanner(file); try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) { while (scanner.hasNextLine()) {
final String lineFromFile = scanner.nextLine(); final String lineFromFile = scanner.nextLine();
if (lineFromFile.equals(url)) { if (lineFromFile.equals(url)) {
@ -140,6 +140,7 @@ public abstract class AbstractRipper
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
return false; return false;
} }
return false; return false;
} }

View File

@ -68,11 +68,10 @@ public abstract class AlbumRipper extends AbstractRipper {
if (Utils.getConfigBoolean("urls_only.save", false)) { if (Utils.getConfigBoolean("urls_only.save", false)) {
// Output URL to file // Output URL to file
String urlFile = this.workingDir + File.separator + "urls.txt"; String urlFile = this.workingDir + File.separator + "urls.txt";
try { try (FileWriter fw = new FileWriter(urlFile, true)) {
FileWriter fw = new FileWriter(urlFile, true);
fw.write(url.toExternalForm()); fw.write(url.toExternalForm());
fw.write("\n"); fw.write("\n");
fw.close();
RipStatusMessage msg = new RipStatusMessage(STATUS.DOWNLOAD_COMPLETE, urlFile); RipStatusMessage msg = new RipStatusMessage(STATUS.DOWNLOAD_COMPLETE, urlFile);
itemsCompleted.put(url, new File(urlFile)); itemsCompleted.put(url, new File(urlFile));
observer.update(this, msg); observer.update(this, msg);

View File

@ -45,11 +45,11 @@ public abstract class VideoRipper extends AbstractRipper {
if (Utils.getConfigBoolean("urls_only.save", false)) { if (Utils.getConfigBoolean("urls_only.save", false)) {
// Output URL to file // Output URL to file
String urlFile = this.workingDir + File.separator + "urls.txt"; String urlFile = this.workingDir + File.separator + "urls.txt";
try {
FileWriter fw = new FileWriter(urlFile, true); try (FileWriter fw = new FileWriter(urlFile, true)) {
fw.write(url.toExternalForm()); fw.write(url.toExternalForm());
fw.write("\n"); fw.write("\n");
fw.close();
RipStatusMessage msg = new RipStatusMessage(STATUS.DOWNLOAD_COMPLETE, urlFile); RipStatusMessage msg = new RipStatusMessage(STATUS.DOWNLOAD_COMPLETE, urlFile);
observer.update(this, msg); observer.update(this, msg);
} catch (IOException e) { } catch (IOException e) {

View File

@ -149,9 +149,11 @@ public class UpdateUtils {
.timeout(Utils.getConfigInteger("download.timeout", 60 * 1000)) .timeout(Utils.getConfigInteger("download.timeout", 60 * 1000))
.maxBodySize(1024 * 1024 * 100) .maxBodySize(1024 * 1024 * 100)
.execute(); .execute();
FileOutputStream out = new FileOutputStream(updateFileName);
out.write(response.bodyAsBytes()); try (FileOutputStream out = new FileOutputStream(updateFileName)) {
out.close(); out.write(response.bodyAsBytes());
}
logger.info("Download of new version complete; saved to " + updateFileName); logger.info("Download of new version complete; saved to " + updateFileName);
// Setup updater script // Setup updater script
@ -185,11 +187,13 @@ public class UpdateUtils {
+ "rm -f " + batchPath + "\n"; + "rm -f " + batchPath + "\n";
batchExec = new String[] { "sh", batchPath }; batchExec = new String[] { "sh", batchPath };
} }
// Create updater script // Create updater script
BufferedWriter bw = new BufferedWriter(new FileWriter(batchFile)); try (BufferedWriter bw = new BufferedWriter(new FileWriter(batchFile))) {
bw.write(script); bw.write(script);
bw.flush(); bw.flush();
bw.close(); }
logger.info("Saved update script to " + batchFile); logger.info("Saved update script to " + batchFile);
// Run updater script on exit // Run updater script on exit
Runtime.getRuntime().addShutdownHook(new Thread(() -> { Runtime.getRuntime().addShutdownHook(new Thread(() -> {