mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 12:18:00 +00:00
Adding controls on ProgressBar that might avoid sudden NPE with downloading net decks.
I am still not sure how to reproduce the issue, but I do sometimes get a NPE on progress bar (directly raised by Java Swing, tbh) when downloading certain net decks. I have the feeling there might be some sort of concurrency issue//race condition so just in case, any update to the progress bar is now surrounded by a check that's not null.
This commit is contained in:
@@ -1,12 +1,13 @@
|
|||||||
package forge.gui.download;
|
package forge.gui.download;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import com.esotericsoftware.minlog.Log;
|
||||||
import java.io.BufferedOutputStream;
|
import com.google.common.io.Files;
|
||||||
import java.io.File;
|
import forge.gui.FThreads;
|
||||||
import java.io.FileOutputStream;
|
import forge.gui.GuiBase;
|
||||||
import java.io.IOException;
|
import forge.gui.interfaces.IProgressBar;
|
||||||
import java.io.InputStream;
|
import forge.util.FileUtil;
|
||||||
import java.io.OutputStream;
|
|
||||||
|
import java.io.*;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
@@ -16,14 +17,6 @@ import java.util.Map;
|
|||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipFile;
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
import com.esotericsoftware.minlog.Log;
|
|
||||||
import com.google.common.io.Files;
|
|
||||||
|
|
||||||
import forge.gui.FThreads;
|
|
||||||
import forge.gui.GuiBase;
|
|
||||||
import forge.gui.interfaces.IProgressBar;
|
|
||||||
import forge.util.FileUtil;
|
|
||||||
|
|
||||||
public class GuiDownloadZipService extends GuiDownloadService {
|
public class GuiDownloadZipService extends GuiDownloadService {
|
||||||
private final String name, desc, sourceUrl, destFolder, deleteFolder;
|
private final String name, desc, sourceUrl, destFolder, deleteFolder;
|
||||||
private int filesExtracted;
|
private int filesExtracted;
|
||||||
@@ -66,7 +59,8 @@ public class GuiDownloadZipService extends GuiDownloadService {
|
|||||||
FThreads.invokeInEdtNowOrLater(new Runnable() {
|
FThreads.invokeInEdtNowOrLater(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
progressBar.setDescription(filesExtracted + " " + desc + " extracted");
|
if (progressBar != null)
|
||||||
|
progressBar.setDescription(filesExtracted + " " + desc + " extracted");
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -85,6 +79,8 @@ public class GuiDownloadZipService extends GuiDownloadService {
|
|||||||
public String download(final String filename) {
|
public String download(final String filename) {
|
||||||
GuiBase.getInterface().preventSystemSleep(true); //prevent system from going into sleep mode while downloading
|
GuiBase.getInterface().preventSystemSleep(true); //prevent system from going into sleep mode while downloading
|
||||||
|
|
||||||
|
if (progressBar == null)
|
||||||
|
return "";
|
||||||
progressBar.reset();
|
progressBar.reset();
|
||||||
progressBar.setPercentMode(true);
|
progressBar.setPercentMode(true);
|
||||||
progressBar.setDescription("Downloading " + desc);
|
progressBar.setDescription("Downloading " + desc);
|
||||||
@@ -128,7 +124,8 @@ public class GuiDownloadZipService extends GuiDownloadService {
|
|||||||
if (cancel) { break; }
|
if (cancel) { break; }
|
||||||
|
|
||||||
total += count;
|
total += count;
|
||||||
progressBar.setValue((int)(100 * total / contentLength));
|
if (progressBar != null)
|
||||||
|
progressBar.setValue((int)(100 * total / contentLength));
|
||||||
output.write(data, 0, count);
|
output.write(data, 0, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,8 +158,10 @@ public class GuiDownloadZipService extends GuiDownloadService {
|
|||||||
final File deleteDir = new File(deleteFolder);
|
final File deleteDir = new File(deleteFolder);
|
||||||
if (deleteDir.exists()) {
|
if (deleteDir.exists()) {
|
||||||
//attempt to delete previous res directory if to be rebuilt
|
//attempt to delete previous res directory if to be rebuilt
|
||||||
progressBar.reset();
|
if (progressBar != null) {
|
||||||
progressBar.setDescription("Deleting old " + desc + "...");
|
progressBar.reset();
|
||||||
|
progressBar.setDescription("Deleting old " + desc + "...");
|
||||||
|
}
|
||||||
if (deleteFolder.equals(destFolder)) { //move zip file to prevent deleting it
|
if (deleteFolder.equals(destFolder)) { //move zip file to prevent deleting it
|
||||||
final String oldZipFilename = zipFilename;
|
final String oldZipFilename = zipFilename;
|
||||||
zipFilename = deleteDir.getParentFile().getAbsolutePath() + File.separator + "temp.zip";
|
zipFilename = deleteDir.getParentFile().getAbsolutePath() + File.separator + "temp.zip";
|
||||||
@@ -181,11 +180,12 @@ public class GuiDownloadZipService extends GuiDownloadService {
|
|||||||
}
|
}
|
||||||
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||||
|
|
||||||
progressBar.reset();
|
if (progressBar != null) {
|
||||||
progressBar.setPercentMode(true);
|
progressBar.reset();
|
||||||
progressBar.setDescription("Extracting " + desc);
|
progressBar.setPercentMode(true);
|
||||||
progressBar.setMaximum(zipFile.size());
|
progressBar.setDescription("Extracting " + desc);
|
||||||
|
progressBar.setMaximum(zipFile.size());
|
||||||
|
}
|
||||||
FileUtil.ensureDirectoryExists(destFolder);
|
FileUtil.ensureDirectoryExists(destFolder);
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@@ -199,15 +199,18 @@ public class GuiDownloadZipService extends GuiDownloadService {
|
|||||||
final String path = destFolder + File.separator + entry.getName();
|
final String path = destFolder + File.separator + entry.getName();
|
||||||
if (entry.isDirectory()) {
|
if (entry.isDirectory()) {
|
||||||
new File(path).mkdir();
|
new File(path).mkdir();
|
||||||
progressBar.setValue(++count);
|
if (progressBar != null)
|
||||||
|
progressBar.setValue(++count);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
copyInputStream(zipFile.getInputStream(entry), path);
|
copyInputStream(zipFile.getInputStream(entry), path);
|
||||||
progressBar.setValue(++count);
|
if (progressBar != null)
|
||||||
|
progressBar.setValue(++count);
|
||||||
filesExtracted++;
|
filesExtracted++;
|
||||||
}
|
}
|
||||||
catch (final Exception e) { //don't quit out completely if an entry is not UTF-8
|
catch (final Exception e) { //don't quit out completely if an entry is not UTF-8
|
||||||
progressBar.setValue(++count);
|
if (progressBar != null)
|
||||||
|
progressBar.setValue(++count);
|
||||||
failedCount++;
|
failedCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user