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
  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 
  40 import static jdk.internal.jmod.JmodFile.*;
  41 
  42 /**
  43  * Output stream to write to JMOD file
  44  */
  45 class JmodOutputStream extends OutputStream implements AutoCloseable {
  46     /**
  47      * This method creates (or overrides, if exists) the JMOD file,
  48      * returning the the output stream to write to the JMOD file.
  49      */
  50     static JmodOutputStream newOutputStream(Path file) throws IOException {
  51         OutputStream out = Files.newOutputStream(file);
  52         BufferedOutputStream bos = new BufferedOutputStream(out);
  53         return new JmodOutputStream(bos);
  54     }
  55 
  56     private final ZipOutputStream zos;
  57     private JmodOutputStream(OutputStream out) {
  58         this.zos = new ZipOutputStream(out);
  59         try {
  60             out.write(JMOD_MAGIC_NUMBER);
  61         } catch (IOException e) {
  62             throw new UncheckedIOException(e);
  63         }
  64     }
  65 
  66     /**
  67      * Writes the input stream to the named entry of the given section.
  68      */
  69     public void writeEntry(InputStream in, Section section, String name)
  70         throws IOException
  71     {
  72         ZipEntry ze = newEntry(section, name);
  73         zos.putNextEntry(ze);
  74         in.transferTo(zos);
  75         zos.closeEntry();
  76     }
  77 
  78     /**
  79      * Writes the given bytes to the named entry of the given section.
  80      */
  81     public void writeEntry(byte[] bytes, Section section, String path)
  82         throws IOException
  83     {
  84         ZipEntry ze = newEntry(section, path);
  85         zos.putNextEntry(ze);
  86         zos.write(bytes);
  87         zos.closeEntry();
  88     }
  89 
  90     /**
  91      * Writes the given entry to the given input stream.
  92      */
  93     public void writeEntry(InputStream in, Entry e) throws IOException {
  94         zos.putNextEntry(e.zipEntry());
  95         zos.write(in.readAllBytes());
  96         zos.closeEntry();
  97     }
  98 
  99     private ZipEntry newEntry(Section section, String path) {
 100         String prefix = section.jmodDir();
 101         String name = Paths.get(prefix, path).toString()
 102                            .replace(File.separatorChar, '/');
 103         return new ZipEntry(name);
 104     }
 105 
 106     @Override
 107     public void write(int b) throws IOException {
 108         zos.write(b);
 109     }
 110 
 111     @Override
 112     public void close() throws IOException {
 113         zos.close();
 114     }
 115 }
 116