author | redestad |
Mon, 23 Apr 2018 13:32:00 +0200 | |
changeset 49850 | e286fa159ff1 |
parent 49111 | 1b33025ae610 |
child 50413 | 1234ff7199c7 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
48936
13a3013ae0cb
8198539: Cleanup of unused imports in java/util/jar/Attributes.java (java.base) and JdpController.java (jdk.management.agent)
clanger
parents:
47444
diff
changeset
|
2 |
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util.jar; |
|
27 |
||
28 |
import java.io.DataOutputStream; |
|
29 |
import java.io.IOException; |
|
48936
13a3013ae0cb
8198539: Cleanup of unused imports in java/util/jar/Attributes.java (java.base) and JdpController.java (jdk.management.agent)
clanger
parents:
47444
diff
changeset
|
30 |
import java.util.Collection; |
49850 | 31 |
import java.util.HashMap; |
27718
5e08d0fb67c9
8062194: java.util.jar.Attributes should use insertion-ordered iteration
martin
parents:
25859
diff
changeset
|
32 |
import java.util.LinkedHashMap; |
2 | 33 |
import java.util.Map; |
49850 | 34 |
import java.util.Objects; |
2 | 35 |
import java.util.Set; |
48936
13a3013ae0cb
8198539: Cleanup of unused imports in java/util/jar/Attributes.java (java.base) and JdpController.java (jdk.management.agent)
clanger
parents:
47444
diff
changeset
|
36 |
|
3861
a98a057ec335
6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
2
diff
changeset
|
37 |
import sun.util.logging.PlatformLogger; |
2 | 38 |
|
39 |
/** |
|
40 |
* The Attributes class maps Manifest attribute names to associated string |
|
41 |
* values. Valid attribute names are case-insensitive, are restricted to |
|
42 |
* the ASCII characters in the set [0-9a-zA-Z_-], and cannot exceed 70 |
|
49111
1b33025ae610
6372077: JarFile.getManifest() should handle manifest attribute name 70 bytes
rriggs
parents:
48936
diff
changeset
|
43 |
* characters in length. There must be a colon and a SPACE after the name; |
1b33025ae610
6372077: JarFile.getManifest() should handle manifest attribute name 70 bytes
rriggs
parents:
48936
diff
changeset
|
44 |
* the combined length will not exceed 72 characters. |
1b33025ae610
6372077: JarFile.getManifest() should handle manifest attribute name 70 bytes
rriggs
parents:
48936
diff
changeset
|
45 |
* Attribute values can contain any characters and |
2 | 46 |
* will be UTF8-encoded when written to the output stream. See the |
45663
4a0cbf8f2474
8182023: some java.util.jar docs contain links to technotes
psandoz
parents:
36411
diff
changeset
|
47 |
* <a href="{@docRoot}/../specs/jar/jar.html">JAR File Specification</a> |
2 | 48 |
* for more information about valid attribute names and values. |
49 |
* |
|
27718
5e08d0fb67c9
8062194: java.util.jar.Attributes should use insertion-ordered iteration
martin
parents:
25859
diff
changeset
|
50 |
* <p>This map and its views have a predictable iteration order, namely the |
5e08d0fb67c9
8062194: java.util.jar.Attributes should use insertion-ordered iteration
martin
parents:
25859
diff
changeset
|
51 |
* order that keys were inserted into the map, as with {@link LinkedHashMap}. |
5e08d0fb67c9
8062194: java.util.jar.Attributes should use insertion-ordered iteration
martin
parents:
25859
diff
changeset
|
52 |
* |
2 | 53 |
* @author David Connelly |
54 |
* @see Manifest |
|
55 |
* @since 1.2 |
|
56 |
*/ |
|
57 |
public class Attributes implements Map<Object,Object>, Cloneable { |
|
58 |
/** |
|
59 |
* The attribute name-value mappings. |
|
60 |
*/ |
|
61 |
protected Map<Object,Object> map; |
|
62 |
||
63 |
/** |
|
64 |
* Constructs a new, empty Attributes object with default size. |
|
65 |
*/ |
|
66 |
public Attributes() { |
|
67 |
this(11); |
|
68 |
} |
|
69 |
||
70 |
/** |
|
71 |
* Constructs a new, empty Attributes object with the specified |
|
72 |
* initial size. |
|
73 |
* |
|
74 |
* @param size the initial number of attributes |
|
75 |
*/ |
|
76 |
public Attributes(int size) { |
|
27718
5e08d0fb67c9
8062194: java.util.jar.Attributes should use insertion-ordered iteration
martin
parents:
25859
diff
changeset
|
77 |
map = new LinkedHashMap<>(size); |
2 | 78 |
} |
79 |
||
80 |
/** |
|
81 |
* Constructs a new Attributes object with the same attribute name-value |
|
82 |
* mappings as in the specified Attributes. |
|
83 |
* |
|
84 |
* @param attr the specified Attributes |
|
85 |
*/ |
|
86 |
public Attributes(Attributes attr) { |
|
27718
5e08d0fb67c9
8062194: java.util.jar.Attributes should use insertion-ordered iteration
martin
parents:
25859
diff
changeset
|
87 |
map = new LinkedHashMap<>(attr); |
2 | 88 |
} |
89 |
||
90 |
||
91 |
/** |
|
92 |
* Returns the value of the specified attribute name, or null if the |
|
93 |
* attribute name was not found. |
|
94 |
* |
|
95 |
* @param name the attribute name |
|
96 |
* @return the value of the specified attribute name, or null if |
|
97 |
* not found. |
|
98 |
*/ |
|
99 |
public Object get(Object name) { |
|
100 |
return map.get(name); |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Returns the value of the specified attribute name, specified as |
|
105 |
* a string, or null if the attribute was not found. The attribute |
|
106 |
* name is case-insensitive. |
|
107 |
* <p> |
|
108 |
* This method is defined as: |
|
109 |
* <pre> |
|
110 |
* return (String)get(new Attributes.Name((String)name)); |
|
111 |
* </pre> |
|
112 |
* |
|
113 |
* @param name the attribute name as a string |
|
114 |
* @return the String value of the specified attribute name, or null if |
|
115 |
* not found. |
|
116 |
* @throws IllegalArgumentException if the attribute name is invalid |
|
117 |
*/ |
|
118 |
public String getValue(String name) { |
|
49850 | 119 |
return (String)get(Name.of(name)); |
2 | 120 |
} |
121 |
||
122 |
/** |
|
123 |
* Returns the value of the specified Attributes.Name, or null if the |
|
124 |
* attribute was not found. |
|
125 |
* <p> |
|
126 |
* This method is defined as: |
|
127 |
* <pre> |
|
128 |
* return (String)get(name); |
|
129 |
* </pre> |
|
130 |
* |
|
131 |
* @param name the Attributes.Name object |
|
132 |
* @return the String value of the specified Attribute.Name, or null if |
|
133 |
* not found. |
|
134 |
*/ |
|
135 |
public String getValue(Name name) { |
|
136 |
return (String)get(name); |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* Associates the specified value with the specified attribute name |
|
141 |
* (key) in this Map. If the Map previously contained a mapping for |
|
142 |
* the attribute name, the old value is replaced. |
|
143 |
* |
|
144 |
* @param name the attribute name |
|
145 |
* @param value the attribute value |
|
146 |
* @return the previous value of the attribute, or null if none |
|
147 |
* @exception ClassCastException if the name is not a Attributes.Name |
|
148 |
* or the value is not a String |
|
149 |
*/ |
|
150 |
public Object put(Object name, Object value) { |
|
151 |
return map.put((Attributes.Name)name, (String)value); |
|
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* Associates the specified value with the specified attribute name, |
|
156 |
* specified as a String. The attributes name is case-insensitive. |
|
157 |
* If the Map previously contained a mapping for the attribute name, |
|
158 |
* the old value is replaced. |
|
159 |
* <p> |
|
160 |
* This method is defined as: |
|
161 |
* <pre> |
|
162 |
* return (String)put(new Attributes.Name(name), value); |
|
163 |
* </pre> |
|
164 |
* |
|
165 |
* @param name the attribute name as a string |
|
166 |
* @param value the attribute value |
|
167 |
* @return the previous value of the attribute, or null if none |
|
168 |
* @exception IllegalArgumentException if the attribute name is invalid |
|
169 |
*/ |
|
170 |
public String putValue(String name, String value) { |
|
49850 | 171 |
return (String)put(Name.of(name), value); |
2 | 172 |
} |
173 |
||
174 |
/** |
|
175 |
* Removes the attribute with the specified name (key) from this Map. |
|
176 |
* Returns the previous attribute value, or null if none. |
|
177 |
* |
|
178 |
* @param name attribute name |
|
179 |
* @return the previous value of the attribute, or null if none |
|
180 |
*/ |
|
181 |
public Object remove(Object name) { |
|
182 |
return map.remove(name); |
|
183 |
} |
|
184 |
||
185 |
/** |
|
186 |
* Returns true if this Map maps one or more attribute names (keys) |
|
187 |
* to the specified value. |
|
188 |
* |
|
189 |
* @param value the attribute value |
|
190 |
* @return true if this Map maps one or more attribute names to |
|
191 |
* the specified value |
|
192 |
*/ |
|
193 |
public boolean containsValue(Object value) { |
|
194 |
return map.containsValue(value); |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* Returns true if this Map contains the specified attribute name (key). |
|
199 |
* |
|
200 |
* @param name the attribute name |
|
201 |
* @return true if this Map contains the specified attribute name |
|
202 |
*/ |
|
203 |
public boolean containsKey(Object name) { |
|
204 |
return map.containsKey(name); |
|
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* Copies all of the attribute name-value mappings from the specified |
|
209 |
* Attributes to this Map. Duplicate mappings will be replaced. |
|
210 |
* |
|
211 |
* @param attr the Attributes to be stored in this map |
|
212 |
* @exception ClassCastException if attr is not an Attributes |
|
213 |
*/ |
|
214 |
public void putAll(Map<?,?> attr) { |
|
215 |
// ## javac bug? |
|
216 |
if (!Attributes.class.isInstance(attr)) |
|
217 |
throw new ClassCastException(); |
|
218 |
for (Map.Entry<?,?> me : (attr).entrySet()) |
|
219 |
put(me.getKey(), me.getValue()); |
|
220 |
} |
|
221 |
||
222 |
/** |
|
223 |
* Removes all attributes from this Map. |
|
224 |
*/ |
|
225 |
public void clear() { |
|
226 |
map.clear(); |
|
227 |
} |
|
228 |
||
229 |
/** |
|
230 |
* Returns the number of attributes in this Map. |
|
231 |
*/ |
|
232 |
public int size() { |
|
233 |
return map.size(); |
|
234 |
} |
|
235 |
||
236 |
/** |
|
237 |
* Returns true if this Map contains no attributes. |
|
238 |
*/ |
|
239 |
public boolean isEmpty() { |
|
240 |
return map.isEmpty(); |
|
241 |
} |
|
242 |
||
243 |
/** |
|
244 |
* Returns a Set view of the attribute names (keys) contained in this Map. |
|
245 |
*/ |
|
246 |
public Set<Object> keySet() { |
|
247 |
return map.keySet(); |
|
248 |
} |
|
249 |
||
250 |
/** |
|
251 |
* Returns a Collection view of the attribute values contained in this Map. |
|
252 |
*/ |
|
253 |
public Collection<Object> values() { |
|
254 |
return map.values(); |
|
255 |
} |
|
256 |
||
257 |
/** |
|
258 |
* Returns a Collection view of the attribute name-value mappings |
|
259 |
* contained in this Map. |
|
260 |
*/ |
|
261 |
public Set<Map.Entry<Object,Object>> entrySet() { |
|
262 |
return map.entrySet(); |
|
263 |
} |
|
264 |
||
265 |
/** |
|
266 |
* Compares the specified Attributes object with this Map for equality. |
|
267 |
* Returns true if the given object is also an instance of Attributes |
|
268 |
* and the two Attributes objects represent the same mappings. |
|
269 |
* |
|
270 |
* @param o the Object to be compared |
|
271 |
* @return true if the specified Object is equal to this Map |
|
272 |
*/ |
|
273 |
public boolean equals(Object o) { |
|
274 |
return map.equals(o); |
|
275 |
} |
|
276 |
||
277 |
/** |
|
278 |
* Returns the hash code value for this Map. |
|
279 |
*/ |
|
280 |
public int hashCode() { |
|
281 |
return map.hashCode(); |
|
282 |
} |
|
283 |
||
284 |
/** |
|
285 |
* Returns a copy of the Attributes, implemented as follows: |
|
286 |
* <pre> |
|
287 |
* public Object clone() { return new Attributes(this); } |
|
288 |
* </pre> |
|
289 |
* Since the attribute names and values are themselves immutable, |
|
290 |
* the Attributes returned can be safely modified without affecting |
|
291 |
* the original. |
|
292 |
*/ |
|
293 |
public Object clone() { |
|
294 |
return new Attributes(this); |
|
295 |
} |
|
296 |
||
297 |
/* |
|
298 |
* Writes the current attributes to the specified data output stream. |
|
299 |
* XXX Need to handle UTF8 values and break up lines longer than 72 bytes |
|
300 |
*/ |
|
27804
4659e70271c4
8066617: Suppress deprecation warnings in java.base module
darcy
parents:
27799
diff
changeset
|
301 |
@SuppressWarnings("deprecation") |
2 | 302 |
void write(DataOutputStream os) throws IOException { |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
303 |
for (Entry<Object, Object> e : entrySet()) { |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
304 |
StringBuffer buffer = new StringBuffer( |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
305 |
((Name) e.getKey()).toString()); |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
306 |
buffer.append(": "); |
2 | 307 |
|
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
308 |
String value = (String) e.getValue(); |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
309 |
if (value != null) { |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
310 |
byte[] vb = value.getBytes("UTF8"); |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
311 |
value = new String(vb, 0, 0, vb.length); |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
312 |
} |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
313 |
buffer.append(value); |
2 | 314 |
|
49111
1b33025ae610
6372077: JarFile.getManifest() should handle manifest attribute name 70 bytes
rriggs
parents:
48936
diff
changeset
|
315 |
Manifest.make72Safe(buffer); |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
316 |
buffer.append("\r\n"); |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
317 |
os.writeBytes(buffer.toString()); |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
318 |
} |
2 | 319 |
os.writeBytes("\r\n"); |
320 |
} |
|
321 |
||
322 |
/* |
|
323 |
* Writes the current attributes to the specified data output stream, |
|
324 |
* make sure to write out the MANIFEST_VERSION or SIGNATURE_VERSION |
|
325 |
* attributes first. |
|
326 |
* |
|
327 |
* XXX Need to handle UTF8 values and break up lines longer than 72 bytes |
|
328 |
*/ |
|
27804
4659e70271c4
8066617: Suppress deprecation warnings in java.base module
darcy
parents:
27799
diff
changeset
|
329 |
@SuppressWarnings("deprecation") |
2 | 330 |
void writeMain(DataOutputStream out) throws IOException |
331 |
{ |
|
332 |
// write out the *-Version header first, if it exists |
|
333 |
String vername = Name.MANIFEST_VERSION.toString(); |
|
334 |
String version = getValue(vername); |
|
335 |
if (version == null) { |
|
336 |
vername = Name.SIGNATURE_VERSION.toString(); |
|
337 |
version = getValue(vername); |
|
338 |
} |
|
339 |
||
340 |
if (version != null) { |
|
341 |
out.writeBytes(vername+": "+version+"\r\n"); |
|
342 |
} |
|
343 |
||
344 |
// write out all attributes except for the version |
|
345 |
// we wrote out earlier |
|
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
346 |
for (Entry<Object, Object> e : entrySet()) { |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
347 |
String name = ((Name) e.getKey()).toString(); |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
348 |
if ((version != null) && !(name.equalsIgnoreCase(vername))) { |
2 | 349 |
|
350 |
StringBuffer buffer = new StringBuffer(name); |
|
351 |
buffer.append(": "); |
|
352 |
||
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19409
diff
changeset
|
353 |
String value = (String) e.getValue(); |
2 | 354 |
if (value != null) { |
355 |
byte[] vb = value.getBytes("UTF8"); |
|
356 |
value = new String(vb, 0, 0, vb.length); |
|
357 |
} |
|
358 |
buffer.append(value); |
|
359 |
||
49111
1b33025ae610
6372077: JarFile.getManifest() should handle manifest attribute name 70 bytes
rriggs
parents:
48936
diff
changeset
|
360 |
Manifest.make72Safe(buffer); |
2 | 361 |
buffer.append("\r\n"); |
362 |
out.writeBytes(buffer.toString()); |
|
363 |
} |
|
364 |
} |
|
365 |
out.writeBytes("\r\n"); |
|
366 |
} |
|
367 |
||
368 |
/* |
|
369 |
* Reads attributes from the specified input stream. |
|
370 |
* XXX Need to handle UTF8 values. |
|
371 |
*/ |
|
27804
4659e70271c4
8066617: Suppress deprecation warnings in java.base module
darcy
parents:
27799
diff
changeset
|
372 |
@SuppressWarnings("deprecation") |
2 | 373 |
void read(Manifest.FastInputStream is, byte[] lbuf) throws IOException { |
49850 | 374 |
String name = null, value; |
2 | 375 |
byte[] lastline = null; |
376 |
||
377 |
int len; |
|
378 |
while ((len = is.readLine(lbuf)) != -1) { |
|
379 |
boolean lineContinued = false; |
|
380 |
if (lbuf[--len] != '\n') { |
|
381 |
throw new IOException("line too long"); |
|
382 |
} |
|
383 |
if (len > 0 && lbuf[len-1] == '\r') { |
|
384 |
--len; |
|
385 |
} |
|
386 |
if (len == 0) { |
|
387 |
break; |
|
388 |
} |
|
389 |
int i = 0; |
|
390 |
if (lbuf[0] == ' ') { |
|
391 |
// continuation of previous line |
|
392 |
if (name == null) { |
|
393 |
throw new IOException("misplaced continuation line"); |
|
394 |
} |
|
395 |
lineContinued = true; |
|
396 |
byte[] buf = new byte[lastline.length + len - 1]; |
|
397 |
System.arraycopy(lastline, 0, buf, 0, lastline.length); |
|
398 |
System.arraycopy(lbuf, 1, buf, lastline.length, len - 1); |
|
399 |
if (is.peek() == ' ') { |
|
400 |
lastline = buf; |
|
401 |
continue; |
|
402 |
} |
|
403 |
value = new String(buf, 0, buf.length, "UTF8"); |
|
404 |
lastline = null; |
|
405 |
} else { |
|
406 |
while (lbuf[i++] != ':') { |
|
407 |
if (i >= len) { |
|
408 |
throw new IOException("invalid header field"); |
|
409 |
} |
|
410 |
} |
|
411 |
if (lbuf[i++] != ' ') { |
|
412 |
throw new IOException("invalid header field"); |
|
413 |
} |
|
414 |
name = new String(lbuf, 0, 0, i - 2); |
|
415 |
if (is.peek() == ' ') { |
|
416 |
lastline = new byte[len - i]; |
|
417 |
System.arraycopy(lbuf, i, lastline, 0, len - i); |
|
418 |
continue; |
|
419 |
} |
|
420 |
value = new String(lbuf, i, len - i, "UTF8"); |
|
421 |
} |
|
422 |
try { |
|
423 |
if ((putValue(name, value) != null) && (!lineContinued)) { |
|
3861
a98a057ec335
6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
2
diff
changeset
|
424 |
PlatformLogger.getLogger("java.util.jar").warning( |
2 | 425 |
"Duplicate name in Manifest: " + name |
426 |
+ ".\n" |
|
427 |
+ "Ensure that the manifest does not " |
|
428 |
+ "have duplicate entries, and\n" |
|
429 |
+ "that blank lines separate " |
|
430 |
+ "individual sections in both your\n" |
|
431 |
+ "manifest and in the META-INF/MANIFEST.MF " |
|
432 |
+ "entry in the jar file."); |
|
433 |
} |
|
434 |
} catch (IllegalArgumentException e) { |
|
435 |
throw new IOException("invalid header field name: " + name); |
|
436 |
} |
|
437 |
} |
|
438 |
} |
|
439 |
||
440 |
/** |
|
441 |
* The Attributes.Name class represents an attribute name stored in |
|
442 |
* this Map. Valid attribute names are case-insensitive, are restricted |
|
443 |
* to the ASCII characters in the set [0-9a-zA-Z_-], and cannot exceed |
|
444 |
* 70 characters in length. Attribute values can contain any characters |
|
445 |
* and will be UTF8-encoded when written to the output stream. See the |
|
45663
4a0cbf8f2474
8182023: some java.util.jar docs contain links to technotes
psandoz
parents:
36411
diff
changeset
|
446 |
* <a href="{@docRoot}/../specs/jar/jar.html">JAR File Specification</a> |
2 | 447 |
* for more information about valid attribute names and values. |
448 |
*/ |
|
449 |
public static class Name { |
|
49850 | 450 |
private final String name; |
451 |
private final int hashCode; |
|
452 |
||
453 |
/** |
|
454 |
* Avoid allocation for common Names |
|
455 |
*/ |
|
456 |
private static final Map<String, Name> KNOWN_NAMES; |
|
457 |
||
458 |
static final Name of(String name) { |
|
459 |
Name n = KNOWN_NAMES.get(name); |
|
460 |
if (n != null) { |
|
461 |
return n; |
|
462 |
} |
|
463 |
return new Name(name); |
|
464 |
} |
|
2 | 465 |
|
466 |
/** |
|
467 |
* Constructs a new attribute name using the given string name. |
|
468 |
* |
|
469 |
* @param name the attribute string name |
|
470 |
* @exception IllegalArgumentException if the attribute name was |
|
471 |
* invalid |
|
472 |
* @exception NullPointerException if the attribute name was null |
|
473 |
*/ |
|
474 |
public Name(String name) { |
|
49850 | 475 |
this.hashCode = hash(name); |
2 | 476 |
this.name = name.intern(); |
477 |
} |
|
478 |
||
49850 | 479 |
// Checks the string is valid |
480 |
private final int hash(String name) { |
|
481 |
Objects.requireNonNull(name, "name"); |
|
2 | 482 |
int len = name.length(); |
483 |
if (len > 70 || len == 0) { |
|
49850 | 484 |
throw new IllegalArgumentException(name); |
2 | 485 |
} |
49850 | 486 |
// Calculate hash code case insensitively |
487 |
int h = 0; |
|
2 | 488 |
for (int i = 0; i < len; i++) { |
49850 | 489 |
char c = name.charAt(i); |
490 |
if (c >= 'a' && c <= 'z') { |
|
491 |
// hashcode must be identical for upper and lower case |
|
492 |
h = h * 31 + (c - 0x20); |
|
493 |
} else if ((c >= 'A' && c <= 'Z' || |
|
494 |
c >= '0' && c <= '9' || |
|
495 |
c == '_' || c == '-')) { |
|
496 |
h = h * 31 + c; |
|
497 |
} else { |
|
498 |
throw new IllegalArgumentException(name); |
|
2 | 499 |
} |
500 |
} |
|
49850 | 501 |
return h; |
2 | 502 |
} |
503 |
||
504 |
/** |
|
505 |
* Compares this attribute name to another for equality. |
|
506 |
* @param o the object to compare |
|
507 |
* @return true if this attribute name is equal to the |
|
508 |
* specified attribute object |
|
509 |
*/ |
|
510 |
public boolean equals(Object o) { |
|
49850 | 511 |
if (this == o) { |
512 |
return true; |
|
513 |
} |
|
2 | 514 |
if (o instanceof Name) { |
49850 | 515 |
Name other = (Name)o; |
516 |
return other.name.equalsIgnoreCase(name); |
|
2 | 517 |
} else { |
518 |
return false; |
|
519 |
} |
|
520 |
} |
|
521 |
||
522 |
/** |
|
523 |
* Computes the hash value for this attribute name. |
|
524 |
*/ |
|
525 |
public int hashCode() { |
|
526 |
return hashCode; |
|
527 |
} |
|
528 |
||
529 |
/** |
|
530 |
* Returns the attribute name as a String. |
|
531 |
*/ |
|
532 |
public String toString() { |
|
533 |
return name; |
|
534 |
} |
|
535 |
||
536 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
537 |
* {@code Name} object for {@code Manifest-Version} |
2 | 538 |
* manifest attribute. This attribute indicates the version number |
539 |
* of the manifest standard to which a JAR file's manifest conforms. |
|
45663
4a0cbf8f2474
8182023: some java.util.jar docs contain links to technotes
psandoz
parents:
36411
diff
changeset
|
540 |
* @see <a href="{@docRoot}/../specs/jar/jar.html#JAR_Manifest"> |
2 | 541 |
* Manifest and Signature Specification</a> |
542 |
*/ |
|
543 |
public static final Name MANIFEST_VERSION = new Name("Manifest-Version"); |
|
544 |
||
545 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
546 |
* {@code Name} object for {@code Signature-Version} |
2 | 547 |
* manifest attribute used when signing JAR files. |
45663
4a0cbf8f2474
8182023: some java.util.jar docs contain links to technotes
psandoz
parents:
36411
diff
changeset
|
548 |
* @see <a href="{@docRoot}/../specs/jar/jar.html#JAR_Manifest"> |
2 | 549 |
* Manifest and Signature Specification</a> |
550 |
*/ |
|
551 |
public static final Name SIGNATURE_VERSION = new Name("Signature-Version"); |
|
552 |
||
553 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
554 |
* {@code Name} object for {@code Content-Type} |
2 | 555 |
* manifest attribute. |
556 |
*/ |
|
557 |
public static final Name CONTENT_TYPE = new Name("Content-Type"); |
|
558 |
||
559 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
560 |
* {@code Name} object for {@code Class-Path} |
27565 | 561 |
* manifest attribute. |
45663
4a0cbf8f2474
8182023: some java.util.jar docs contain links to technotes
psandoz
parents:
36411
diff
changeset
|
562 |
* @see <a href="{@docRoot}/../specs/jar/jar.html#classpath"> |
27565 | 563 |
* JAR file specification</a> |
2 | 564 |
*/ |
565 |
public static final Name CLASS_PATH = new Name("Class-Path"); |
|
566 |
||
567 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
568 |
* {@code Name} object for {@code Main-Class} manifest |
2 | 569 |
* attribute used for launching applications packaged in JAR files. |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
570 |
* The {@code Main-Class} attribute is used in conjunction |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
571 |
* with the {@code -jar} command-line option of the |
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
572 |
* {@code java} application launcher. |
2 | 573 |
*/ |
574 |
public static final Name MAIN_CLASS = new Name("Main-Class"); |
|
575 |
||
576 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
577 |
* {@code Name} object for {@code Sealed} manifest attribute |
2 | 578 |
* used for sealing. |
47444
b6da56767057
8183901: Fix broken links to "Package Sealing" in the JAR spec
bchristi
parents:
47216
diff
changeset
|
579 |
* @see <a href="{@docRoot}/../specs/jar/jar.html#package-sealing"> |
27565 | 580 |
* Package Sealing</a> |
2 | 581 |
*/ |
582 |
public static final Name SEALED = new Name("Sealed"); |
|
583 |
||
49850 | 584 |
/** |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
585 |
* {@code Name} object for {@code Extension-List} manifest attribute |
27565 | 586 |
* used for the extension mechanism that is no longer supported. |
2 | 587 |
*/ |
588 |
public static final Name EXTENSION_LIST = new Name("Extension-List"); |
|
589 |
||
590 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
591 |
* {@code Name} object for {@code Extension-Name} manifest attribute. |
27565 | 592 |
* used for the extension mechanism that is no longer supported. |
2 | 593 |
*/ |
594 |
public static final Name EXTENSION_NAME = new Name("Extension-Name"); |
|
595 |
||
596 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
597 |
* {@code Name} object for {@code Extension-Installation} manifest attribute. |
27565 | 598 |
* |
599 |
* @deprecated Extension mechanism is no longer supported. |
|
2 | 600 |
*/ |
27565 | 601 |
@Deprecated |
2 | 602 |
public static final Name EXTENSION_INSTALLATION = new Name("Extension-Installation"); |
603 |
||
604 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
605 |
* {@code Name} object for {@code Implementation-Title} |
2 | 606 |
* manifest attribute used for package versioning. |
607 |
*/ |
|
608 |
public static final Name IMPLEMENTATION_TITLE = new Name("Implementation-Title"); |
|
609 |
||
610 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
611 |
* {@code Name} object for {@code Implementation-Version} |
2 | 612 |
* manifest attribute used for package versioning. |
613 |
*/ |
|
614 |
public static final Name IMPLEMENTATION_VERSION = new Name("Implementation-Version"); |
|
615 |
||
616 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
617 |
* {@code Name} object for {@code Implementation-Vendor} |
2 | 618 |
* manifest attribute used for package versioning. |
619 |
*/ |
|
620 |
public static final Name IMPLEMENTATION_VENDOR = new Name("Implementation-Vendor"); |
|
621 |
||
622 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
623 |
* {@code Name} object for {@code Implementation-Vendor-Id} |
27565 | 624 |
* manifest attribute. |
625 |
* |
|
626 |
* @deprecated Extension mechanism is no longer supported. |
|
2 | 627 |
*/ |
27565 | 628 |
@Deprecated |
2 | 629 |
public static final Name IMPLEMENTATION_VENDOR_ID = new Name("Implementation-Vendor-Id"); |
630 |
||
49850 | 631 |
/** |
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
632 |
* {@code Name} object for {@code Implementation-URL} |
27565 | 633 |
* manifest attribute. |
634 |
* |
|
635 |
* @deprecated Extension mechanism is no longer supported. |
|
2 | 636 |
*/ |
27565 | 637 |
@Deprecated |
2 | 638 |
public static final Name IMPLEMENTATION_URL = new Name("Implementation-URL"); |
639 |
||
640 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
641 |
* {@code Name} object for {@code Specification-Title} |
2 | 642 |
* manifest attribute used for package versioning. |
643 |
*/ |
|
644 |
public static final Name SPECIFICATION_TITLE = new Name("Specification-Title"); |
|
645 |
||
646 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
647 |
* {@code Name} object for {@code Specification-Version} |
2 | 648 |
* manifest attribute used for package versioning. |
649 |
*/ |
|
650 |
public static final Name SPECIFICATION_VERSION = new Name("Specification-Version"); |
|
651 |
||
652 |
/** |
|
32037
ab4526f4ac10
8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents:
27804
diff
changeset
|
653 |
* {@code Name} object for {@code Specification-Vendor} |
2 | 654 |
* manifest attribute used for package versioning. |
655 |
*/ |
|
656 |
public static final Name SPECIFICATION_VENDOR = new Name("Specification-Vendor"); |
|
36129
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
32037
diff
changeset
|
657 |
|
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
32037
diff
changeset
|
658 |
/** |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
32037
diff
changeset
|
659 |
* {@code Name} object for {@code Multi-Release} |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
32037
diff
changeset
|
660 |
* manifest attribute that indicates this is a multi-release JAR file. |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
32037
diff
changeset
|
661 |
* |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
32037
diff
changeset
|
662 |
* @since 9 |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
32037
diff
changeset
|
663 |
*/ |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
32037
diff
changeset
|
664 |
public static final Name MULTI_RELEASE = new Name("Multi-Release"); |
49850 | 665 |
|
666 |
private static void addName(Map<String, Name> names, Name name) { |
|
667 |
names.put(name.name, name); |
|
668 |
} |
|
669 |
||
670 |
static { |
|
671 |
var names = new HashMap<String, Name>(64); |
|
672 |
addName(names, MANIFEST_VERSION); |
|
673 |
addName(names, SIGNATURE_VERSION); |
|
674 |
addName(names, CONTENT_TYPE); |
|
675 |
addName(names, CLASS_PATH); |
|
676 |
addName(names, MAIN_CLASS); |
|
677 |
addName(names, SEALED); |
|
678 |
addName(names, EXTENSION_LIST); |
|
679 |
addName(names, EXTENSION_NAME); |
|
680 |
addName(names, IMPLEMENTATION_TITLE); |
|
681 |
addName(names, IMPLEMENTATION_VERSION); |
|
682 |
addName(names, IMPLEMENTATION_VENDOR); |
|
683 |
addName(names, SPECIFICATION_TITLE); |
|
684 |
addName(names, SPECIFICATION_VERSION); |
|
685 |
addName(names, SPECIFICATION_VENDOR); |
|
686 |
addName(names, MULTI_RELEASE); |
|
687 |
||
688 |
// Common attributes used in MANIFEST.MF et.al; adding these has a |
|
689 |
// small footprint cost, but is likely to be quickly paid for by |
|
690 |
// reducing allocation when reading and parsing typical manifests |
|
691 |
addName(names, new Name("Add-Exports")); |
|
692 |
addName(names, new Name("Add-Opens")); |
|
693 |
addName(names, new Name("Ant-Version")); |
|
694 |
addName(names, new Name("Archiver-Version")); |
|
695 |
addName(names, new Name("Build-Jdk")); |
|
696 |
addName(names, new Name("Built-By")); |
|
697 |
addName(names, new Name("Bnd-LastModified")); |
|
698 |
addName(names, new Name("Bundle-Description")); |
|
699 |
addName(names, new Name("Bundle-DocURL")); |
|
700 |
addName(names, new Name("Bundle-License")); |
|
701 |
addName(names, new Name("Bundle-ManifestVersion")); |
|
702 |
addName(names, new Name("Bundle-Name")); |
|
703 |
addName(names, new Name("Bundle-Vendor")); |
|
704 |
addName(names, new Name("Bundle-Version")); |
|
705 |
addName(names, new Name("Bundle-SymbolicName")); |
|
706 |
addName(names, new Name("Created-By")); |
|
707 |
addName(names, new Name("Export-Package")); |
|
708 |
addName(names, new Name("Import-Package")); |
|
709 |
addName(names, new Name("Name")); |
|
710 |
addName(names, new Name("SHA1-Digest")); |
|
711 |
addName(names, new Name("X-Compile-Source-JDK")); |
|
712 |
addName(names, new Name("X-Compile-Target-JDK")); |
|
713 |
KNOWN_NAMES = names; |
|
714 |
} |
|
2 | 715 |
} |
716 |
} |