test/jdk/sun/security/tools/jarsigner/Utils.java
changeset 57488 94691d8e746f
parent 47216 71c04702a3d5
equal deleted inserted replaced
57487:643978a35f6e 57488:94691d8e746f
     1 /*
     1 /*
     2  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    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
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
       
    24 import java.io.ByteArrayOutputStream;
    24 import java.io.File;
    25 import java.io.File;
       
    26 import java.io.InputStream;
    25 import java.io.IOException;
    27 import java.io.IOException;
       
    28 import java.util.jar.JarFile;
       
    29 import java.util.jar.Manifest;
       
    30 import java.util.zip.ZipFile;
       
    31 
       
    32 import static java.nio.charset.StandardCharsets.UTF_8;
    26 
    33 
    27 /**
    34 /**
    28  * Helper class.
    35  * Helper class.
    29  */
    36  */
    30 public class Utils {
    37 public class Utils {
    33         for (String filename : filenames) {
    40         for (String filename : filenames) {
    34             new File(filename).createNewFile();
    41             new File(filename).createNewFile();
    35         }
    42         }
    36     }
    43     }
    37 
    44 
       
    45     static void printNonPrintableCharactersEscaped(byte[] manifest)
       
    46             throws IOException {
       
    47         // keep byte sequences encoding multi-byte UTF-8 encoded and composite
       
    48         // characters together as much as possible before decoding into a String
       
    49         ByteArrayOutputStream lineBuf = new ByteArrayOutputStream();
       
    50         for (int i = 0; i < manifest.length; i++) {
       
    51             switch (manifest[i]) {
       
    52             case '\t':
       
    53                 lineBuf.write("\\t".getBytes(UTF_8));
       
    54                 break;
       
    55             case '\r':
       
    56                 lineBuf.write("\\r".getBytes(UTF_8));
       
    57                 if (i + 1 >= manifest.length || manifest[i + 1] != '\n') {
       
    58                     System.out.println(lineBuf.toString(UTF_8));
       
    59                     lineBuf.reset();
       
    60                 }
       
    61                 break;
       
    62             case '\n':
       
    63                 lineBuf.write("\\n".getBytes(UTF_8));
       
    64                 System.out.println(lineBuf.toString(UTF_8));
       
    65                 lineBuf.reset();
       
    66                 break;
       
    67             default:
       
    68                 lineBuf.write(manifest[i]);
       
    69             }
       
    70         }
       
    71         if (lineBuf.size() > 0) {
       
    72             System.out.println(lineBuf.toString(UTF_8));
       
    73         }
       
    74     }
       
    75 
       
    76     static void echoManifest(byte[] manifest, String msg) throws IOException {
       
    77         System.out.println("-".repeat(72));
       
    78         System.out.println(msg);
       
    79         System.out.println("-".repeat(72));
       
    80         printNonPrintableCharactersEscaped(manifest);
       
    81         System.out.println("-".repeat(72));
       
    82     }
       
    83 
       
    84     static byte[] readJarManifestBytes(String jarFilename) throws IOException {
       
    85         return readJarEntryBytes(jarFilename, JarFile.MANIFEST_NAME);
       
    86     }
       
    87 
       
    88     static byte[] readJarEntryBytes(String jarFilename, String jarEntryname)
       
    89             throws IOException {
       
    90         try (
       
    91             ZipFile jar = new ZipFile(jarFilename);
       
    92             InputStream is = jar.getInputStream(jar.getEntry(jarEntryname));
       
    93         ) {
       
    94             return is.readAllBytes();
       
    95         }
       
    96     }
       
    97 
       
    98     static String escapeStringWithNumbers(String lineBreak) {
       
    99         String escaped = "";
       
   100         byte[] bytes = lineBreak.getBytes(UTF_8);
       
   101         for (int i = 0; i < bytes.length; i++) {
       
   102             escaped += bytes[i];
       
   103         }
       
   104         return escaped;
       
   105     }
       
   106 
    38 }
   107 }
    39