test/org/openjdk/jigsaw/cli/ModuleFormatHeaderHashTest.java

Print this page




  72 
  73     void test() throws Exception {
  74         testEmptyModule();
  75     }
  76 
  77     /**
  78      * Get a module file's stored hash.
  79      */
  80     byte [] readHash(String name, String version) throws Exception {
  81         String fname = moduleDir + File.separator + name + "@" + version + ".jmod";
  82         try (FileInputStream fis = new FileInputStream(fname);
  83              DataInputStream in = new DataInputStream(fis);
  84              ModuleFile.Reader r = new ModuleFile.Reader(in);) {
  85              return r.getHash();
  86         }
  87     }
  88 
  89     /**
  90      * Hash a module file (without the file hash in the module file header).
  91      */

  92     byte [] hash(String name, String version, String digest) throws Exception {
  93         String fname = moduleDir + File.separator + name + "@" + version + ".jmod";
  94         MessageDigest md = MessageDigest.getInstance(digest);
  95         try (FileInputStream fis = new FileInputStream(fname);
  96              DigestInputStream dis = new DigestInputStream(fis, md)) {
  97             dis.read(new byte[ModuleFile.ModuleFileHeader.LENGTH_WITHOUT_HASH]);
  98             dis.on(false);
  99             dis.read(new byte [md.getDigestLength()]);
 100             dis.on(true);
 101             for (int c = dis.read() ; c != -1 ; c = dis.read())
 102                 ;
 103             return md.digest();
 104         }
 105     }
 106 
 107     /**
 108      * Compress a module.
 109      */
 110     void compress(String name) throws Exception {
 111         compress(name, false);
 112     }
 113 
 114     void compress(String name, boolean haveNatLibs)
 115         throws Exception {
 116         compress(name, haveNatLibs, false);
 117     }




  72 
  73     void test() throws Exception {
  74         testEmptyModule();
  75     }
  76 
  77     /**
  78      * Get a module file's stored hash.
  79      */
  80     byte [] readHash(String name, String version) throws Exception {
  81         String fname = moduleDir + File.separator + name + "@" + version + ".jmod";
  82         try (FileInputStream fis = new FileInputStream(fname);
  83              DataInputStream in = new DataInputStream(fis);
  84              ModuleFile.Reader r = new ModuleFile.Reader(in);) {
  85              return r.getHash();
  86         }
  87     }
  88 
  89     /**
  90      * Hash a module file (without the file hash in the module file header).
  91      */
  92     static final int LENGTH_WITHOUT_HASH = 30; // computed from module-file format
  93     byte [] hash(String name, String version, String digest) throws Exception {
  94         String fname = moduleDir + File.separator + name + "@" + version + ".jmod";
  95         MessageDigest md = MessageDigest.getInstance(digest);
  96         try (FileInputStream fis = new FileInputStream(fname);
  97              DigestInputStream dis = new DigestInputStream(fis, md)) {
  98             dis.read(new byte[LENGTH_WITHOUT_HASH]);
  99             dis.on(false);
 100             dis.read(new byte [md.getDigestLength()]);
 101             dis.on(true);
 102             for (int c = dis.read() ; c != -1 ; c = dis.read())
 103                 ;
 104             return md.digest();
 105         }
 106     }
 107 
 108     /**
 109      * Compress a module.
 110      */
 111     void compress(String name) throws Exception {
 112         compress(name, false);
 113     }
 114 
 115     void compress(String name, boolean haveNatLibs)
 116         throws Exception {
 117         compress(name, haveNatLibs, false);
 118     }