1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * Portions Copyright (c) 2011 IBM Corporation
  26  */
  27 
  28 /*
  29  * @test
  30  * @bug 7031076
  31  * @summary Allow stale InputStreams from ZipFiles to be GC'd
  32  * @author Neil Richards <neil.richards@ngmr.net>, <neil_richards@uk.ibm.com>
  33  */
  34 import java.lang.ref.ReferenceQueue;
  35 import java.lang.ref.WeakReference;
  36 import java.io.File;
  37 import java.io.FileOutputStream;
  38 import java.io.InputStream;
  39 import java.util.Enumeration;
  40 import java.util.HashSet;
  41 import java.util.Random;
  42 import java.util.Set;
  43 import java.util.zip.ZipEntry;
  44 import java.util.zip.ZipFile;
  45 import java.util.zip.ZipOutputStream;
  46 
  47 public class ClearStaleZipFileInputStreams {
  48     private static final int ZIP_ENTRY_NUM = 5;
  49 
  50     private static final byte[][] data;
  51 
  52     static {
  53         data = new byte[ZIP_ENTRY_NUM][];
  54         Random r = new Random();
  55         for (int i = 0; i < ZIP_ENTRY_NUM; i++) {
  56             data[i] = new byte[1000];
  57             r.nextBytes(data[i]);
  58         }
  59     }
  60 
  61     private static File createTestFile(int compression) throws Exception {
  62         File tempZipFile =
  63             File.createTempFile("test-data" + compression, ".zip");
  64         tempZipFile.deleteOnExit();
  65 
  66         try (FileOutputStream fos = new FileOutputStream(tempZipFile);
  67                 ZipOutputStream zos = new ZipOutputStream(fos)) {
  68             zos.setLevel(compression);
  69             for (int i = 0; i < ZIP_ENTRY_NUM; i++) {
  70                 String text = "Entry" + i;
  71                 ZipEntry entry = new ZipEntry(text);
  72                 zos.putNextEntry(entry);
  73                 try {
  74                     zos.write(data[i], 0, data[i].length);
  75                 } finally {
  76                     zos.closeEntry();
  77                 }
  78             }
  79         }
  80 
  81         return tempZipFile;
  82     }
  83 
  84     private static final class GcInducingThread extends Thread {
  85         private final int sleepMillis;
  86         private boolean keepRunning = true;
  87 
  88         public GcInducingThread(final int sleepMillis) {
  89             this.sleepMillis = sleepMillis;
  90         }
  91 
  92         public synchronized void run() {
  93             while (keepRunning) {
  94                 System.gc();
  95                 try {
  96                     wait(sleepMillis);
  97                 } catch (InterruptedException e) {
  98                     System.out.println("GCing thread unexpectedly interrupted");
  99                     return;
 100                 }
 101             }
 102         }
 103 
 104         public synchronized void shutDown() {
 105             keepRunning = false;
 106             notifyAll();
 107         }
 108     }
 109 
 110     public static void main(String[] args) throws Exception {
 111         GcInducingThread gcThread = new GcInducingThread(500);
 112         gcThread.start();
 113         try {
 114             runTest(ZipOutputStream.DEFLATED);
 115             runTest(ZipOutputStream.STORED);
 116         } finally {
 117             gcThread.shutDown();
 118             gcThread.join();
 119         }
 120     }
 121 
 122     private static void runTest(int compression) throws Exception {
 123         ReferenceQueue<InputStream> rq = new ReferenceQueue<>();
 124 
 125         System.out.println("Testing with a zip file with compression level = "
 126                 + compression);
 127         File f = createTestFile(compression);
 128         try (ZipFile zf = new ZipFile(f)) {
 129             Set<Object> refSet = createTransientInputStreams(zf, rq);
 130 
 131             System.out.println("Waiting for 'stale' input streams from ZipFile to be GC'd ...");
 132             System.out.println("(The test will hang on failure)");
 133             while (false == refSet.isEmpty()) {
 134                 refSet.remove(rq.remove());
 135             }
 136             System.out.println("Test PASSED.");
 137             System.out.println();
 138         } finally {
 139             f.delete();
 140         }
 141     }
 142 
 143     private static Set<Object> createTransientInputStreams(ZipFile zf,
 144             ReferenceQueue<InputStream> rq) throws Exception {
 145         Enumeration<? extends ZipEntry> zfe = zf.entries();
 146         Set<Object> refSet = new HashSet<>();
 147 
 148         while (zfe.hasMoreElements()) {
 149             InputStream is = zf.getInputStream(zfe.nextElement());
 150             refSet.add(new WeakReference<InputStream>(is, rq));
 151         }
 152 
 153         return refSet;
 154     }
 155 }