fix ArrayOutOfBoundsException & shows "task aborted".

This commit is contained in:
huanghongxun
2015-07-29 23:28:13 +08:00
parent 7e781dd24c
commit bfcad25dee
8 changed files with 56 additions and 55 deletions

View File

@@ -39,8 +39,15 @@ public abstract class Task {
* @return is aborted.
*/
public boolean abort() {
aborted = true;
return false;
}
protected boolean aborted = false;
public boolean isAborted() {
return aborted;
}
public Throwable getFailReason() {
return failReason;

View File

@@ -35,7 +35,7 @@ public class TaskList extends Thread {
ArrayList<NonConsumer> allDone = new ArrayList();
ArrayList<DoingDoneListener<Task>> taskListener = new ArrayList();
int totTask = 0;
int totTask;
boolean shouldContinue = true;
public TaskList() {
@@ -43,7 +43,6 @@ public class TaskList extends Thread {
public void clean() {
shouldContinue = true;
totTask = 0;
taskQueue.clear();
}
@@ -57,7 +56,6 @@ public class TaskList extends Thread {
public void addTask(Task task) {
taskQueue.add(task);
totTask++;
}
public int taskCount() {
@@ -120,7 +118,7 @@ public class TaskList extends Thread {
d.onDoing(t);
if (t.executeTask()) {
HMCLog.log("Task finished: " + t.getInfo());
HMCLog.log((t.isAborted() ? "Task aborted: " : "Task finished: ") + t.getInfo());
for (DoingDoneListener<Task> d : taskListener)
d.onDone(t);
for (DoingDoneListener<Task> d : t.getTaskListeners())
@@ -140,6 +138,7 @@ public class TaskList extends Thread {
Thread.currentThread().setName("TaskList");
threadPool.clear();
totTask = taskQueue.size();
for (Task taskQueue1 : taskQueue)
executeTask(taskQueue1);
if (shouldContinue)

View File

@@ -182,7 +182,7 @@ public class TaskWindow extends javax.swing.JDialog
private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
tasks.clear();
if (!this.failReasons.isEmpty()) {
MessageBox.Show(StrUtils.parseParams("", failReasons.toArray(), "\n"), C.i18n("message.error"), MessageBox.ERROR_MESSAGE);
failReasons.clear();
@@ -206,18 +206,18 @@ public class TaskWindow extends javax.swing.JDialog
ArrayList<Task> tasks = new ArrayList<>();
ArrayList<Integer> progresses = new ArrayList<>();
@Override
public void setProgress(Task task, int progress, int max) {
SwingUtilities.invokeLater(() -> {
int idx = tasks.indexOf(task);
if(idx == -1) return;
if (idx == -1) return;
int pgs = progress * 100 / max;
if(progresses.get(idx) != pgs) {
if (progresses.get(idx) != pgs) {
SwingUtils.setValueAt(lstDownload, pgs + "%", idx, 1);
progresses.set(idx, pgs);
}
if(task.isParallelExecuting()) return;
if (task.isParallelExecuting()) return;
pgsSingle.setMaximum(max);
pgsSingle.setValue(progress);
});
@@ -266,6 +266,7 @@ public class TaskWindow extends javax.swing.JDialog
pgsTotal.setMaximum(taskList.taskCount());
pgsTotal.setValue(pgsTotal.getValue() + 1);
int idx = tasks.indexOf(task);
if (idx == -1) return;
SwingUtils.setValueAt(lstDownload, task.getFailReason(), idx, 0);
SwingUtils.setValueAt(lstDownload, "0%", idx, 1);
SwingUtils.moveEnd(srlDownload);

View File

@@ -105,7 +105,7 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
RandomAccessFile file = null;
InputStream stream = null;
boolean shouldContinue = true, aborted = false;
boolean shouldContinue = true;
private void closeFiles() {
// Close file.
@@ -199,7 +199,10 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
ppl.setProgress(this, downloaded, size);
}
closeFiles();
tempFile.renameTo(filePath);
if (aborted)
tempFile.delete();
else
tempFile.renameTo(filePath);
if (ppl != null)
ppl.onProgressProviderDone(this);
return true;

View File

@@ -81,6 +81,7 @@ public class HTTPGetTask extends TaskInfo implements PreviousResult<String> {
@Override
public boolean abort() {
shouldContinue = false;
aborted = true;
return true;
}

View File

@@ -162,30 +162,20 @@ public final class JdkVersion {
JavaProcess jp = new JavaProcess(str, pb.start(), null);
InputStream is = jp.getRawProcess().getErrorStream();
BufferedReader br = null;
int lineNumber = 0;
String ver = null;
Platform platform = Platform.UNKNOWN;
Platform platform = Platform.BIT_32;
try {
br = new BufferedReader(new InputStreamReader(is));
String line;
jp.getRawProcess().waitFor();
while ((line = br.readLine()) != null) {
lineNumber++;
switch (lineNumber) {
case 1:
Matcher m = p.matcher(line);
if (m.find()) {
ver = m.group();
ver = ver.substring("java version \"".length(), ver.length() - 1);
}
break;
case 3:
if (line.contains("64-Bit"))
platform = Platform.BIT_64;
else
platform = Platform.BIT_32;
break;
Matcher m = p.matcher(line);
if (m.find()) {
ver = m.group();
ver = ver.substring("java version \"".length(), ver.length() - 1);
}
if (line.contains("64-Bit"))
platform = Platform.BIT_64;
}
} catch (InterruptedException | IOException e) {
HMCLog.warn("Failed to get java version", e);