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.
* This is where all the fun happens, with the main method.
* 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.
*/
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.
* Otherwise, it launches a GUI.
*
*
* @param args Array of command line arguments.
*/
public static void main(String[] args) throws MalformedURLException {
@ -84,7 +84,7 @@ public class App {
* Creates an abstract ripper and instructs it to rip.
* @param url URL to be ripped
* @throws Exception Nothing too specific here, just a catch-all.
*
*
*/
private static void rip(URL url) throws Exception {
AbstractRipper ripper = AbstractRipper.getRipper(url);
@ -217,9 +217,9 @@ public class App {
//Read URLs from File
if (cl.hasOption('f')) {
String filename = cl.getOptionValue('f');
try {
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String url;
BufferedReader br = new BufferedReader(new FileReader(filename));
while ((url = br.readLine()) != null) {
if (url.startsWith("//") || url.startsWith("#")) {
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) {
File file = new File(URLHistoryFile);
url = normalizeUrl(url);
try {
Scanner scanner = new Scanner(file);
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
final String lineFromFile = scanner.nextLine();
if (lineFromFile.equals(url)) {
@ -140,6 +140,7 @@ public abstract class AbstractRipper
} catch (FileNotFoundException e) {
return false;
}
return false;
}

View File

@ -68,11 +68,10 @@ public abstract class AlbumRipper extends AbstractRipper {
if (Utils.getConfigBoolean("urls_only.save", false)) {
// Output URL to file
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("\n");
fw.close();
RipStatusMessage msg = new RipStatusMessage(STATUS.DOWNLOAD_COMPLETE, urlFile);
itemsCompleted.put(url, new File(urlFile));
observer.update(this, msg);

View File

@ -45,11 +45,11 @@ public abstract class VideoRipper extends AbstractRipper {
if (Utils.getConfigBoolean("urls_only.save", false)) {
// Output URL to file
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("\n");
fw.close();
RipStatusMessage msg = new RipStatusMessage(STATUS.DOWNLOAD_COMPLETE, urlFile);
observer.update(this, msg);
} catch (IOException e) {

View File

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