< prev index next >

test/lib/jdk/test/lib/util/JarUtils.java

Print this page
rev 52026 : 8205654: serviceability/dcmd/framework/HelpTest.java timed out
8218705: Test sun/tools/jcmd/TestJcmdDefaults.java fails on Linux
Reviewed-by: sspitsyn, dholmes


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.lib.util;
  25 
  26 import java.io.ByteArrayOutputStream;

  27 import java.io.FileInputStream;
  28 import java.io.FileNotFoundException;
  29 import java.io.FileOutputStream;
  30 import java.io.IOException;

  31 import java.nio.file.Files;
  32 import java.nio.file.InvalidPathException;
  33 import java.nio.file.Path;
  34 import java.nio.file.Paths;

  35 import java.util.Enumeration;
  36 import java.util.HashMap;

  37 import java.util.Map;
  38 import java.util.jar.JarEntry;
  39 import java.util.jar.JarFile;
  40 import java.util.jar.JarOutputStream;
  41 import java.util.jar.Manifest;

  42 
  43 /**
  44  * Common library for various test jar file utility functions.
  45  */
  46 public final class JarUtils {






































  47 
  48     /**
  49      * Create jar file with specified files. If a specified file does not exist,
  50      * a new jar entry will be created with the file name itself as the content.
  51      */
  52     public static void createJar(String dest, String... files)
  53             throws IOException {
  54         try (JarOutputStream jos = new JarOutputStream(
  55                 new FileOutputStream(dest), new Manifest())) {
  56             for (String file : files) {
  57                 System.out.println(String.format("Adding %s to %s",
  58                         file, dest));
  59 
  60                 // add an archive entry, and write a file
  61                 jos.putNextEntry(new JarEntry(file));
  62                 try (FileInputStream fis = new FileInputStream(file)) {
  63                     fis.transferTo(jos);
  64                 } catch (FileNotFoundException e) {
  65                     jos.write(file.getBytes());
  66                 }


 176     }
 177 
 178     private static void updateEntry(JarOutputStream jos, String name, Object content)
 179            throws IOException {
 180         if (content instanceof Boolean) {
 181             if (((Boolean) content).booleanValue()) {
 182                 throw new RuntimeException("Boolean value must be FALSE");
 183             }
 184         } else {
 185             jos.putNextEntry(new JarEntry(name));
 186             if (content instanceof Path) {
 187                 Files.newInputStream((Path) content).transferTo(jos);
 188             } else if (content instanceof byte[]) {
 189                 jos.write((byte[]) content);
 190             } else if (content instanceof String) {
 191                 jos.write(((String) content).getBytes());
 192             } else {
 193                 throw new RuntimeException("Unknown type " + content.getClass());
 194             }
 195         }






















 196     }
 197 }


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.lib.util;
  25 
  26 import java.io.ByteArrayOutputStream;
  27 import java.io.File;
  28 import java.io.FileInputStream;
  29 import java.io.FileNotFoundException;
  30 import java.io.FileOutputStream;
  31 import java.io.IOException;
  32 import java.io.OutputStream;
  33 import java.nio.file.Files;
  34 import java.nio.file.InvalidPathException;
  35 import java.nio.file.Path;
  36 import java.nio.file.Paths;
  37 import java.util.ArrayList;
  38 import java.util.Enumeration;
  39 import java.util.HashMap;
  40 import java.util.List;
  41 import java.util.Map;
  42 import java.util.jar.JarEntry;
  43 import java.util.jar.JarFile;
  44 import java.util.jar.JarOutputStream;
  45 import java.util.jar.Manifest;
  46 import java.util.stream.Stream;
  47 
  48 /**
  49  * Common library for various test jar file utility functions.
  50  */
  51 public final class JarUtils {
  52     private JarUtils() { }
  53 
  54     /**
  55      * Creates a JAR file.
  56      *
  57      * Equivalent to {@code jar cfm <jarfile> <manifest> -C <dir> file...}
  58      *
  59      * The input files are resolved against the given directory. Any input
  60      * files that are directories are processed recursively.
  61      */
  62     public static void createJarFile(Path jarfile, Manifest man, Path dir, Path... files)
  63             throws IOException
  64     {
  65         // create the target directory
  66         Path parent = jarfile.getParent();
  67         if (parent != null) {
  68             Files.createDirectories(parent);
  69         }
  70 
  71         List<Path> entries = findAllRegularFiles(dir, files);
  72 
  73         try (OutputStream out = Files.newOutputStream(jarfile);
  74              JarOutputStream jos = new JarOutputStream(out)) {
  75             if (man != null) {
  76                 JarEntry je = new JarEntry(JarFile.MANIFEST_NAME);
  77                 jos.putNextEntry(je);
  78                 man.write(jos);
  79                 jos.closeEntry();
  80             }
  81 
  82             for (Path entry : entries) {
  83                 String name = toJarEntryName(entry);
  84                 jos.putNextEntry(new JarEntry(name));
  85                 Files.copy(dir.resolve(entry), jos);
  86                 jos.closeEntry();
  87             }
  88         }
  89     }
  90 
  91     /**
  92      * Create jar file with specified files. If a specified file does not exist,
  93      * a new jar entry will be created with the file name itself as the content.
  94      */
  95     public static void createJar(String dest, String... files)
  96             throws IOException {
  97         try (JarOutputStream jos = new JarOutputStream(
  98                 new FileOutputStream(dest), new Manifest())) {
  99             for (String file : files) {
 100                 System.out.println(String.format("Adding %s to %s",
 101                         file, dest));
 102 
 103                 // add an archive entry, and write a file
 104                 jos.putNextEntry(new JarEntry(file));
 105                 try (FileInputStream fis = new FileInputStream(file)) {
 106                     fis.transferTo(jos);
 107                 } catch (FileNotFoundException e) {
 108                     jos.write(file.getBytes());
 109                 }


 219     }
 220 
 221     private static void updateEntry(JarOutputStream jos, String name, Object content)
 222            throws IOException {
 223         if (content instanceof Boolean) {
 224             if (((Boolean) content).booleanValue()) {
 225                 throw new RuntimeException("Boolean value must be FALSE");
 226             }
 227         } else {
 228             jos.putNextEntry(new JarEntry(name));
 229             if (content instanceof Path) {
 230                 Files.newInputStream((Path) content).transferTo(jos);
 231             } else if (content instanceof byte[]) {
 232                 jos.write((byte[]) content);
 233             } else if (content instanceof String) {
 234                 jos.write(((String) content).getBytes());
 235             } else {
 236                 throw new RuntimeException("Unknown type " + content.getClass());
 237             }
 238         }
 239     }
 240 
 241     /**
 242      * Maps a file path to the equivalent name in a JAR file
 243      */
 244     private static String toJarEntryName(Path file) {
 245         Path normalized = file.normalize();
 246         return normalized.subpath(0, normalized.getNameCount())  // drop root
 247                          .toString()
 248                          .replace(File.separatorChar, '/');
 249     }
 250 
 251     private static List<Path> findAllRegularFiles(Path dir, Path[] files) throws IOException {
 252         List<Path> entries = new ArrayList<>();
 253         for (Path file : files) {
 254             try (Stream<Path> stream = Files.find(dir.resolve(file), Integer.MAX_VALUE,
 255                     (p, attrs) -> attrs.isRegularFile())) {
 256                 stream.map(dir::relativize)
 257                       .forEach(entries::add);
 258             }
 259         }
 260         return entries;
 261     }
 262 }
< prev index next >