FileDocCategorySizeDatePackage
JarOutputStream.javaAPI DocJava SE 6 API4140Tue Jun 10 00:25:58 BST 2008java.util.jar

JarOutputStream

public class JarOutputStream extends ZipOutputStream
The JarOutputStream class is used to write the contents of a JAR file to any output stream. It extends the class java.util.zip.ZipOutputStream with support for writing an optional Manifest entry. The Manifest can be used to specify meta-information about the JAR file and its entries.
author
David Connelly
version
1.24, 01/09/07
see
Manifest
see
java.util.zip.ZipOutputStream
since
1.2

Fields Summary
private static final int
JAR_MAGIC
private boolean
firstEntry
Constructors Summary
public JarOutputStream(OutputStream out, Manifest man)
Creates a new JarOutputStream with the specified Manifest. The manifest is written as the first entry to the output stream.

param
out the actual output stream
param
man the optional Manifest
exception
IOException if an I/O error has occurred


                                                
           
	super(out);
	if (man == null) {
	    throw new NullPointerException("man");
	}
	ZipEntry e = new ZipEntry(JarFile.MANIFEST_NAME);
	putNextEntry(e);
	man.write(new BufferedOutputStream(this));
	closeEntry();
    
public JarOutputStream(OutputStream out)
Creates a new JarOutputStream with no manifest.

param
out the actual output stream
exception
IOException if an I/O error has occurred

	super(out);
    
Methods Summary
private static intget16(byte[] b, int off)

	return (b[off] & 0xff) | ((b[off+1] & 0xff) << 8);
    
private static booleanhasMagic(byte[] edata)


    /*
     * Returns true if specified byte array contains the
     * jar magic extra field id.
     */
         
	try {
	    int i = 0;
	    while (i < edata.length) {
		if (get16(edata, i) == JAR_MAGIC) {
		    return true;
		}
		i += get16(edata, i + 2) + 4;
	    }
	} catch (ArrayIndexOutOfBoundsException e) {
	    // Invalid extra field data
	}
	return false;
    
public voidputNextEntry(java.util.zip.ZipEntry ze)
Begins writing a new JAR file entry and positions the stream to the start of the entry data. This method will also close any previous entry. The default compression method will be used if no compression method was specified for the entry. The current time will be used if the entry has no set modification time.

param
ze the ZIP/JAR entry to be written
exception
ZipException if a ZIP error has occurred
exception
IOException if an I/O error has occurred

	if (firstEntry) {
	    // Make sure that extra field data for first JAR
	    // entry includes JAR magic number id.
	    byte[] edata = ze.getExtra();
            if (edata == null || !hasMagic(edata)) {
                if (edata == null) {
                    edata = new byte[4];
                } else {
                    // Prepend magic to existing extra data
                    byte[] tmp = new byte[edata.length + 4];
                    System.arraycopy(edata, 0, tmp, 4, edata.length);
                    edata = tmp;
                }
                set16(edata, 0, JAR_MAGIC); // extra field id
                set16(edata, 2, 0);         // extra field size
                ze.setExtra(edata);
	    }
	    firstEntry = false;
	}
	super.putNextEntry(ze);
    
private static voidset16(byte[] b, int off, int value)

	b[off+0] = (byte)value;
	b[off+1] = (byte)(value >> 8);