FileDocCategorySizeDatePackage
JarOutputStream.javaAPI DocJava SE 5 API3944Fri Aug 26 14:57:26 BST 2005java.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.22, 12/19/03
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)) {
		// Prepend magic to existing extra data
		byte[] tmp = new byte[edata.length + 4];
		System.arraycopy(tmp, 4, edata, 0, edata.length);
		edata = tmp;
	    } else {
		edata = new byte[4];
	    }
	    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);