rev 58246 : 8240333: jmod incorrectly updates .jar and .jmod files during hashing

   1 /*
   2  * Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.tools.jmod;
  27 
  28 import java.io.BufferedOutputStream;
  29 import java.io.File;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 import java.io.OutputStream;
  33 import java.io.UncheckedIOException;
  34 import java.nio.file.Files;
  35 import java.nio.file.Path;
  36 import java.nio.file.Paths;
  37 import java.util.zip.ZipEntry;
  38 import java.util.zip.ZipOutputStream;
  39 import jdk.internal.jmod.JmodFile;
  40 
  41 import static jdk.internal.jmod.JmodFile.*;
  42 
  43 /**
  44  * Output stream to write to JMOD file
  45  */
  46 class JmodOutputStream extends OutputStream implements AutoCloseable {
  47     /**
  48      * This method creates (or overrides, if exists) the JMOD file,
  49      * returning the the output stream to write to the JMOD file.
  50      */
  51     static JmodOutputStream newOutputStream(Path file) throws IOException {
  52         OutputStream out = Files.newOutputStream(file);
  53         BufferedOutputStream bos = new BufferedOutputStream(out);
  54         return new JmodOutputStream(bos);
  55     }
  56 
  57     private final ZipOutputStream zos;
  58     private JmodOutputStream(OutputStream out) {
  59         this.zos = new ZipOutputStream(out);
  60         try {
  61             JmodFile.writeMagicNumber(out);
  62         } catch (IOException e) {
  63             throw new UncheckedIOException(e);
  64         }
  65     }
  66 
  67     /**
  68      * Writes the input stream to the named entry of the given section.
  69      */
  70     public void writeEntry(InputStream in, Section section, String name)
  71         throws IOException
  72     {
  73         ZipEntry ze = newEntry(section, name);
  74         zos.putNextEntry(ze);
  75         in.transferTo(zos);
  76         zos.closeEntry();
  77     }
  78 
  79     /**
  80      * Writes the given bytes to the named entry of the given section.
  81      */
  82     public void writeEntry(byte[] bytes, Section section, String path)
  83         throws IOException
  84     {
  85         ZipEntry ze = newEntry(section, path);
  86         zos.putNextEntry(ze);
  87         zos.write(bytes);
  88         zos.closeEntry();
  89     }
  90 
  91     /**
  92      * Writes the given entry to the given input stream.
  93      */
  94     public void writeEntry(InputStream in, Entry e) throws IOException {
  95         ZipEntry e1 = e.zipEntry();
  96         // We can't know which exact Deflater and which
  97         // compression level was used to compress the entry.
  98         // Only preserve attributes which won't change by
  99         // inflating and deflating the entry. See:
 100         // sun.tools.jar.Main.update()
 101         ZipEntry e2 = new ZipEntry(e1.getName());
 102         e2.setMethod(e1.getMethod());
 103         e2.setTime(e1.getTime());
 104         e2.setComment(e1.getComment());
 105         e2.setExtra(e1.getExtra());
 106         if (e1.getMethod() == ZipEntry.STORED) {
 107             e2.setSize(e1.getSize());
 108             e2.setCrc(e1.getCrc());
 109         }
 110         zos.putNextEntry(e2);
 111         zos.write(in.readAllBytes());
 112         zos.closeEntry();
 113     }
 114 
 115     private ZipEntry newEntry(Section section, String path) {
 116         String prefix = section.jmodDir();
 117         String name = Paths.get(prefix, path).toString()
 118                            .replace(File.separatorChar, '/');
 119         return new ZipEntry(name);
 120     }
 121 
 122     @Override
 123     public void write(int b) throws IOException {
 124         zos.write(b);
 125     }
 126 
 127     @Override
 128     public void close() throws IOException {
 129         zos.close();
 130     }
 131 }
 132 
--- EOF ---