JarArchive.java revision 15746:efaf8263c116
162587Sitojun/* 295023Ssuz * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 362587Sitojun * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4139823Simp * 554263Sshin * This code is free software; you can redistribute it and/or modify it 654263Sshin * under the terms of the GNU General Public License version 2 only, as 754263Sshin * published by the Free Software Foundation. Oracle designates this 854263Sshin * particular file as subject to the "Classpath" exception as provided 954263Sshin * by Oracle in the LICENSE file that accompanied this code. 1054263Sshin * 1154263Sshin * This code is distributed in the hope that it will be useful, but WITHOUT 1254263Sshin * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1354263Sshin * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1454263Sshin * version 2 for more details (a copy is included in the LICENSE file that 1554263Sshin * accompanied this code). 1654263Sshin * 1754263Sshin * You should have received a copy of the GNU General Public License version 1854263Sshin * 2 along with this work; if not, write to the Free Software Foundation, 1954263Sshin * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2054263Sshin * 2154263Sshin * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2254263Sshin * or visit www.oracle.com if you need additional information or have any 2354263Sshin * questions. 2454263Sshin */ 2554263Sshin 2654263Sshinpackage jdk.tools.jlink.internal; 2754263Sshin 2854263Sshinimport java.io.IOException; 2954263Sshinimport java.io.InputStream; 3054263Sshinimport java.io.UncheckedIOException; 3154263Sshinimport java.nio.file.Path; 3254263Sshinimport java.util.Objects; 3354263Sshinimport java.util.stream.Stream; 3454263Sshinimport java.util.zip.ZipEntry; 3554263Sshinimport java.util.zip.ZipFile; 3654263Sshinimport jdk.tools.jlink.internal.Archive.Entry.EntryType; 3754263Sshin 38219206Sbz/** 3954263Sshin * An Archive backed by a jar file. 4054263Sshin */ 4154263Sshinpublic abstract class JarArchive implements Archive { 42129880Sphk 4354263Sshin /** 4454263Sshin * An entry located in a jar file. 4554263Sshin */ 4654263Sshin public class JarEntry extends Entry { 4791270Sbrooks 4854263Sshin private final long size; 49193664Shrs private final ZipEntry entry; 50178888Sjulian private final ZipFile file; 5162587Sitojun 5279106Sbrooks JarEntry(String path, String name, EntryType type, ZipFile file, ZipEntry entry) { 5354263Sshin super(JarArchive.this, path, name, type); 5454263Sshin this.entry = Objects.requireNonNull(entry); 5554263Sshin this.file = Objects.requireNonNull(file); 56130933Sbrooks size = entry.getSize(); 5754263Sshin } 5854263Sshin 5954263Sshin /** 6054263Sshin * Returns the number of uncompressed bytes for this entry. 61196019Srwatson */ 6254263Sshin @Override 6354263Sshin public long size() { 6454263Sshin return size; 6578064Sume } 6678064Sume 6754263Sshin @Override 6854263Sshin public InputStream stream() throws IOException { 6979106Sbrooks return file.getInputStream(entry); 7054263Sshin } 7154263Sshin } 7254263Sshin 7354263Sshin private final Path file; 7454263Sshin private final String moduleName; 7554263Sshin // currently processed ZipFile 7654263Sshin protected ZipFile zipFile; 7754263Sshin 7854263Sshin protected JarArchive(String mn, Path file) { 79148385Sume Objects.requireNonNull(mn); 8054263Sshin Objects.requireNonNull(file); 8162587Sitojun this.moduleName = mn; 8254263Sshin this.file = file; 8354263Sshin } 8462587Sitojun 85153621Sthompsa @Override 86153621Sthompsa public String moduleName() { 8754263Sshin return moduleName; 8854263Sshin } 89163606Srwatson 90163606Srwatson @Override 9179106Sbrooks public Path getPath() { 9262587Sitojun return file; 93127305Srwatson } 94127898Sru 95127305Srwatson @Override 96127305Srwatson public Stream<Entry> entries() { 9779106Sbrooks try { 98215701Sdim if (zipFile == null) { 99195727Srwatson open(); 100195699Srwatson } 10183998Sbrooks } catch (IOException ioe) { 10283998Sbrooks throw new UncheckedIOException(ioe); 10383998Sbrooks } 10483998Sbrooks return zipFile.stream().map(this::toEntry).filter(n -> n != null); 10583998Sbrooks } 106153621Sthompsa 107160195Ssam abstract EntryType toEntryType(String entryName); 108128209Sbrooks 10979106Sbrooks abstract String getFileName(String entryName); 110130933Sbrooks 11179106Sbrooks abstract Entry toEntry(ZipEntry ze); 11292725Salfred 11379106Sbrooks @Override 11491270Sbrooks public void close() throws IOException { 115248085Smarius if (zipFile != null) { 11691270Sbrooks zipFile.close(); 11762587Sitojun } 11862587Sitojun } 11991270Sbrooks 12062587Sitojun @Override 12162587Sitojun public void open() throws IOException { 12262587Sitojun if (zipFile != null) { 12395023Ssuz zipFile.close(); 12462587Sitojun } 12562587Sitojun zipFile = new ZipFile(file.toFile()); 12662587Sitojun } 12762587Sitojun} 128215701Sdim