test/java/util/zip/ZipFile/Comment.java
Print this page
rev 3516 : 7021582: convert jar/zip code and tests to use try-with-resources
Reviewed-by: alanb, dholmes, sherman
@@ -55,36 +55,36 @@
}
private static void writeZipFile(String name, String comment)
throws IOException
{
- ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(name));
- try {
+ try (FileOutputStream fos = new FileOutputStream(name);
+ ZipOutputStream zos = new ZipOutputStream(fos))
+ {
zos.setComment(comment);
ZipEntry ze = new ZipEntry(entryName);
ze.setMethod(ZipEntry.DEFLATED);
zos.putNextEntry(ze);
new DataOutputStream(zos).writeUTF(entryContents);
zos.closeEntry();
- } finally {
- zos.close();
}
}
private static void verifyZipFile(String name, String comment)
throws Exception
{
// Check that Zip entry was correctly written.
- ZipFile zipFile = new ZipFile(name);
+ try (ZipFile zipFile = new ZipFile(name)) {
ZipEntry zipEntry = zipFile.getEntry(entryName);
InputStream is = zipFile.getInputStream(zipEntry);
String result = new DataInputStream(is).readUTF();
if (!result.equals(entryContents))
throw new Exception("Entry contents corrupted");
+ }
+ try (RandomAccessFile file = new RandomAccessFile(name, "r")) {
// Check that comment length was correctly written.
- RandomAccessFile file = new RandomAccessFile(name, "r");
file.seek(file.length() - comment.length()
- ZipFile.ENDHDR + ZipFile.ENDCOM);
int b1 = file.readUnsignedByte();
int b2 = file.readUnsignedByte();
if (b1 + (b2 << 8) != comment.length())
@@ -92,15 +92,14 @@
// Check that comment was correctly written.
file.seek(file.length() - comment.length());
byte [] bytes = new byte [comment.length()];
file.readFully(bytes);
- zipFile.close();
- file.close();
if (! comment.equals(new String(bytes, "UTF8")))
throw new Exception("Zip file comment corrupted");
}
+ }
private static String buildComment(int length) {
StringBuffer result = new StringBuffer();
while (result.length() < length) {
/* Endhdr is 22 bytes long, so this pattern makes it easy