/* * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package jdk.testlibrary; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.JarOutputStream; import java.util.jar.Manifest; /** * Common library for various test jar file utility functions. */ public final class JarUtils { private static final int BUF_SIZE = 1024; /** * Create jar file with specified files. */ public static void createJar(String jarFilename, String... files) throws IOException { try (JarOutputStream jos = new JarOutputStream(new FileOutputStream( jarFilename), new Manifest())) { for (String file : files) { System.out.println(String.format("Adding %s to %s", new Object[] {file, jarFilename})); String filename = new File(file).getName(); // add archive entry JarEntry entry = new JarEntry(filename); jos.putNextEntry(entry); // write file try (FileInputStream fis = new FileInputStream(file)) { byte[] buffer = new byte[BUF_SIZE]; while (true) { int read = fis.read(buffer, 0, buffer.length); if (read <= 0) { break; } jos.write(buffer, 0, read); } } } } System.out.println(); } /** * Add specified files to existing jar file. */ public static void updateJar(String srcJarFilename, String destJarFilename, String... files) throws IOException { try (JarOutputStream jos = new JarOutputStream(new FileOutputStream( destJarFilename))) { byte[] buffer = new byte[BUF_SIZE]; int read; for (String file : files) { System.out.println(String.format("Updating %s with %s", new Object[] {destJarFilename, file})); String filename = new File(file).getName(); try (FileInputStream fis = new FileInputStream(file)) { JarEntry entry = new JarEntry(filename); jos.putNextEntry(entry); while ((read = fis.read(buffer)) != -1) { jos.write(buffer, 0, read); } } try (JarFile srcJarFile = new JarFile(srcJarFilename)) { for (Enumeration entries = srcJarFile.entries(); entries. hasMoreElements();) { JarEntry entry = (JarEntry) entries.nextElement(); if (!entry.getName().equals(file)) { InputStream entryStream = srcJarFile.getInputStream( entry); jos.putNextEntry(entry); while ((read = entryStream.read(buffer)) != -1) { jos.write(buffer, 0, read); } } } } } } System.out.println(); } }