1 /*
   2  *  Copyright (c) 2019, 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.
   8  *
   9  *  This code is distributed in the hope that it will be useful, but WITHOUT
  10  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  *  version 2 for more details (a copy is included in the LICENSE file that
  13  *  accompanied this code).
  14  *
  15  *  You should have received a copy of the GNU General Public License version
  16  *  2 along with this work; if not, write to the Free Software Foundation,
  17  *  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  *  Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  *  or visit www.oracle.com if you need additional information or have any
  21  *  questions.
  22  */
  23 
  24 package com.sun.tools.jextract;
  25 
  26 import java.io.*;
  27 import java.nio.file.Files;
  28 import java.nio.file.Path;
  29 import java.util.jar.JarOutputStream;
  30 import java.util.logging.Level;
  31 import java.util.zip.ZipEntry;
  32 
  33 import static java.nio.file.StandardOpenOption.*;
  34 
  35 // Utility class to generate a .jmod file
  36 public final class JarWriter {
  37     private final Log log;
  38 
  39     private final Writer writer;
  40 
  41     public JarWriter(Context ctx, Writer writer) {
  42         this.log = ctx.log;
  43         this.writer = writer;
  44     }
  45 
  46     public void writeJarFile(Path jarpath, String[] args) throws IOException {
  47         log.print(Level.INFO, () -> "Collecting jar file " + jarpath);
  48         try (OutputStream os = Files.newOutputStream(jarpath, CREATE, TRUNCATE_EXISTING, WRITE);
  49                 JarOutputStream jar = new JarOutputStream(os)) {
  50             writeJarFile(jar, args);
  51         }
  52     }
  53 
  54     public void writeJarFile(JarOutputStream jar, String[] args) {
  55         writer.results().forEach((cls, bytes) -> {
  56                 try {
  57                     String path = cls.replace('.', File.separatorChar) + ".class";
  58                     log.print(Level.FINE, () -> "Add " + path);
  59                     jar.putNextEntry(new ZipEntry(path));
  60                     jar.write(bytes);
  61                     jar.closeEntry();
  62                 } catch (IOException ioe) {
  63                     throw new UncheckedIOException(ioe);
  64                 }
  65             });
  66 
  67             try {
  68                 jar.putNextEntry(new ZipEntry(Writer.JEXTRACT_MANIFEST));
  69                 jar.write(writer.getJextractProperties(args));
  70                 jar.closeEntry();
  71             } catch (IOException ioe) {
  72                 throw new UncheckedIOException(ioe);
  73             }
  74     }
  75 }