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