test/java/util/zip/Available.java
Print this page
rev 3516 : 7021582: convert jar/zip code and tests to use try-with-resources
Reviewed-by: alanb, dholmes, sherman
@@ -42,18 +42,21 @@
private static void test1() throws Exception {
File f = new File(System.getProperty("test.src", "."), "input.jar");
// test ZipInputStream
- ZipInputStream z = new ZipInputStream(new FileInputStream(f));
+ try (FileInputStream fis = new FileInputStream(f);
+ ZipInputStream z = new ZipInputStream(fis))
+ {
z.getNextEntry();
tryAvail(z);
+ }
// test InflaterInputStream
- ZipFile zfile = new ZipFile(f);
+ try (ZipFile zfile = new ZipFile(f)) {
tryAvail(zfile.getInputStream(zfile.getEntry("Available.java")));
- z.close();
+ }
}
static void tryAvail(InputStream in) throws Exception {
byte[] buf = new byte[1024];
int n;
@@ -65,11 +68,11 @@
}
// To reproduce 4401122
private static void test2() throws Exception {
File f = new File(System.getProperty("test.src", "."), "input.jar");
- ZipFile zf = new ZipFile(f);
+ try (ZipFile zf = new ZipFile(f)) {
InputStream in = zf.getInputStream(zf.getEntry("Available.java"));
int initialAvailable = in.available();
in.read();
if (in.available() != initialAvailable - 1)
@@ -80,7 +83,8 @@
throw new RuntimeException();
in.close();
if (in.available() != 0)
throw new RuntimeException();
}
+ }
}