jdk/test/java/util/jar/JarFile/ScanSignedJar.java
changeset 8553 46c2babb1e44
parent 5506 202f599c92aa
child 9035 1255eb81cc2f
equal deleted inserted replaced
8552:f36470084574 8553:46c2babb1e44
    35 import java.util.jar.*;
    35 import java.util.jar.*;
    36 
    36 
    37 public class ScanSignedJar {
    37 public class ScanSignedJar {
    38 
    38 
    39     public static void main(String[] args) throws Exception {
    39     public static void main(String[] args) throws Exception {
    40         JarFile file = new JarFile(new File(System.getProperty("test.src","."),
       
    41             "bogus-signerinfo-attr.jar"));
       
    42         byte[] buffer = new byte[8192];
       
    43         boolean isSigned = false;
    40         boolean isSigned = false;
       
    41         try (JarFile file = new JarFile(new File(System.getProperty("test.src","."),
       
    42                  "bogus-signerinfo-attr.jar"))) {
       
    43             byte[] buffer = new byte[8192];
    44 
    44 
    45         for (Enumeration entries = file.entries(); entries.hasMoreElements();) {
    45             for (Enumeration entries = file.entries(); entries.hasMoreElements();) {
    46             JarEntry entry = (JarEntry) entries.nextElement();
    46                 JarEntry entry = (JarEntry) entries.nextElement();
    47             InputStream jis = file.getInputStream(entry);
    47                 try (InputStream jis = file.getInputStream(entry)) {
    48             while (jis.read(buffer, 0, buffer.length) != -1) {
    48                     while (jis.read(buffer, 0, buffer.length) != -1) {
    49                 // read the jar entry
    49                         // read the jar entry
       
    50                     }
       
    51                 }
       
    52                 if (entry.getCertificates() != null) {
       
    53                     isSigned = true;
       
    54                 }
       
    55                 System.out.println((isSigned ? "[signed] " : "\t ") +
       
    56                     entry.getName());
    50             }
    57             }
    51             jis.close();
       
    52             if (entry.getCertificates() != null) {
       
    53                 isSigned = true;
       
    54             }
       
    55             System.out.println((isSigned ? "[signed] " : "\t ") +
       
    56                 entry.getName());
       
    57         }
    58         }
    58         file.close();
       
    59 
    59 
    60         if (isSigned) {
    60         if (isSigned) {
    61             System.out.println("\nJAR file has signed entries");
    61             System.out.println("\nJAR file has signed entries");
    62         } else {
    62         } else {
    63             throw new Exception("Failed to detect that the JAR file is signed");
    63             throw new Exception("Failed to detect that the JAR file is signed");