Path: blob/master/test/jdk/java/net/URLConnection/6212146/Test.java
41153 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*/2223import java.net.URL;24import java.net.URLConnection;25import java.nio.file.Paths;2627public class Test {2829public static void main(String[] args)30throws Exception {31String baseDir = args[0];32String archiveName = args[1];33String lProperty = System.getProperty("do.iterations", "5000");34int lRepetitions = Integer.valueOf(lProperty);35System.out.println("Start creating copys of the archive, "36+ lRepetitions + " times");37for (int i = 0; i < lRepetitions; i++) {38// Copy the given jar file and add a prefix39copyFile(baseDir, archiveName, i);40}41System.out.println("Start opening the archives archive, "42+ lRepetitions + " times");43System.out.println("First URL is jar:" + Paths.get(baseDir,440 + archiveName).toUri() + "!/foo/Test.class");45for (int i = 0; i < lRepetitions; i++) {46// Create URL47String lURLPath = "jar:" + Paths.get(baseDir, i48+ archiveName).toUri() + "!/foo/Test.class";49URL lURL = new URL(lURLPath);50// Open URL Connection51try {52URLConnection lConnection = lURL.openConnection();53lConnection.getInputStream();54} catch (java.io.FileNotFoundException fnfe) {55// Ignore this one because we expect this one56} catch (java.util.zip.ZipException ze) {57throw new RuntimeException("Test failed: " + ze.getMessage());58}59}60}6162private static void copyFile( String pBaseDir, String pArchiveName, int pIndex) {63try {64java.io.File lSource = new java.io.File( pBaseDir, pArchiveName );65java.io.File lDestination = new java.io.File( pBaseDir, pIndex + pArchiveName );66if( !lDestination.exists() ) {67lDestination.createNewFile();68java.io.InputStream lInput = new java.io.FileInputStream( lSource );69java.io.OutputStream lOutput = new java.io.FileOutputStream( lDestination );70byte[] lBuffer = new byte[ 1024 ];71int lLength = -1;72while( ( lLength = lInput.read( lBuffer ) ) > 0 ) {73lOutput.write( lBuffer, 0, lLength );74}75lInput.close();76lOutput.close();77}78} catch( Exception e ) {79e.printStackTrace();80}81}82}838485