jdk/test/java/util/zip/InterruptibleZip.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2005 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 6304463
       
    26  * @summary Test whether the zip file still can be read after thread is interrupted
       
    27  */
       
    28 
       
    29 import java.io.*;
       
    30 import java.util.zip.*;
       
    31 
       
    32 public class InterruptibleZip {
       
    33     private static String rtJar() {
       
    34         String bcp = System.getProperty("sun.boot.class.path");
       
    35         for (String pathElement : bcp.split(File.pathSeparator)) {
       
    36             if (pathElement.endsWith(File.separator + "rt.jar") &&
       
    37                 new File(pathElement).exists()) {
       
    38                 System.out.println("rtJar="+pathElement);
       
    39                 return pathElement;
       
    40             }
       
    41             if (pathElement.endsWith(File.separator + "classes") &&
       
    42                 new File(pathElement).isDirectory()) {
       
    43                 System.out.println("rt.jar not available");
       
    44                 return null;
       
    45             }
       
    46         }
       
    47         throw new Error("Can't find rt.jar or classes directory");
       
    48     }
       
    49 
       
    50     public static void main(String[] args) throws Exception {
       
    51         /* Interrupt the current thread. The is.read call below
       
    52            should continue reading rt.jar.
       
    53         */
       
    54         String rtJar = rtJar();
       
    55         if (rtJar == null) return;
       
    56         Thread.currentThread().interrupt();
       
    57         ZipFile zf = new ZipFile(rtJar);
       
    58         ZipEntry ze = zf.getEntry("java/lang/Object.class");
       
    59         InputStream is = zf.getInputStream(ze);
       
    60         byte[] buf = new byte[512];
       
    61         int n = is.read(buf);
       
    62         boolean interrupted = Thread.interrupted();
       
    63         System.out.printf("interrupted=%s n=%d name=%s%n",
       
    64                           interrupted, n, ze.getName());
       
    65         if (! interrupted) {
       
    66             throw new Error("Wrong interrupt status");
       
    67         }
       
    68         if (n != buf.length) {
       
    69             throw new Error("Read error");
       
    70         }
       
    71     }
       
    72 }