author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 41828 | jdk/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JarArchive.java@0436ea0c6099 |
child 47737 | acfedb75dd51 |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
2 |
* Copyright (c) 2014, 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.jlink.internal; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.io.UncheckedIOException; |
|
31 |
import java.nio.file.Path; |
|
32 |
import java.util.Objects; |
|
41828
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
33 |
import java.util.jar.JarFile; |
36511 | 34 |
import java.util.stream.Stream; |
35 |
import java.util.zip.ZipEntry; |
|
36 |
import java.util.zip.ZipFile; |
|
41828
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
37 |
import jdk.internal.util.jar.VersionedStream; |
36511 | 38 |
import jdk.tools.jlink.internal.Archive.Entry.EntryType; |
39 |
||
40 |
/** |
|
41 |
* An Archive backed by a jar file. |
|
42 |
*/ |
|
43 |
public abstract class JarArchive implements Archive { |
|
44 |
||
45 |
/** |
|
46 |
* An entry located in a jar file. |
|
47 |
*/ |
|
41352 | 48 |
public class JarEntry extends Entry { |
36511 | 49 |
|
50 |
private final long size; |
|
51 |
private final ZipEntry entry; |
|
52 |
private final ZipFile file; |
|
53 |
||
54 |
JarEntry(String path, String name, EntryType type, ZipFile file, ZipEntry entry) { |
|
55 |
super(JarArchive.this, path, name, type); |
|
39308 | 56 |
this.entry = Objects.requireNonNull(entry); |
57 |
this.file = Objects.requireNonNull(file); |
|
36511 | 58 |
size = entry.getSize(); |
59 |
} |
|
60 |
||
61 |
/** |
|
62 |
* Returns the number of uncompressed bytes for this entry. |
|
63 |
*/ |
|
64 |
@Override |
|
65 |
public long size() { |
|
66 |
return size; |
|
67 |
} |
|
68 |
||
69 |
@Override |
|
70 |
public InputStream stream() throws IOException { |
|
71 |
return file.getInputStream(entry); |
|
72 |
} |
|
73 |
} |
|
74 |
||
75 |
private final Path file; |
|
76 |
private final String moduleName; |
|
41828
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
77 |
// currently processed JarFile |
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
78 |
private JarFile jarFile; |
36511 | 79 |
|
80 |
protected JarArchive(String mn, Path file) { |
|
81 |
Objects.requireNonNull(mn); |
|
82 |
Objects.requireNonNull(file); |
|
83 |
this.moduleName = mn; |
|
84 |
this.file = file; |
|
85 |
} |
|
86 |
||
87 |
@Override |
|
88 |
public String moduleName() { |
|
89 |
return moduleName; |
|
90 |
} |
|
91 |
||
92 |
@Override |
|
93 |
public Path getPath() { |
|
94 |
return file; |
|
95 |
} |
|
96 |
||
97 |
@Override |
|
98 |
public Stream<Entry> entries() { |
|
99 |
try { |
|
41828
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
100 |
if (jarFile == null) { |
36511 | 101 |
open(); |
102 |
} |
|
103 |
} catch (IOException ioe) { |
|
104 |
throw new UncheckedIOException(ioe); |
|
105 |
} |
|
41828
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
106 |
return VersionedStream.stream(jarFile) |
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
107 |
.filter(je -> !je.isDirectory()) |
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
108 |
.map(this::toEntry); |
36511 | 109 |
} |
110 |
||
111 |
abstract EntryType toEntryType(String entryName); |
|
112 |
||
113 |
abstract String getFileName(String entryName); |
|
114 |
||
41352 | 115 |
abstract Entry toEntry(ZipEntry ze); |
36511 | 116 |
|
117 |
@Override |
|
118 |
public void close() throws IOException { |
|
41828
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
119 |
if (jarFile != null) { |
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
120 |
jarFile.close(); |
36511 | 121 |
} |
122 |
} |
|
123 |
||
124 |
@Override |
|
125 |
public void open() throws IOException { |
|
41828
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
126 |
if (jarFile != null) { |
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
127 |
jarFile.close(); |
36511 | 128 |
} |
41828
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
129 |
jarFile = new JarFile(file.toFile(), true, ZipFile.OPEN_READ, JarFile.runtimeVersion()); |
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
130 |
} |
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
131 |
|
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
132 |
protected JarFile getJarFile() { |
0436ea0c6099
8156499: Update jlink to support creating images with modules that are packaged as multi-release JARs
sdrach
parents:
41352
diff
changeset
|
133 |
return jarFile; |
36511 | 134 |
} |
135 |
} |