FileDocCategorySizeDatePackage
Manifest.javaAPI DocJava SE 5 API10605Fri Aug 26 14:57:26 BST 2005java.util.jar

Manifest

public class Manifest extends Object implements Cloneable
The Manifest class is used to maintain Manifest entry names and their associated Attributes. There are main Manifest Attributes as well as per-entry Attributes. For information on the Manifest format, please see the Manifest format specification.
author
David Connelly
version
1.45, 05/05/04
see
Attributes
since
1.2

Fields Summary
private Attributes
attr
private Map
entries
Constructors Summary
public Manifest()
Constructs a new, empty Manifest.


              
      
    
public Manifest(InputStream is)
Constructs a new Manifest from the specified input stream.

param
is the input stream containing manifest data
throws
IOException if an I/O error has occured

	read(is);
    
public Manifest(Manifest man)
Constructs a new Manifest that is a copy of the specified Manifest.

param
man the Manifest to copy

	attr.putAll(man.getMainAttributes());
	entries.putAll(man.getEntries());
    
Methods Summary
public voidclear()
Clears the main Attributes as well as the entries in this Manifest.

	attr.clear();
	entries.clear();
    
public java.lang.Objectclone()
Returns a shallow copy of this Manifest. The shallow copy is implemented as follows:
public Object clone() { return new Manifest(this); }

return
a shallow copy of this Manifest

	return new Manifest(this);
    
public booleanequals(java.lang.Object o)
Returns true if the specified Object is also a Manifest and has the same main Attributes and entries.

param
o the object to be compared
return
true if the specified Object is also a Manifest and has the same main Attributes and entries

	if (o instanceof Manifest) {
	    Manifest m = (Manifest)o;
	    return attr.equals(m.getMainAttributes()) &&
		   entries.equals(m.getEntries());
	} else {
	    return false;
	}
    
public java.util.jar.AttributesgetAttributes(java.lang.String name)
Returns the Attributes for the specified entry name. This method is defined as:
return (Attributes)getEntries().get(name)

param
name entry name
return
the Attributes for the specified entry name

	return (Attributes)getEntries().get(name);
    
public java.util.MapgetEntries()
Returns a Map of the entries contained in this Manifest. Each entry is represented by a String name (key) and associated Attributes (value).

return
a Map of the entries contained in this Manifest

	return entries;
    
public java.util.jar.AttributesgetMainAttributes()
Returns the main Attributes for the Manifest.

return
the main Attributes for the Manifest

	return attr;
    
public inthashCode()
Returns the hash code for this Manifest.

	return attr.hashCode() + entries.hashCode();
    
static voidmake72Safe(java.lang.StringBuffer line)
Adds line breaks to enforce a maximum 72 bytes per line.

        int length = line.length();
        if (length > 72) {
            int index = 70;
            while (index < length - 2) {
                line.insert(index, "\r\n ");
                index += 72;
                length += 3;
            }
        }
        return;
    
private java.lang.StringparseName(byte[] lbuf, int len)

	if (toLower(lbuf[0]) == 'n" && toLower(lbuf[1]) == 'a" &&
	    toLower(lbuf[2]) == 'm" && toLower(lbuf[3]) == 'e" &&
	    lbuf[4] == ':" && lbuf[5] == ' ") {
            try {
	        return new String(lbuf, 6, len - 6, "UTF8");
            }
            catch (Exception e) {
            }
	}
	return null;
    
public voidread(java.io.InputStream is)
Reads the Manifest from the specified InputStream. The entry names and attributes read will be merged in with the current manifest entries.

param
is the input stream
exception
IOException if an I/O error has occurred

	// Buffered input stream for reading manifest data
	FastInputStream fis = new FastInputStream(is);
	// Line buffer
	byte[] lbuf = new byte[512];
	// Read the main attributes for the manifest
	attr.read(fis, lbuf);
	// Total number of entries, attributes read
	int ecount = 0, acount = 0;
	// Average size of entry attributes
	int asize = 2;
	// Now parse the manifest entries
	int len;
	String name = null;
        boolean skipEmptyLines = true;
        byte[] lastline = null;

	while ((len = fis.readLine(lbuf)) != -1) {
	    if (lbuf[--len] != '\n") {
		throw new IOException("manifest line too long");
	    }
	    if (len > 0 && lbuf[len-1] == '\r") {
		--len;
	    }
            if (len == 0 && skipEmptyLines) {
                continue;
            }
            skipEmptyLines = false;

	    if (name == null) {
		name = parseName(lbuf, len);
		if (name == null) {
		    throw new IOException("invalid manifest format");                
		}
                if (fis.peek() == ' ") {
		    // name is wrapped
                    lastline = new byte[len - 6];
                    System.arraycopy(lbuf, 6, lastline, 0, len - 6);
                    continue;
                }
	    } else {
		// continuation line
                byte[] buf = new byte[lastline.length + len - 1];
                System.arraycopy(lastline, 0, buf, 0, lastline.length);
                System.arraycopy(lbuf, 1, buf, lastline.length, len - 1);
                if (fis.peek() == ' ") {
		    // name is wrapped
                    lastline = buf;
		    continue;
	        }
		name = new String(buf, 0, buf.length, "UTF8");
                lastline = null;
	    }
	    Attributes attr = getAttributes(name);
	    if (attr == null) {
		attr = new Attributes(asize);
		entries.put(name, attr);
	    }
	    attr.read(fis, lbuf);
	    ecount++;
	    acount += attr.size();
	    //XXX: Fix for when the average is 0. When it is 0, 
	    // you get an Attributes object with an initial
	    // capacity of 0, which tickles a bug in HashMap.
	    asize = Math.max(2, acount / ecount);

	    name = null;
            skipEmptyLines = true;
	}
    
private inttoLower(int c)

	return (c >= 'A" && c <= 'Z") ? 'a" + (c - 'A") : c;
    
public voidwrite(java.io.OutputStream out)
Writes the Manifest to the specified OutputStream. Attributes.Name.MANIFEST_VERSION must be set in MainAttributes prior to invoking this method.

param
out the output stream
exception
IOException if an I/O error has occurred
see
#getMainAttributes

	DataOutputStream dos = new DataOutputStream(out);
	// Write out the main attributes for the manifest
	attr.writeMain(dos);
	// Now write out the pre-entry attributes
	Iterator it = entries.entrySet().iterator();
	while (it.hasNext()) {
	    Map.Entry e = (Map.Entry)it.next();
            StringBuffer buffer = new StringBuffer("Name: ");
            String value = (String)e.getKey();
            if (value != null) {
                byte[] vb = value.getBytes("UTF8");
                value = new String(vb, 0, 0, vb.length);
            }
	    buffer.append(value);
	    buffer.append("\r\n");
            make72Safe(buffer);
            dos.writeBytes(buffer.toString());
	    ((Attributes)e.getValue()).write(dos);
	}
	dos.flush();