mirror of
https://github.com/magefree/mage.git
synced 2025-12-28 14:32:06 -08:00
* fixed https://sonarcloud.io/project/issues?id=org.xmage%3Amage-root&issues=AWIlv32RgrzAwlaaQ7rP&open=AWIlv32RgrzAwlaaQ7rP * ensure closing of scanner if it was opened * Refactored method in EmpyrialArchAngel to not always return same value. * Refactored method in FalkenrathAristocrat to not always return same value. * Refactored method in GilderBairn to not always return the same value. * fixed left open resources, ensured quiet closing of the streams * Refactored method in IceCave to not always return same value. * Refactored method in KjeldoranRoyalGuard to not always return same value. * Refactored method in LegionsInitiative to not always return same value. * Refactored method in NaturesWill to not always return same value. * added quiet closing method in new streamutils class, used to clean up the connectdialog * Fix small typo * added quiet closing to saveobjectutil * closed resources in savegame method of gamecontroller * properly close resources in loadGame method of GameReplay class * further proper resource closing in ServerMessagesUtil * fixed unclosed resources in copy method in mage framework Copier * closed unclosed resources in copyCompressed method in Copier * ensure closing of filewriter in manasymbols * ensure proper closing of Stream in arcane UI * ensure closing of datagram socket in arcane Util * ensure resource closing in deckimport from clipboard * ensure closing of plugin classloader * ensured closing of zipinputstream resource * ensure closing of fileoutputstream in ScryfallSymbolsSource * ensure closing resources after finishing/canceling download of pictures * remove commented code * move locks to try block to ensure unlocking along all execution paths * remove dangerous instance of double-checked locking * removed dangerous instance of double checked locking in settingsmanager * Removed dangerous instance of double-checked locking in ThemePluginImpl * close resource which did not happen certainly * close another stream * ensure closing of inputstream
123 lines
3.4 KiB
Java
123 lines
3.4 KiB
Java
package com.magefree.update.helpers;
|
|
|
|
import java.io.*;
|
|
import java.net.HttpURLConnection;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.io.Closeable;
|
|
/**
|
|
* Helper for file operations.
|
|
*
|
|
* @author noxx
|
|
*/
|
|
public final class FileHelper {
|
|
|
|
private FileHelper() {
|
|
}
|
|
|
|
/**
|
|
* Filters out dirs.
|
|
*/
|
|
private static final FileFilter anyFileFilter = f -> f.isFile();
|
|
|
|
/**
|
|
* Filters out jars.
|
|
*/
|
|
private static final FilenameFilter jarFileFilter = (dir, name) -> name.endsWith(".jar");
|
|
|
|
/**
|
|
* Gets .jar files from specified folder.
|
|
*
|
|
* @param dir Folder to scan for rile
|
|
* @return
|
|
*/
|
|
public static List<File> findJarsInDir(String dir) {
|
|
ArrayList<File> result = new ArrayList<>();
|
|
File directory = new File(dir);
|
|
if (directory.exists() && directory.isDirectory()) {
|
|
for (File jar : directory.listFiles(jarFileFilter)) {
|
|
result.add(jar);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Gets non-dir files from specified folder.
|
|
*
|
|
* @param dir Folder to scan for rile
|
|
* @return
|
|
*/
|
|
public static List<File> findAllFilesInDir(String dir) {
|
|
ArrayList<File> result = new ArrayList<>();
|
|
File directory = new File(dir);
|
|
if (directory.exists() && directory.isDirectory()) {
|
|
for (File jar : directory.listFiles(anyFileFilter)) {
|
|
result.add(jar);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Removes all files from the list.
|
|
*
|
|
* @param files
|
|
*/
|
|
public static void removeFiles(List<String> files) {
|
|
for (String filename : files) {
|
|
File f = new File(filename);
|
|
if (f.exists()) {
|
|
f.delete();
|
|
System.out.println("File has been deleted: " + filename);
|
|
} else {
|
|
System.out.println("ERROR. Couldn't find file to delete: " + filename);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Downloads specified file.
|
|
*
|
|
* @param filename
|
|
* @param urlConnection
|
|
*/
|
|
public static void downloadFile(String filename, HttpURLConnection urlConnection) {
|
|
System.out.println("Downloading " + filename);
|
|
InputStream in = null;
|
|
FileOutputStream out = null;
|
|
try {
|
|
in = urlConnection.getInputStream();
|
|
File f = new File(filename);
|
|
if (!f.exists() && f.getParentFile() != null) {
|
|
f.getParentFile().mkdirs();
|
|
System.out.println("Directories have been created: " + f.getParentFile().getPath());
|
|
}
|
|
|
|
out = new FileOutputStream(filename);
|
|
byte[] buf = new byte[4 * 1024];
|
|
int bytesRead;
|
|
|
|
while ((bytesRead = in.read(buf)) != -1) {
|
|
out.write(buf, 0, bytesRead);
|
|
}
|
|
|
|
System.out.println("File has been updated: " + filename);
|
|
} catch (IOException e) {
|
|
System.out.println("i/o exception - " + e.getMessage());
|
|
} finally {
|
|
closeQuietly(in);
|
|
closeQuietly(out);
|
|
}
|
|
}
|
|
|
|
public static void closeQuietly(Closeable s) {
|
|
if(s != null) {
|
|
try {
|
|
s.close();
|
|
} catch (Exception e) {
|
|
System.out.println("i/o exception - " + e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|