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