Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLConnection/6212146/Test.java
38828 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.*;24import java.io.*;2526public class Test {2728public static void main(String[] args)29throws Exception {30String BASE_DIR = args[0];31String ARCHIVE_NAME = args[1];32String lProperty = System.getProperty( "do.iterations", "5000" );33int lRepetitions = new Integer( lProperty ).intValue();34System.out.println ( "Start creating copys of the archive, " + lRepetitions + " times" );35for( int i = 0; i < lRepetitions; i++ ) {36// Copy the given jar file and add a prefix37copyFile( BASE_DIR, ARCHIVE_NAME, i);38}39System.out.println ( "Start opening the archives archive, " + lRepetitions + " times" );40System.out.println ( "First URL is jar:file://" + BASE_DIR + "1" + ARCHIVE_NAME + "!/foo/Test.class");41for( int i = 0; i < lRepetitions; i++ ) {42// Create ULR43String lURLPath = "jar:file://" + BASE_DIR + i + ARCHIVE_NAME + "!/foo/Test.class";44URL lURL = new URL( lURLPath );45// Open URL Connection46try {47URLConnection lConnection = lURL.openConnection();48lConnection.getInputStream();49} catch( java.io.FileNotFoundException fnfe ) {50// Ignore this one because we expect this one51} catch( java.util.zip.ZipException ze ) {52throw new RuntimeException ("Test failed: " + ze.getMessage());53}54}55//System.out.println ( "Done testing, waiting 20 seconds for checking" );56//System.out.println ( "Cleaning up");57//for( int i = 0; i < lRepetitions; i++ ) {58// Copy the given jar file and add a prefix59//deleteFile( BASE_DIR, i, ARCHIVE_NAME);60////}61}6263private static void deleteFile (String BASE_DIR, int pIndex, String pArchiveName) {64java.io.File file = new java.io.File (BASE_DIR, pIndex + pArchiveName );65file.delete ();66}6768private static void copyFile( String pBaseDir, String pArchiveName, int pIndex) {69try {70java.io.File lSource = new java.io.File( pBaseDir, pArchiveName );71java.io.File lDestination = new java.io.File( pBaseDir, pIndex + pArchiveName );72if( !lDestination.exists() ) {73lDestination.createNewFile();74java.io.InputStream lInput = new java.io.FileInputStream( lSource );75java.io.OutputStream lOutput = new java.io.FileOutputStream( lDestination );76byte[] lBuffer = new byte[ 1024 ];77int lLength = -1;78while( ( lLength = lInput.read( lBuffer ) ) > 0 ) {79lOutput.write( lBuffer, 0, lLength );80}81lInput.close();82lOutput.close();83}84} catch( Exception e ) {85e.printStackTrace();86}87}88}899091