src/share/classes/com/sun/java/util/jar/pack/Driver.java
Print this page
rev 3516 : 7021582: convert jar/zip code and tests to use try-with-resources
Reviewed-by: XXX
@@ -148,16 +148,16 @@
}
// See if there is any other action to take.
if ("--config-file=".equals(state)) {
String propFile = av.remove(0);
- InputStream propIn = new FileInputStream(propFile);
Properties fileProps = new Properties();
- fileProps.load(new BufferedInputStream(propIn));
+ try (InputStream propIn = new FileInputStream(propFile)) {
+ fileProps.load(propIn);
+ }
if (engProps.get(verboseProp) != null)
fileProps.list(System.out);
- propIn.close();
for (Map.Entry<Object,Object> me : fileProps.entrySet()) {
engProps.put((String) me.getKey(), (String) me.getValue());
}
} else if ("--version".equals(state)) {
System.out.println(MessageFormat.format(RESOURCE.getString(DriverResource.VERSION), Driver.class.getName(), "1.31, 07/05/05"));
@@ -346,14 +346,14 @@
if (outfile.equals("-"))
fileOut = System.out;
else
fileOut = new FileOutputStream(outfile);
fileOut = new BufferedOutputStream(fileOut);
- JarOutputStream out = new JarOutputStream(fileOut);
+ try (JarOutputStream out = new JarOutputStream(fileOut)) {
junpack.unpack(in, out);
- //in.close(); // p200 closes in but not out
- out.close();
+ // p200 closes in but not out
+ }
// At this point, we have a good jarfile (or newfile, if -r)
}
if (!bakfile.equals("")) {
// On success, abort jarfile recovery bracket.
@@ -409,12 +409,11 @@
String getZipComment(String jarfile) throws IOException {
byte[] tail = new byte[1000];
long filelen = new File(jarfile).length();
if (filelen <= 0) return "";
long skiplen = Math.max(0, filelen - tail.length);
- InputStream in = new FileInputStream(new File(jarfile));
- try {
+ try (InputStream in = new FileInputStream(new File(jarfile))) {
in.skip(skiplen);
in.read(tail);
for (int i = tail.length-4; i >= 0; i--) {
if (tail[i+0] == 'P' && tail[i+1] == 'K' &&
tail[i+2] == 5 && tail[i+3] == 6) {
@@ -424,12 +423,10 @@
return new String(tail, i, tail.length-i, "UTF8");
return "";
}
}
return "";
- } finally {
- in.close();
}
}
private static final String PACK200_OPTION_MAP =
(""