test/java/util/zip/ZipFile/CorruptedZipFiles.java
Print this page
rev 3516 : 7021582: convert jar/zip code and tests to use try-with-resources
Reviewed-by: alanb, dholmes, sherman
@@ -45,25 +45,23 @@
failed++;
t.printStackTrace();
}
public static void main(String[] args) throws Exception {
- ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("x.zip"));
- try {
+ try (FileOutputStream fos = new FileOutputStream("x.zip");
+ ZipOutputStream zos = new ZipOutputStream(fos))
+ {
ZipEntry e = new ZipEntry("x");
zos.putNextEntry(e);
zos.write((int)'x');
- } finally {
- zos.close();
}
int len = (int)(new File("x.zip").length());
byte[] good = new byte[len];
- FileInputStream fis = new FileInputStream("x.zip");
+ try (FileInputStream fis = new FileInputStream("x.zip")) {
fis.read(good);
- fis.close();
- fis = null;
+ }
new File("x.zip").delete();
int endpos = len - ENDHDR;
int cenpos = u16(good, endpos+ENDOFF);
int locpos = u16(good, cenpos+CENOFF);
@@ -148,32 +146,28 @@
static void checkZipExceptionImpl(byte[] data,
String msgPattern,
boolean getInputStream) {
String zipName = "bad" + (uniquifier++) + ".zip";
try {
- FileOutputStream fos = new FileOutputStream(zipName);
+ try (FileOutputStream fos = new FileOutputStream(zipName)) {
fos.write(data);
- fos.close();
- ZipFile zf = new ZipFile(zipName);
- try {
+ }
+ try (ZipFile zf = new ZipFile(zipName)) {
if (getInputStream) {
InputStream is = zf.getInputStream(new ZipEntry("x"));
is.read();
}
- } finally {
- zf.close();
}
fail("Failed to throw expected ZipException");
} catch (ZipException e) {
if (e.getMessage().matches(msgPattern))
passed++;
else
unexpected(e);
} catch (Throwable t) {
unexpected(t);
- }
- finally {
+ } finally {
new File(zipName).delete();
}
}
static void checkZipException(byte[] data, String msgPattern) {