src/jdk.jlink/share/classes/jdk/tools/jmod/JmodOutputStream.java
changeset 47216 71c04702a3d5
parent 42468 7a9555a7e080
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     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 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         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     }
       
   116 }
       
   117