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  * @key randomness
  34  */
  35 import java.lang.ref.ReferenceQueue;
  36 import java.lang.ref.WeakReference;
  37 import java.io.File;
  38 import java.io.FileOutputStream;
  39 import java.io.InputStream;
  40 import java.util.Enumeration;
  41 import java.util.HashSet;
  42 import java.util.Random;
  43 import java.util.Set;
  44 import java.util.zip.ZipEntry;
  45 import java.util.zip.ZipFile;
  46 import java.util.zip.ZipOutputStream;
  47 
  48 public class ClearStaleZipFileInputStreams {
  49     private static final int ZIP_ENTRY_NUM = 5;
  50 
  51     private static final byte[][] data;
  52 
  53     static {
  54         data = new byte[ZIP_ENTRY_NUM][];
  55         Random r = new Random();
  56         for (int i = 0; i < ZIP_ENTRY_NUM; i++) {
  57             data[i] = new byte[1000];
  58             r.nextBytes(data[i]);
  59         }
  60     }
  61 
  62     private static File createTestFile(int compression) throws Exception {
  63         File tempZipFile =
  64             File.createTempFile("test-data" + compression, ".zip");
  65         tempZipFile.deleteOnExit();
  66 
  67         try (FileOutputStream fos = new FileOutputStream(tempZipFile);
  68                 ZipOutputStream zos = new ZipOutputStream(fos)) {
  69             zos.setLevel(compression);
  70             for (int i = 0; i < ZIP_ENTRY_NUM; i++) {
  71                 String text = "Entry" + i;
  72                 ZipEntry entry = new ZipEntry(text);
  73                 zos.putNextEntry(entry);
  74                 try {
  75                     zos.write(data[i], 0, data[i].length);
  76                 } finally {
  77                     zos.closeEntry();
  78                 }
  79             }
  80         }
  81 
  82         return tempZipFile;
  83     }
  84 
  85     private static final class GcInducingThread extends Thread {
  86         private final int sleepMillis;
  87         private boolean keepRunning = true;
  88 
  89         public GcInducingThread(final int sleepMillis) {
  90             this.sleepMillis = sleepMillis;
  91         }
  92 
  93         public synchronized void run() {
  94             while (keepRunning) {
  95                 System.gc();
  96                 try {
  97                     wait(sleepMillis);
  98                 } catch (InterruptedException e) {
  99                     System.out.println("GCing thread unexpectedly interrupted");
 100                     return;
 101                 }
 102             }
 103         }
 104 
 105         public synchronized void shutDown() {
 106             keepRunning = false;
 107             notifyAll();
 108         }
 109     }
 110 
 111     public static void main(String[] args) throws Exception {
 112         GcInducingThread gcThread = new GcInducingThread(500);
 113         gcThread.start();
 114         try {
 115             runTest(ZipOutputStream.DEFLATED);
 116             runTest(ZipOutputStream.STORED);
 117         } finally {
 118             gcThread.shutDown();
 119             gcThread.join();
 120         }
 121     }
 122 
 123     private static void runTest(int compression) throws Exception {
 124         ReferenceQueue<InputStream> rq = new ReferenceQueue<>();
 125 
 126         System.out.println("Testing with a zip file with compression level = "
 127                 + compression);
 128         File f = createTestFile(compression);
 129         try (ZipFile zf = new ZipFile(f)) {
 130             Set<Object> refSet = createTransientInputStreams(zf, rq);
 131 
 132             System.out.println("Waiting for 'stale' input streams from ZipFile to be GC'd ...");
 133             System.out.println("(The test will hang on failure)");
 134             while (false == refSet.isEmpty()) {
 135                 refSet.remove(rq.remove());
 136             }
 137             System.out.println("Test PASSED.");
 138             System.out.println();
 139         } finally {
 140             f.delete();
 141         }
 142     }
 143 
 144     private static Set<Object> createTransientInputStreams(ZipFile zf,
 145             ReferenceQueue<InputStream> rq) throws Exception {
 146         Enumeration<? extends ZipEntry> zfe = zf.entries();
 147         Set<Object> refSet = new HashSet<>();
 148 
 149         while (zfe.hasMoreElements()) {
 150             InputStream is = zf.getInputStream(zfe.nextElement());
 151             refSet.add(new WeakReference<InputStream>(is, rq));
 152         }
 153 
 154         return refSet;
 155     }
 156 }