0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util.jar;
0N/A
0N/Aimport java.util.zip.*;
0N/Aimport java.io.*;
0N/A
0N/A/**
0N/A * The <code>JarOutputStream</code> class is used to write the contents
0N/A * of a JAR file to any output stream. It extends the class
0N/A * <code>java.util.zip.ZipOutputStream</code> with support
0N/A * for writing an optional <code>Manifest</code> entry. The
0N/A * <code>Manifest</code> can be used to specify meta-information about
0N/A * the JAR file and its entries.
0N/A *
0N/A * @author David Connelly
0N/A * @see Manifest
0N/A * @see java.util.zip.ZipOutputStream
0N/A * @since 1.2
0N/A */
0N/Apublic
0N/Aclass JarOutputStream extends ZipOutputStream {
0N/A private static final int JAR_MAGIC = 0xCAFE;
0N/A
0N/A /**
0N/A * Creates a new <code>JarOutputStream</code> with the specified
0N/A * <code>Manifest</code>. The manifest is written as the first
0N/A * entry to the output stream.
0N/A *
0N/A * @param out the actual output stream
0N/A * @param man the optional <code>Manifest</code>
0N/A * @exception IOException if an I/O error has occurred
0N/A */
0N/A public JarOutputStream(OutputStream out, Manifest man) throws IOException {
0N/A super(out);
0N/A if (man == null) {
0N/A throw new NullPointerException("man");
0N/A }
0N/A ZipEntry e = new ZipEntry(JarFile.MANIFEST_NAME);
0N/A putNextEntry(e);
0N/A man.write(new BufferedOutputStream(this));
0N/A closeEntry();
0N/A }
0N/A
0N/A /**
0N/A * Creates a new <code>JarOutputStream</code> with no manifest.
0N/A * @param out the actual output stream
0N/A * @exception IOException if an I/O error has occurred
0N/A */
0N/A public JarOutputStream(OutputStream out) throws IOException {
0N/A super(out);
0N/A }
0N/A
0N/A /**
0N/A * Begins writing a new JAR file entry and positions the stream
0N/A * to the start of the entry data. This method will also close
0N/A * any previous entry. The default compression method will be
0N/A * used if no compression method was specified for the entry.
0N/A * The current time will be used if the entry has no set modification
0N/A * time.
0N/A *
0N/A * @param ze the ZIP/JAR entry to be written
0N/A * @exception ZipException if a ZIP error has occurred
0N/A * @exception IOException if an I/O error has occurred
0N/A */
0N/A public void putNextEntry(ZipEntry ze) throws IOException {
0N/A if (firstEntry) {
0N/A // Make sure that extra field data for first JAR
0N/A // entry includes JAR magic number id.
0N/A byte[] edata = ze.getExtra();
0N/A if (edata == null || !hasMagic(edata)) {
0N/A if (edata == null) {
0N/A edata = new byte[4];
0N/A } else {
0N/A // Prepend magic to existing extra data
0N/A byte[] tmp = new byte[edata.length + 4];
0N/A System.arraycopy(edata, 0, tmp, 4, edata.length);
0N/A edata = tmp;
0N/A }
0N/A set16(edata, 0, JAR_MAGIC); // extra field id
0N/A set16(edata, 2, 0); // extra field size
0N/A ze.setExtra(edata);
0N/A }
0N/A firstEntry = false;
0N/A }
0N/A super.putNextEntry(ze);
0N/A }
0N/A
0N/A private boolean firstEntry = true;
0N/A
0N/A /*
0N/A * Returns true if specified byte array contains the
0N/A * jar magic extra field id.
0N/A */
0N/A private static boolean hasMagic(byte[] edata) {
0N/A try {
0N/A int i = 0;
0N/A while (i < edata.length) {
0N/A if (get16(edata, i) == JAR_MAGIC) {
0N/A return true;
0N/A }
0N/A i += get16(edata, i + 2) + 4;
0N/A }
0N/A } catch (ArrayIndexOutOfBoundsException e) {
0N/A // Invalid extra field data
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /*
0N/A * Fetches unsigned 16-bit value from byte array at specified offset.
0N/A * The bytes are assumed to be in Intel (little-endian) byte order.
0N/A */
0N/A private static int get16(byte[] b, int off) {
0N/A return (b[off] & 0xff) | ((b[off+1] & 0xff) << 8);
0N/A }
0N/A
0N/A /*
0N/A * Sets 16-bit value at specified offset. The bytes are assumed to
0N/A * be in Intel (little-endian) byte order.
0N/A */
0N/A private static void set16(byte[] b, int off, int value) {
0N/A b[off+0] = (byte)value;
0N/A b[off+1] = (byte)(value >> 8);
0N/A }
0N/A}