author | jboes |
Tue, 24 Sep 2019 09:43:43 +0100 | |
changeset 58288 | 48e480e56aad |
parent 58242 | 94bb65cb37d3 |
child 58679 | 9c3209ff7550 |
child 59201 | b24f4caa1411 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1997, 2019, 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.util.zip.*; |
|
29 |
import java.io.*; |
|
30 |
||
31 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
32 |
* The {@code JarOutputStream} class is used to write the contents |
2 | 33 |
* of a JAR file to any output stream. It extends the class |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
34 |
* {@code java.util.zip.ZipOutputStream} with support |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
35 |
* for writing an optional {@code Manifest} entry. The |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
36 |
* {@code Manifest} can be used to specify meta-information about |
2 | 37 |
* the JAR file and its entries. |
38 |
* |
|
39 |
* @author David Connelly |
|
40 |
* @see Manifest |
|
41 |
* @see java.util.zip.ZipOutputStream |
|
42 |
* @since 1.2 |
|
43 |
*/ |
|
44 |
public |
|
45 |
class JarOutputStream extends ZipOutputStream { |
|
46 |
private static final int JAR_MAGIC = 0xCAFE; |
|
47 |
||
48 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
49 |
* Creates a new {@code JarOutputStream} with the specified |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
50 |
* {@code Manifest}. The manifest is written as the first |
2 | 51 |
* entry to the output stream. |
52 |
* |
|
53 |
* @param out the actual output stream |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
54 |
* @param man the optional {@code Manifest} |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
55 |
* @throws IOException if an I/O error has occurred |
2 | 56 |
*/ |
57 |
public JarOutputStream(OutputStream out, Manifest man) throws IOException { |
|
58 |
super(out); |
|
59 |
if (man == null) { |
|
60 |
throw new NullPointerException("man"); |
|
61 |
} |
|
62 |
ZipEntry e = new ZipEntry(JarFile.MANIFEST_NAME); |
|
63 |
putNextEntry(e); |
|
64 |
man.write(new BufferedOutputStream(this)); |
|
65 |
closeEntry(); |
|
66 |
} |
|
67 |
||
68 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
69 |
* Creates a new {@code JarOutputStream} with no manifest. |
2 | 70 |
* @param out the actual output stream |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
71 |
* @throws IOException if an I/O error has occurred |
2 | 72 |
*/ |
73 |
public JarOutputStream(OutputStream out) throws IOException { |
|
74 |
super(out); |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Begins writing a new JAR file entry and positions the stream |
|
79 |
* to the start of the entry data. This method will also close |
|
80 |
* any previous entry. The default compression method will be |
|
81 |
* used if no compression method was specified for the entry. |
|
82 |
* The current time will be used if the entry has no set modification |
|
83 |
* time. |
|
84 |
* |
|
85 |
* @param ze the ZIP/JAR entry to be written |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
86 |
* @throws ZipException if a ZIP error has occurred |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
47216
diff
changeset
|
87 |
* @throws IOException if an I/O error has occurred |
2 | 88 |
*/ |
89 |
public void putNextEntry(ZipEntry ze) throws IOException { |
|
90 |
if (firstEntry) { |
|
91 |
// Make sure that extra field data for first JAR |
|
92 |
// entry includes JAR magic number id. |
|
93 |
byte[] edata = ze.getExtra(); |
|
94 |
if (edata == null || !hasMagic(edata)) { |
|
95 |
if (edata == null) { |
|
96 |
edata = new byte[4]; |
|
97 |
} else { |
|
98 |
// Prepend magic to existing extra data |
|
99 |
byte[] tmp = new byte[edata.length + 4]; |
|
100 |
System.arraycopy(edata, 0, tmp, 4, edata.length); |
|
101 |
edata = tmp; |
|
102 |
} |
|
103 |
set16(edata, 0, JAR_MAGIC); // extra field id |
|
104 |
set16(edata, 2, 0); // extra field size |
|
105 |
ze.setExtra(edata); |
|
106 |
} |
|
107 |
firstEntry = false; |
|
108 |
} |
|
109 |
super.putNextEntry(ze); |
|
110 |
} |
|
111 |
||
112 |
private boolean firstEntry = true; |
|
113 |
||
114 |
/* |
|
115 |
* Returns true if specified byte array contains the |
|
116 |
* jar magic extra field id. |
|
117 |
*/ |
|
118 |
private static boolean hasMagic(byte[] edata) { |
|
119 |
try { |
|
120 |
int i = 0; |
|
121 |
while (i < edata.length) { |
|
122 |
if (get16(edata, i) == JAR_MAGIC) { |
|
123 |
return true; |
|
124 |
} |
|
125 |
i += get16(edata, i + 2) + 4; |
|
126 |
} |
|
127 |
} catch (ArrayIndexOutOfBoundsException e) { |
|
128 |
// Invalid extra field data |
|
129 |
} |
|
130 |
return false; |
|
131 |
} |
|
132 |
||
133 |
/* |
|
134 |
* Fetches unsigned 16-bit value from byte array at specified offset. |
|
135 |
* The bytes are assumed to be in Intel (little-endian) byte order. |
|
136 |
*/ |
|
137 |
private static int get16(byte[] b, int off) { |
|
11828
590711df7828
7143629: JDK jar/zip code should use unsigned library support
darcy
parents:
5506
diff
changeset
|
138 |
return Byte.toUnsignedInt(b[off]) | ( Byte.toUnsignedInt(b[off+1]) << 8); |
2 | 139 |
} |
140 |
||
141 |
/* |
|
142 |
* Sets 16-bit value at specified offset. The bytes are assumed to |
|
143 |
* be in Intel (little-endian) byte order. |
|
144 |
*/ |
|
145 |
private static void set16(byte[] b, int off, int value) { |
|
146 |
b[off+0] = (byte)value; |
|
147 |
b[off+1] = (byte)(value >> 8); |
|
148 |
} |
|
149 |
} |