Cleanup: Use try-with and small fixes like NPE

Also make class final so that no class can extend it and start the thread
This commit is contained in:
Phoenix616 2023-03-01 17:33:34 +01:00
parent f0661656a0
commit d6673c8afd
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
1 changed files with 68 additions and 77 deletions

View File

@ -39,7 +39,7 @@ import java.util.zip.ZipFile;
* @version 2.1
*/
public class Updater {
public final class Updater {
private Plugin plugin;
private UpdateType type;
@ -204,7 +204,7 @@ public class Updater {
}
String key = this.config.getString("api-key");
if (key.equalsIgnoreCase("PUT_API_KEY_HERE") || key.equals("")) {
if (key != null && (key.equalsIgnoreCase("PUT_API_KEY_HERE") || key.equals(""))) {
key = null;
}
@ -305,14 +305,12 @@ public class Updater {
if (!folder.exists()) {
folder.mkdir();
}
BufferedInputStream in = null;
FileOutputStream fout = null;
try {
// Download the file
final URL url = new URL(link);
final int fileLength = url.openConnection().getContentLength();
in = new BufferedInputStream(url.openStream());
fout = new FileOutputStream(folder.getAbsolutePath() + File.separator + file);
try (BufferedInputStream in = new BufferedInputStream(url.openStream());
FileOutputStream fout = new FileOutputStream(folder.getAbsolutePath() + File.separator + file)) {
final byte[] data = new byte[Updater.BYTE_SIZE];
int count;
@ -329,11 +327,14 @@ public class Updater {
}
}
//Just a quick check to make sure we didn't leave any files from last time...
for (final File xFile : new File(this.plugin.getDataFolder().getParent(), this.updateFolder).listFiles()) {
File[] files = new File(this.plugin.getDataFolder().getParent(), this.updateFolder).listFiles();
if (files != null) {
for (final File xFile : files) {
if (xFile.getName().endsWith(".zip")) {
xFile.delete();
}
}
}
// Check to see if it's a zip file, if it is, unzip it.
final File dFile = new File(folder.getAbsolutePath() + File.separator + file);
if (dFile.getName().endsWith(".zip")) {
@ -343,19 +344,10 @@ public class Updater {
if (this.announce) {
this.plugin.getLogger().info("Finished updating.");
}
}
} catch (final Exception ex) {
this.plugin.getLogger().warning("The auto-updater tried to download a new update, but was unsuccessful.");
this.result = Updater.UpdateResult.FAIL_DOWNLOAD;
} finally {
try {
if (in != null) {
in.close();
}
if (fout != null) {
fout.close();
}
} catch (final Exception ex) {
}
}
}
@ -365,21 +357,18 @@ public class Updater {
* @param file the location of the file to extract.
*/
private void unzip(String file) {
try {
final File fSourceZip = new File(file);
final String zipPath = file.substring(0, file.length() - 4);
ZipFile zipFile = new ZipFile(fSourceZip);
try (ZipFile zipFile = new ZipFile(fSourceZip)) {
Enumeration<? extends ZipEntry> e = zipFile.entries();
while (e.hasMoreElements()) {
ZipEntry entry = e.nextElement();
File destinationFilePath = new File(zipPath, entry.getName());
destinationFilePath.getParentFile().mkdirs();
if (entry.isDirectory()) {
continue;
} else {
if (!entry.isDirectory()) {
final BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
int b;
final byte buffer[] = new byte[Updater.BYTE_SIZE];
final byte[] buffer = new byte[Updater.BYTE_SIZE];
final FileOutputStream fos = new FileOutputStream(destinationFilePath);
final BufferedOutputStream bos = new BufferedOutputStream(fos, Updater.BYTE_SIZE);
while ((b = bis.read(buffer, 0, Updater.BYTE_SIZE)) != -1) {
@ -393,15 +382,13 @@ public class Updater {
destinationFilePath.renameTo(new File(this.plugin.getDataFolder().getParent(), this.updateFolder + File.separator + name));
}
}
entry = null;
destinationFilePath = null;
}
e = null;
zipFile.close();
zipFile = null;
// Move any plugin data folders that were included to the right place, Bukkit won't do this for us.
for (final File dFile : new File(zipPath).listFiles()) {
File[] files = new File(zipPath).listFiles();
if (files != null) {
for (final File dFile : files) {
if (dFile.isDirectory()) {
if (this.pluginFile(dFile.getName())) {
final File oFile = new File(this.plugin.getDataFolder().getParent(), dFile.getName()); // Get current dir
@ -428,6 +415,7 @@ public class Updater {
}
dFile.delete();
}
}
new File(zipPath).delete();
fSourceZip.delete();
} catch (final IOException e) {
@ -444,11 +432,14 @@ public class Updater {
* @return true if a file inside the plugins folder is named this.
*/
private boolean pluginFile(String name) {
for (final File file : new File("plugins").listFiles()) {
File[] files = new File("plugins").listFiles();
if (files != null) {
for (final File file : files) {
if (file.getName().equals(name)) {
return true;
}
}
}
return false;
}