Skip to content

Commit f7ce52f

Browse files
authored
Use try-with-resources to ensure zip input is closed on exceptions too (#654)
1 parent 3c2f4ed commit f7ce52f

4 files changed

Lines changed: 75 additions & 77 deletions

File tree

‎plugins/org.eclipse.embedcdt.debug.gdbjtag.core/META-INF/MANIFEST.MF‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %bundle.name
44
Bundle-SymbolicName: org.eclipse.embedcdt.debug.gdbjtag.core;singleton:=true
5-
Bundle-Version: 5.2.2.qualifier
5+
Bundle-Version: 5.2.3.qualifier
66
Bundle-Activator: org.eclipse.embedcdt.internal.debug.gdbjtag.core.Activator
77
Require-Bundle: org.eclipse.embedcdt.core;bundle-version="6.7.0",
88
org.eclipse.embedcdt.debug.core;bundle-version="2.0.0",

‎plugins/org.eclipse.embedcdt.debug.gdbjtag.core/src/org/eclipse/embedcdt/debug/gdbjtag/core/datamodel/SvdUtils.java‎

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -435,35 +435,34 @@ public static Leaf getTree(IPath path) throws CoreException {
435435
out.println("Decompressing zipped SVD file \"" + path.toOSString() + "\"...");
436436

437437
// This is the signature of ZIP files.
438-
ZipInputStream zipInput;
439-
zipInput = new ZipInputStream(new FileInputStream(file));
440-
// Get the zipped file list entry
441-
ZipEntry zipEntry = zipInput.getNextEntry();
442-
while (zipEntry != null) {
443-
if (!zipEntry.isDirectory()) {
444-
String fileName = zipEntry.getName();
445-
446-
File outFile = PacksStorage.getCachedFileObject(fileName);
447-
if (!outFile.getParentFile().exists()) {
448-
outFile.getParentFile().mkdirs();
438+
try (ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file))) {
439+
// Get the zipped file list entry
440+
ZipEntry zipEntry = zipInput.getNextEntry();
441+
while (zipEntry != null) {
442+
if (!zipEntry.isDirectory()) {
443+
String fileName = zipEntry.getName();
444+
445+
File outFile = PacksStorage.getCachedFileObject(fileName);
446+
if (!outFile.getParentFile().exists()) {
447+
outFile.getParentFile().mkdirs();
448+
}
449+
out.println("Writing \"" + outFile + "\"...");
450+
451+
OutputStream output = new FileOutputStream(outFile);
452+
453+
byte[] buf = new byte[1024];
454+
int bytesRead;
455+
while ((bytesRead = zipInput.read(buf)) > 0) {
456+
output.write(buf, 0, bytesRead);
457+
}
458+
output.close();
459+
actualPath = new Path(outFile.getAbsolutePath());
460+
break;
449461
}
450-
out.println("Writing \"" + outFile + "\"...");
451-
452-
OutputStream output = new FileOutputStream(outFile);
453-
454-
byte[] buf = new byte[1024];
455-
int bytesRead;
456-
while ((bytesRead = zipInput.read(buf)) > 0) {
457-
output.write(buf, 0, bytesRead);
458-
}
459-
output.close();
460-
actualPath = new Path(outFile.getAbsolutePath());
461-
break;
462+
zipEntry = zipInput.getNextEntry();
462463
}
463-
zipEntry = zipInput.getNextEntry();
464+
zipInput.closeEntry();
464465
}
465-
zipInput.closeEntry();
466-
zipInput.close();
467466

468467
file = actualPath.toFile();
469468
reader = new FileReader(file);

‎plugins/org.eclipse.embedcdt.packs.core/META-INF/MANIFEST.MF‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %bundle.name
44
Bundle-SymbolicName: org.eclipse.embedcdt.packs.core;singleton:=true
5-
Bundle-Version: 3.1.3.qualifier
5+
Bundle-Version: 3.1.4.qualifier
66
Bundle-Activator: org.eclipse.embedcdt.internal.packs.core.Activator
77
Bundle-RequiredExecutionEnvironment: JavaSE-11
88
Bundle-ActivationPolicy: lazy

‎plugins/org.eclipse.embedcdt.packs.core/src/org/eclipse/embedcdt/packs/core/jobs/InstallJob.java‎

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -333,73 +333,72 @@ private boolean unzip(File archiveFile, IPath destRelativePath) throws IOExcepti
333333
boolean result = true;
334334

335335
// Get the zip file content.
336-
ZipInputStream zipInput;
337-
zipInput = new ZipInputStream(new FileInputStream(archiveFile));
338-
// Get the zipped file list entry
339-
ZipEntry zipEntry = zipInput.getNextEntry();
336+
try (ZipInputStream zipInput = new ZipInputStream(new FileInputStream(archiveFile))) {
337+
// Get the zipped file list entry
338+
ZipEntry zipEntry = zipInput.getNextEntry();
340339

341-
int countFiles = 0;
342-
int countBytes = 0;
343-
while (zipEntry != null && (result == true)) {
340+
int countFiles = 0;
341+
int countBytes = 0;
342+
while (zipEntry != null && (result == true)) {
344343

345-
// Skip the folder definitions, we automatically create them.
346-
if (!zipEntry.isDirectory()) {
344+
// Skip the folder definitions, we automatically create them.
345+
if (!zipEntry.isDirectory()) {
347346

348-
String fileName = zipEntry.getName();
347+
String fileName = zipEntry.getName();
349348

350-
IPath path = destRelativePath.append(fileName);
351-
File outFile = PacksStorage.getFileObject(path.toString());
352-
if (!outFile.getParentFile().exists()) {
353-
outFile.getParentFile().mkdirs();
354-
}
355-
fOut.println("Writing \"" + outFile + "\"...");
356-
357-
OutputStream output = new FileOutputStream(outFile);
358-
359-
byte[] buf = new byte[1024];
360-
int bytesRead;
361-
while ((bytesRead = zipInput.read(buf)) > 0) {
349+
IPath path = destRelativePath.append(fileName);
350+
File outFile = PacksStorage.getFileObject(path.toString());
351+
if (!outFile.getParentFile().exists()) {
352+
outFile.getParentFile().mkdirs();
353+
}
354+
fOut.println("Writing \"" + outFile + "\"...");
355+
356+
OutputStream output = new FileOutputStream(outFile);
357+
358+
byte[] buf = new byte[1024];
359+
int bytesRead;
360+
while ((bytesRead = zipInput.read(buf)) > 0) {
361+
try {
362+
output.write(buf, 0, bytesRead);
363+
} catch (IOException e) {
364+
String msg = e.getMessage() + ", file: " + outFile.getName();
365+
fOut.println("Error: " + msg);
366+
DataUtils.reportError(msg);
367+
368+
result = false;
369+
break;
370+
}
371+
countBytes += bytesRead;
372+
}
362373
try {
363-
output.write(buf, 0, bytesRead);
374+
output.close();
364375
} catch (IOException e) {
365376
String msg = e.getMessage() + ", file: " + outFile.getName();
366377
fOut.println("Error: " + msg);
367378
DataUtils.reportError(msg);
368379

369380
result = false;
370-
break;
371381
}
372-
countBytes += bytesRead;
373-
}
374-
try {
375-
output.close();
376-
} catch (IOException e) {
377-
String msg = e.getMessage() + ", file: " + outFile.getName();
378-
fOut.println("Error: " + msg);
379-
DataUtils.reportError(msg);
380-
381-
result = false;
382-
}
383382

384-
outFile.setReadOnly();
385-
++countFiles;
383+
outFile.setReadOnly();
384+
++countFiles;
386385

387-
}
386+
}
388387

389-
zipEntry = zipInput.getNextEntry();
390-
}
388+
zipEntry = zipInput.getNextEntry();
389+
}
391390

392-
fMonitor.worked(1);
391+
fMonitor.worked(1);
393392

394-
zipInput.closeEntry();
395-
zipInput.close();
396-
if (countBytes > 0) {
397-
fOut.println(countFiles + " files written, " + StringUtils.convertSizeToString(countBytes) + ".");
398-
} else {
399-
fOut.println("No files written.");
400-
result = false;
393+
zipInput.closeEntry();
394+
if (countBytes > 0) {
395+
fOut.println(countFiles + " files written, " + StringUtils.convertSizeToString(countBytes) + ".");
396+
} else {
397+
fOut.println("No files written.");
398+
result = false;
399+
}
400+
return result;
401401
}
402-
return result;
403402
}
404403

405404
}

0 commit comments

Comments
 (0)