Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/jar/B6449504.java
38867 views
/*1* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 644950426* @run shell copyin.sh bar.jar27* @run main B6449504 caching28* @run main B6449504 no_caching29* @summary REGRESSION: ZipException throws when try to read a XML file30*/3132import java.io.*;33import java.net.*;3435public class B6449504 {3637public static void main (String[] args) throws Exception {3839boolean caching = args[0].equals ("caching");4041String dirname = System.getProperty ("test.classes");42File f = new File (dirname);43dirname = f.toURI().toString();4445String u = "jar:"+ dirname + "/bar.jar";46URL url = new URL (u+"!/DoesNotExist.txt");47System.out.println ("url = " + url);48JarURLConnection j1 = (JarURLConnection)url.openConnection();4950URL url2 = new URL (u+"!/test.txt");51System.out.println ("url2 = " + url2);52JarURLConnection j2 = (JarURLConnection)url2.openConnection();5354j1.setUseCaches (caching);55j2.setUseCaches (caching);5657/* connecting to j2 opens the jar file but does not read it */5859j2.connect ();6061try {62/* attempt to read a non-existing entry in the jar file63* shows the bug, where the jar file is closed after the64* attempt fails.65*/66InputStream is = j1.getInputStream ();67} catch (IOException e) {68System.out.println ("Got expected exception from j1 ");69}7071/* If bug present, this will fail because we think the jar72* is ready to be read, after the connect() above, but we73* get a ZipException because it has been closed74*/75InputStream is = j2.getInputStream ();76readAndClose (is);77System.out.println ("OK");78}7980static void readAndClose (InputStream is) throws IOException {81while (is.read() != -1) ;82is.close();83}8485}868788