Skip to content

Commit c64cc8f

Browse files
committed
Run JDK extraction on a Task with progress
1 parent 08da516 commit c64cc8f

File tree

1 file changed

+80
-19
lines changed

1 file changed

+80
-19
lines changed

src/main/java/net/imagej/ui/swing/updater/ImageJUpdater.java

+80-19
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import org.apache.commons.compress.archivers.ArchiveEntry;
4444
import org.apache.commons.compress.archivers.ArchiveInputStream;
45+
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
4546
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
4647
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
4748
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
@@ -57,6 +58,8 @@
5758
import org.scijava.plugin.Menu;
5859
import org.scijava.plugin.Parameter;
5960
import org.scijava.plugin.Plugin;
61+
import org.scijava.task.Task;
62+
import org.scijava.task.TaskService;
6063
import org.scijava.ui.DialogPrompt;
6164
import org.scijava.ui.UIService;
6265
import org.scijava.util.AppUtils;
@@ -87,6 +90,9 @@ public class ImageJUpdater implements UpdaterUI {
8790
@Parameter
8891
private DownloadService downloadService;
8992

93+
@Parameter
94+
private TaskService taskService;
95+
9096
@Parameter
9197
private LocationService locationService;
9298

@@ -323,32 +329,56 @@ private boolean updateJava(final Map<String, String> jdkVersions,
323329

324330
String javaLoc = jdkDlLoc.getAbsolutePath();
325331
int extensionLength = 0;
332+
int entryCount = 0;
326333

327334
// Extract the JDK
328335
if (jdkDlLoc.toString().endsWith("tar.gz")) {
336+
extensionLength = 7;
337+
// sadly this is the only way to determine how long extraction
329338
try (FileInputStream fis = new FileInputStream(jdkDlLoc);
330339
GzipCompressorInputStream gzIn = new GzipCompressorInputStream(fis);
331340
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn))
332341
{
333-
doExtraction(jdkDir, tarIn);
334-
extensionLength = 7;
342+
entryCount = countArchiveEntries(tarIn);
335343
}
336344
catch (IOException e) {
337345
log.error(e);
338346
return false;
339347
}
348+
// Do the actual extraction
349+
try (FileInputStream fis = new FileInputStream(jdkDlLoc);
350+
GzipCompressorInputStream gzIn = new GzipCompressorInputStream(fis);
351+
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn))
352+
{
353+
doExtraction(jdkDir, tarIn, entryCount);
354+
}
355+
catch (ExecutionException | InterruptedException | IOException e) {
356+
log.error(e);
357+
return false;
358+
}
340359
}
341360
else if (jdkDlLoc.toString().endsWith("zip")) {
361+
extensionLength = 4;
362+
// sadly this is the only way to determine how long extraction will take
342363
try (FileInputStream fis = new FileInputStream(jdkDlLoc);
343364
ZipArchiveInputStream zis = new ZipArchiveInputStream(fis))
344365
{
345-
doExtraction(jdkDir, zis);
346-
extensionLength = 4;
366+
entryCount = countArchiveEntries(zis);
347367
}
348368
catch (IOException e) {
349369
log.error(e);
350370
return false;
351371
}
372+
// Do the actual extraction
373+
try (FileInputStream fis = new FileInputStream(jdkDlLoc);
374+
ZipArchiveInputStream zis = new ZipArchiveInputStream(fis))
375+
{
376+
doExtraction(jdkDir, zis, entryCount);
377+
}
378+
catch (ExecutionException | InterruptedException | IOException e) {
379+
log.error(e);
380+
return false;
381+
}
352382
}
353383

354384
// Notify user of success
@@ -373,27 +403,58 @@ else if (jdkDlLoc.toString().endsWith("zip")) {
373403
}
374404

375405
/**
376-
* Helper method to extract an archive
406+
* Helper method to count the entries in an archive
377407
*/
378-
private void doExtraction(final File jdkDir, final ArchiveInputStream tarIn)
408+
private int countArchiveEntries(final ArchiveInputStream ais)
379409
throws IOException
380410
{
411+
int entryCount = 0;
381412
ArchiveEntry entry;
382-
while ((entry = tarIn.getNextEntry()) != null) {
383-
if (entry.isDirectory()) {
384-
new File(jdkDir, entry.getName()).mkdirs();
385-
}
386-
else {
387-
byte[] buffer = new byte[1024];
388-
File outputFile = new File(jdkDir, entry.getName());
389-
OutputStream fos = new FileOutputStream(outputFile);
390-
int len;
391-
while ((len = tarIn.read(buffer)) != -1) {
392-
fos.write(buffer, 0, len);
413+
while ((entry = ais.getNextEntry()) != null) {
414+
entryCount++;
415+
}
416+
return entryCount;
417+
}
418+
419+
/**
420+
* Helper method to extract an archive
421+
*/
422+
private void doExtraction(final File jdkDir, final ArchiveInputStream ais,
423+
final int entryCount) throws IOException, ExecutionException,
424+
InterruptedException
425+
{
426+
Task task = taskService.createTask("Extracting JDK");
427+
task.setProgressMaximum(entryCount);
428+
task.setProgressValue(0);
429+
task.start();
430+
task.run(() -> {
431+
try {
432+
int currentEntry = 0;
433+
ArchiveEntry entry;
434+
while ((entry = ais.getNextEntry()) != null) {
435+
if (task != null && task.isCanceled()) break;
436+
if (entry.isDirectory()) {
437+
new File(jdkDir, entry.getName()).mkdirs();
438+
}
439+
else {
440+
byte[] buffer = new byte[1024];
441+
File outputFile = new File(jdkDir, entry.getName());
442+
OutputStream fos = new FileOutputStream(outputFile);
443+
int len;
444+
while ((len = ais.read(buffer)) != -1) {
445+
fos.write(buffer, 0, len);
446+
}
447+
fos.close();
448+
}
449+
task.setProgressValue(++currentEntry);
393450
}
394-
fos.close();
395451
}
396-
}
452+
catch (IOException e) {
453+
task.setStatusMessage("Java extraction failed!");
454+
}
455+
});
456+
task.waitFor();
457+
task.finish();
397458
}
398459

399460
/**

0 commit comments

Comments
 (0)