< prev index next >

src/jdk.jlink/share/classes/jdk/tools/jmod/JmodOutputStream.java

Print this page
rev 58246 : 8240333: jmod incorrectly updates .jar and .jmod files during hashing
   1 /*
   2  * Copyright (c) 2016, 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


  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         zos.putNextEntry(e.zipEntry());















  96         zos.write(in.readAllBytes());
  97         zos.closeEntry();
  98     }
  99 
 100     private ZipEntry newEntry(Section section, String path) {
 101         String prefix = section.jmodDir();
 102         String name = Paths.get(prefix, path).toString()
 103                            .replace(File.separatorChar, '/');
 104         return new ZipEntry(name);
 105     }
 106 
 107     @Override
 108     public void write(int b) throws IOException {
 109         zos.write(b);
 110     }
 111 
 112     @Override
 113     public void close() throws IOException {
 114         zos.close();
 115     }
   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


  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     }
< prev index next >