author | mchung |
Mon, 12 Dec 2016 18:56:50 -0800 | |
changeset 42670 | d833113eb7d7 |
parent 42468 | 7a9555a7e080 |
child 43712 | 5dfd0950317c |
permissions | -rw-r--r-- |
41352 | 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.internal.jmod; |
|
27 |
||
28 |
import java.io.BufferedInputStream; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.InputStream; |
|
42468 | 31 |
import java.io.OutputStream; |
41352 | 32 |
import java.nio.file.Files; |
33 |
import java.nio.file.Path; |
|
42670
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
34 |
import java.util.Arrays; |
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
35 |
import java.util.Map; |
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
36 |
import java.util.function.Function; |
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
37 |
import java.util.stream.Collectors; |
41352 | 38 |
import java.util.stream.Stream; |
39 |
import java.util.zip.ZipEntry; |
|
40 |
import java.util.zip.ZipFile; |
|
41 |
||
42 |
/** |
|
43 |
* Helper class to read JMOD file |
|
44 |
*/ |
|
45 |
public class JmodFile implements AutoCloseable { |
|
46 |
// jmod magic number and version number |
|
42468 | 47 |
private static final int JMOD_MAJOR_VERSION = 0x01; |
48 |
private static final int JMOD_MINOR_VERSION = 0x00; |
|
49 |
private static final byte[] JMOD_MAGIC_NUMBER = { |
|
41352 | 50 |
0x4A, 0x4D, /* JM */ |
51 |
JMOD_MAJOR_VERSION, JMOD_MINOR_VERSION, /* version 1.0 */ |
|
52 |
}; |
|
53 |
||
54 |
public static void checkMagic(Path file) throws IOException { |
|
55 |
try (InputStream in = Files.newInputStream(file); |
|
56 |
BufferedInputStream bis = new BufferedInputStream(in)) { |
|
57 |
// validate the header |
|
58 |
byte[] magic = new byte[4]; |
|
59 |
bis.read(magic); |
|
60 |
if (magic[0] != JMOD_MAGIC_NUMBER[0] || |
|
61 |
magic[1] != JMOD_MAGIC_NUMBER[1]) { |
|
62 |
throw new IOException("Invalid jmod file: " + file.toString()); |
|
63 |
} |
|
64 |
if (magic[2] > JMOD_MAJOR_VERSION || |
|
65 |
(magic[2] == JMOD_MAJOR_VERSION && magic[3] > JMOD_MINOR_VERSION)) { |
|
66 |
throw new IOException("Unsupported jmod version: " + |
|
67 |
magic[2] + "." + magic[3] + " in " + file.toString()); |
|
68 |
} |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
/** |
|
73 |
* JMOD sections |
|
74 |
*/ |
|
75 |
public static enum Section { |
|
76 |
CLASSES("classes"), |
|
41561
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41352
diff
changeset
|
77 |
CONFIG("conf"), |
0c6942d13f2e
8167558: Add new JMOD section for header files and man pages
mchung
parents:
41352
diff
changeset
|
78 |
HEADER_FILES("include"), |
42670
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
79 |
LEGAL_NOTICES("legal"), |
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
80 |
MAN_PAGES("man"), |
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
81 |
NATIVE_LIBS("native"), |
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
82 |
NATIVE_CMDS("bin"); |
41352 | 83 |
|
84 |
private final String jmodDir; |
|
85 |
private Section(String jmodDir) { |
|
86 |
this.jmodDir = jmodDir; |
|
87 |
} |
|
88 |
||
89 |
/** |
|
90 |
* Returns the directory name in the JMOD file corresponding to |
|
91 |
* this section |
|
92 |
*/ |
|
93 |
public String jmodDir() { return jmodDir; } |
|
94 |
} |
|
95 |
||
96 |
/** |
|
97 |
* JMOD file entry. |
|
98 |
* |
|
99 |
* Each entry corresponds to a ZipEntry whose name is: |
|
100 |
* Section::jmodDir + '/' + name |
|
101 |
*/ |
|
102 |
public static class Entry { |
|
103 |
private final ZipEntry zipEntry; |
|
104 |
private final Section section; |
|
105 |
private final String name; |
|
106 |
||
107 |
private Entry(ZipEntry e) { |
|
108 |
String name = e.getName(); |
|
109 |
int i = name.indexOf('/'); |
|
110 |
if (i <= 1) { |
|
111 |
throw new RuntimeException("invalid jmod entry: " + name); |
|
112 |
} |
|
113 |
||
114 |
this.zipEntry = e; |
|
42670
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
115 |
this.section = section(name.substring(0, i)); |
41352 | 116 |
this.name = name.substring(i+1); |
117 |
} |
|
118 |
||
119 |
/** |
|
120 |
* Returns the section of this entry. |
|
121 |
*/ |
|
122 |
public Section section() { |
|
123 |
return section; |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Returns the name of this entry. |
|
128 |
*/ |
|
129 |
public String name() { |
|
130 |
return name; |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Returns the size of this entry. |
|
135 |
*/ |
|
136 |
public long size() { |
|
137 |
return zipEntry.getSize(); |
|
138 |
} |
|
139 |
||
140 |
public ZipEntry zipEntry() { |
|
141 |
return zipEntry; |
|
142 |
} |
|
143 |
||
144 |
@Override |
|
145 |
public String toString() { |
|
146 |
return section.jmodDir() + "/" + name; |
|
147 |
} |
|
148 |
||
42670
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
149 |
/* |
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
150 |
* A map from the jmodDir name to Section |
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
151 |
*/ |
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
152 |
static final Map<String, Section> NAME_TO_SECTION = |
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
153 |
Arrays.stream(Section.values()) |
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
154 |
.collect(Collectors.toMap(Section::jmodDir, Function.identity())); |
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
155 |
|
41352 | 156 |
static Section section(String name) { |
42670
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
157 |
if (!NAME_TO_SECTION.containsKey(name)) { |
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
158 |
throw new IllegalArgumentException("invalid section: " + name); |
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
159 |
|
41352 | 160 |
} |
42670
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
161 |
return NAME_TO_SECTION.get(name); |
41352 | 162 |
} |
42670
d833113eb7d7
8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
42468
diff
changeset
|
163 |
|
41352 | 164 |
} |
165 |
||
166 |
private final Path file; |
|
167 |
private final ZipFile zipfile; |
|
168 |
||
169 |
/** |
|
170 |
* Constructs a {@code JmodFile} from a given path. |
|
171 |
*/ |
|
172 |
public JmodFile(Path file) throws IOException { |
|
173 |
checkMagic(file); |
|
174 |
this.file = file; |
|
175 |
this.zipfile = new ZipFile(file.toFile()); |
|
176 |
} |
|
177 |
||
42468 | 178 |
public static void writeMagicNumber(OutputStream os) throws IOException { |
179 |
os.write(JMOD_MAGIC_NUMBER); |
|
180 |
} |
|
181 |
||
41352 | 182 |
/** |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
183 |
* Returns the {@code Entry} for a resource in a JMOD file section |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
184 |
* or {@code null} if not found. |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
185 |
*/ |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
186 |
public Entry getEntry(Section section, String name) { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
187 |
String entry = section.jmodDir() + "/" + name; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
188 |
ZipEntry ze = zipfile.getEntry(entry); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
189 |
return (ze != null) ? new Entry(ze) : null; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
190 |
} |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
191 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
192 |
/** |
41352 | 193 |
* Opens an {@code InputStream} for reading the named entry of the given |
194 |
* section in this jmod file. |
|
195 |
* |
|
196 |
* @throws IOException if the named entry is not found, or I/O error |
|
197 |
* occurs when reading it |
|
198 |
*/ |
|
199 |
public InputStream getInputStream(Section section, String name) |
|
200 |
throws IOException |
|
201 |
{ |
|
202 |
String entry = section.jmodDir() + "/" + name; |
|
203 |
ZipEntry e = zipfile.getEntry(entry); |
|
204 |
if (e == null) { |
|
205 |
throw new IOException(name + " not found: " + file); |
|
206 |
} |
|
207 |
return zipfile.getInputStream(e); |
|
208 |
} |
|
209 |
||
210 |
/** |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
211 |
* Opens an {@code InputStream} for reading an entry in the JMOD file. |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
212 |
* |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
213 |
* @throws IOException if an I/O error occurs |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
214 |
*/ |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
215 |
public InputStream getInputStream(Entry entry) throws IOException { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
216 |
return zipfile.getInputStream(entry.zipEntry()); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
217 |
} |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
218 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41561
diff
changeset
|
219 |
/** |
41352 | 220 |
* Returns a stream of non-directory entries in this jmod file. |
221 |
*/ |
|
222 |
public Stream<Entry> stream() { |
|
223 |
return zipfile.stream() |
|
224 |
.filter(e -> !e.isDirectory()) |
|
225 |
.map(Entry::new); |
|
226 |
} |
|
227 |
||
228 |
@Override |
|
229 |
public void close() throws IOException { |
|
230 |
if (zipfile != null) { |
|
231 |
zipfile.close(); |
|
232 |
} |
|
233 |
} |
|
234 |
} |