Path: blob/master/test/jdk/java/net/URLClassLoader/B7050028.java
41152 views
/*1* Copyright (c) 2011, 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*/2223import java.io.File;24import java.io.FileOutputStream;25import java.io.InputStream;26import java.io.OutputStream;27import java.net.URL;28import java.net.URLClassLoader;29import java.net.URLConnection;30import java.util.zip.CRC32;31import java.util.zip.ZipEntry;32import java.util.zip.ZipOutputStream;3334/**35* @test36* @bug 705002837* @summary ISE "zip file closed" from JarURLConnection.getInputStream on JDK 7 when !useCaches38* @run main/othervm B705002839*/4041public class B7050028 {42public static void main(String[] args) throws Exception {43URLConnection conn = B7050028.class.getResource("B7050028.class").openConnection();44int len = conn.getContentLength();45byte[] data = new byte[len];46InputStream is = conn.getInputStream();47is.read(data);48is.close();49conn.setDefaultUseCaches(false);50File jar = File.createTempFile("B7050028", ".jar");51jar.deleteOnExit();52OutputStream os = new FileOutputStream(jar);53ZipOutputStream zos = new ZipOutputStream(os);54ZipEntry ze = new ZipEntry("B7050028.class");55ze.setMethod(ZipEntry.STORED);56ze.setSize(len);57CRC32 crc = new CRC32();58crc.update(data);59ze.setCrc(crc.getValue());60zos.putNextEntry(ze);61zos.write(data, 0, len);62zos.closeEntry();63zos.finish();64zos.close();65os.close();66System.out.println(new URLClassLoader(new URL[] {new URL("jar:" + jar.toURI() + "!/")}, ClassLoader.getSystemClassLoader().getParent()).loadClass(B7050028.class.getName()));67}68private B7050028() {}69}707172