1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package java.util.jar;
19
20import java.io.IOException;
21import java.io.OutputStream;
22import java.util.zip.ZipEntry;
23import java.util.zip.ZipOutputStream;
24
25/**
26 * The {@code JarOutputStream} is used to write data in the {@code JarFile}
27 * format to an arbitrary output stream
28 */
29public class JarOutputStream extends ZipOutputStream {
30
31    private Manifest manifest;
32
33    /**
34     * Constructs a new {@code JarOutputStream} using an output stream. The
35     * content of the {@code Manifest} must match the JAR entry information
36     * written subsequently to the stream.
37     *
38     * @param os
39     *            the {@code OutputStream} to write to
40     * @param manifest
41     *            the {@code Manifest} to output for this JAR file.
42     * @throws IOException
43     *             if an error occurs creating the {@code JarOutputStream}.
44     */
45    public JarOutputStream(OutputStream os, Manifest manifest) throws IOException {
46        super(os);
47        if (manifest == null) {
48            throw new NullPointerException("manifest == null");
49        }
50        this.manifest = manifest;
51        ZipEntry ze = new ZipEntry(JarFile.MANIFEST_NAME);
52        putNextEntry(ze);
53        this.manifest.write(this);
54        closeEntry();
55    }
56
57    /**
58     * Constructs a new {@code JarOutputStream} using an arbitrary output
59     * stream.
60     *
61     * @param os
62     *            the {@code OutputStream} to write to.
63     * @throws IOException
64     *             if an error occurs creating the {@code JarOutputStream}.
65     */
66    public JarOutputStream(OutputStream os) throws IOException {
67        super(os);
68    }
69
70    /**
71     * Writes the specified ZIP entry to the underlying stream. The previous
72     * entry is closed if it is still open.
73     *
74     * @param ze
75     *            the {@code ZipEntry} to write to.
76     * @throws IOException
77     *             if an error occurs writing to the entry.
78     * @see ZipEntry
79     */
80    @Override
81    public void putNextEntry(ZipEntry ze) throws IOException {
82        super.putNextEntry(ze);
83    }
84}
85