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

@ -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);
try (FileOutputStream out = new FileOutputStream(updateFileName)) {
out.write(response.bodyAsBytes()); out.write(response.bodyAsBytes());
out.close(); }
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(() -> {