Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/pack200/PackTestZip64.java
38833 views
/*1* Copyright (c) 2014, 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*/22import java.io.*;23import java.util.ArrayList;24import java.util.Collections;25import java.util.Enumeration;26import java.util.List;27import java.util.jar.JarEntry;28import java.util.jar.JarFile;29import java.util.jar.JarInputStream;30import java.util.jar.JarOutputStream;31import java.util.zip.ZipEntry;32/*33* @test34* @bug 802964635* @summary tests that native unpacker produces the same result as Java one36* @compile -XDignore.symbol.file Utils.java PackTestZip64.java37* @run main PackTestZip6438* @author kizune39*/4041public class PackTestZip64 {42public static void main(String... args) throws Exception {43testPacking();44Utils.cleanup();45}4647// 1KB buffer is enough to copy jar content48private static final byte[] BUFFER = new byte[1024];4950static void testPacking() throws IOException {51// make a copy of the test specimen to local directory52File testFile = new File("tools_java.jar");53// Add a large number of small files to the golden jar54generateLargeJar(testFile, Utils.locateJar("golden.jar"));5556List<String> cmdsList = new ArrayList<>();5758// Repack file to get the Java-based result59cmdsList.add(Utils.getPack200Cmd());60cmdsList.add("--repack");61cmdsList.add(testFile.getName());62Utils.runExec(cmdsList);63cmdsList.clear();6465// Pack file with pack200 and unpack in with unpack20066File packedFile = new File("tools.pack.gz");67cmdsList.add(Utils.getPack200Cmd());68cmdsList.add(packedFile.getName());69cmdsList.add(testFile.getName());70Utils.runExec(cmdsList);71cmdsList.clear();7273File unpackedFile = new File("tools_native.jar");74cmdsList.add(Utils.getUnpack200Cmd());75cmdsList.add(packedFile.getName());76cmdsList.add(unpackedFile.getName());77Utils.runExec(cmdsList);7879// Compare files binary80compareTwoFiles(testFile, unpackedFile);8182// Cleaning up generated files83testFile.delete();84packedFile.delete();85unpackedFile.delete();86}8788static void compareTwoFiles(File src, File dst) throws IOException {89if (!src.exists()) {90throw new IOException("File " + src.getName() + " does not exist!");91}9293if(!dst.exists()) {94throw new IOException("File " + dst.getName() + " does not exist!");95}9697BufferedInputStream srcis, dstis;98srcis = new BufferedInputStream(new FileInputStream(src));99dstis = new BufferedInputStream(new FileInputStream(dst));100101int s = 0, d, pos = 0;102while (s != -1) { // Checking of just one result for EOF is enough103s = srcis.read();104d = dstis.read();105106if (s != d) {107throw new IOException("Files are differ starting at position: "108+ Integer.toHexString(pos));109}110111pos++;112}113114srcis.close();115dstis.close();116}117118static void generateLargeJar(File result, File source) throws IOException {119if (result.exists()) {120result.delete();121}122123try (JarOutputStream copyTo = new JarOutputStream(new FileOutputStream(result));124JarFile srcJar = new JarFile(source)) {125126for (JarEntry je : Collections.list(srcJar.entries())) {127copyTo.putNextEntry(je);128if (!je.isDirectory()) {129copyStream(srcJar.getInputStream(je), copyTo);130}131copyTo.closeEntry();132}133134int many = Short.MAX_VALUE * 2 + 2;135136for (int i = 0 ; i < many ; i++) {137JarEntry e = new JarEntry("F-" + i + ".txt");138copyTo.putNextEntry(e);139}140copyTo.flush();141copyTo.close();142}143}144145static void copyStream(InputStream in, OutputStream out) throws IOException {146int bytesRead;147while ((bytesRead = in.read(BUFFER))!= -1) {148out.write(BUFFER, 0, bytesRead);149}150}151}152153154