test/java/util/zip/ZipCoding.java

Print this page
rev 3516 : 7021582: convert jar/zip code and tests to use try-with-resources
Reviewed-by: XXX

*** 55,82 **** static void testZipInputStream(InputStream is, Charset cs, String name, String comment, byte[] bb) throws Exception { ! ZipInputStream zis = new ZipInputStream(is, cs); ZipEntry e = zis.getNextEntry(); if (e == null || ! name.equals(e.getName())) throw new RuntimeException("ZipIS name doesn't match!"); byte[] bBuf = new byte[bb.length << 1]; int n = zis.read(bBuf, 0, bBuf.length); if (n != bb.length || !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) { throw new RuntimeException("ZipIS content doesn't match!"); } ! zis.close(); } static void testZipFile(File f, Charset cs, String name, String comment, byte[] bb) throws Exception { ! ZipFile zf = new ZipFile(f, cs); Enumeration<? extends ZipEntry> zes = zf.entries(); ZipEntry e = (ZipEntry)zes.nextElement(); if (! name.equals(e.getName()) || ! comment.equals(e.getComment())) throw new RuntimeException("ZipFile: name/comment doesn't match!"); --- 55,82 ---- static void testZipInputStream(InputStream is, Charset cs, String name, String comment, byte[] bb) throws Exception { ! try (ZipInputStream zis = new ZipInputStream(is, cs)) { ZipEntry e = zis.getNextEntry(); if (e == null || ! name.equals(e.getName())) throw new RuntimeException("ZipIS name doesn't match!"); byte[] bBuf = new byte[bb.length << 1]; int n = zis.read(bBuf, 0, bBuf.length); if (n != bb.length || !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) { throw new RuntimeException("ZipIS content doesn't match!"); } ! } } static void testZipFile(File f, Charset cs, String name, String comment, byte[] bb) throws Exception { ! try (ZipFile zf = new ZipFile(f, cs)) { Enumeration<? extends ZipEntry> zes = zf.entries(); ZipEntry e = (ZipEntry)zes.nextElement(); if (! name.equals(e.getName()) || ! comment.equals(e.getComment())) throw new RuntimeException("ZipFile: name/comment doesn't match!");
*** 91,117 **** } if (n != bb.length || !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) { throw new RuntimeException("ZipFile content doesn't match!"); } ! zf.close(); } static void test(String csn, String name, String comment) throws Exception { ! byte[] bb = "This is the conent of the zipfile".getBytes("ISO-8859-1"); Charset cs = Charset.forName(csn); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ! ZipOutputStream zos = new ZipOutputStream(baos, cs); ! ZipEntry e = new ZipEntry(name); e.setComment(comment); zos.putNextEntry(e); zos.write(bb, 0, bb.length); zos.closeEntry(); ! zos.close(); ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); testZipInputStream(bis, cs, name, comment, bb); if ("utf-8".equals(csn)) { // EFS should be set --- 91,116 ---- } if (n != bb.length || !Arrays.equals(bb, Arrays.copyOf(bBuf, n))) { throw new RuntimeException("ZipFile content doesn't match!"); } ! } } static void test(String csn, String name, String comment) throws Exception { ! byte[] bb = "This is the content of the zipfile".getBytes("ISO-8859-1"); Charset cs = Charset.forName(csn); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ! try (ZipOutputStream zos = new ZipOutputStream(baos, cs)) { ZipEntry e = new ZipEntry(name); e.setComment(comment); zos.putNextEntry(e); zos.write(bb, 0, bb.length); zos.closeEntry(); ! } ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); testZipInputStream(bis, cs, name, comment, bb); if ("utf-8".equals(csn)) { // EFS should be set
*** 119,131 **** testZipInputStream(bis, Charset.forName("MS932"), name, comment, bb); } File f = new File(new File(System.getProperty("test.dir", ".")), "zfcoding.zip"); ! FileOutputStream fos = new FileOutputStream(f); baos.writeTo(fos); ! fos.close(); testZipFile(f, cs, name, comment, bb); if ("utf-8".equals(csn)) { testZipFile(f, Charset.forName("MS932"), name, comment, bb); } f.delete(); --- 118,130 ---- testZipInputStream(bis, Charset.forName("MS932"), name, comment, bb); } File f = new File(new File(System.getProperty("test.dir", ".")), "zfcoding.zip"); ! try (FileOutputStream fos = new FileOutputStream(f)) { baos.writeTo(fos); ! } testZipFile(f, cs, name, comment, bb); if ("utf-8".equals(csn)) { testZipFile(f, Charset.forName("MS932"), name, comment, bb); } f.delete();