1 /*
2 * Copyright (c) 1997, 2012, 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
35 * for writing an optional <code>Manifest</code> entry. The
36 * <code>Manifest</code> can be used to specify meta-information about
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 /**
49 * Creates a new <code>JarOutputStream</code> with the specified
50 * <code>Manifest</code>. The manifest is written as the first
51 * entry to the output stream.
52 *
53 * @param out the actual output stream
54 * @param man the optional <code>Manifest</code>
55 * @exception IOException if an I/O error has occurred
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 /**
69 * Creates a new <code>JarOutputStream</code> with no manifest.
70 * @param out the actual output stream
71 * @exception IOException if an I/O error has occurred
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
86 * @exception ZipException if a ZIP error has occurred
87 * @exception IOException if an I/O error has occurred
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;
|
1 /*
2 * Copyright (c) 1997, 2019, 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
35 * for writing an optional <code>Manifest</code> entry. The
36 * <code>Manifest</code> can be used to specify meta-information about
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 /**
49 * Creates a new <code>JarOutputStream</code> with the specified
50 * <code>Manifest</code>. The manifest is written as the first
51 * entry to the output stream.
52 *
53 * @param out the actual output stream
54 * @param man the optional <code>Manifest</code>
55 * @throws IOException if an I/O error has occurred
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 /**
69 * Creates a new <code>JarOutputStream</code> with no manifest.
70 * @param out the actual output stream
71 * @throws IOException if an I/O error has occurred
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
86 * @throws ZipException if a ZIP error has occurred
87 * @throws IOException if an I/O error has occurred
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;
|