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

Attributes

public class Attributes extends Object implements Cloneable, Map
The Attributes class maps Manifest attribute names to associated string values. Valid attribute names are case-insensitive, are restricted to the ASCII characters in the set [0-9a-zA-Z_-], and cannot exceed 70 characters in length. Attribute values can contain any characters and will be UTF8-encoded when written to the output stream. See the JAR File Specification for more information about valid attribute names and values.
author
David Connelly
version
1.54, 04/21/06
see
Manifest
since
1.2

Fields Summary
protected Map
map
The attribute name-value mappings.
Constructors Summary
public Attributes()
Constructs a new, empty Attributes object with default size.

	this(11);
    
public Attributes(int size)
Constructs a new, empty Attributes object with the specified initial size.

param
size the initial number of attributes

	map = new HashMap(size);
    
public Attributes(Attributes attr)
Constructs a new Attributes object with the same attribute name-value mappings as in the specified Attributes.

param
attr the specified Attributes

	map = new HashMap(attr);
    
Methods Summary
public voidclear()
Removes all attributes from this Map.

	map.clear();
    
public java.lang.Objectclone()
Returns a copy of the Attributes, implemented as follows:
public Object clone() { return new Attributes(this); }
Since the attribute names and values are themselves immutable, the Attributes returned can be safely modified without affecting the original.

	return new Attributes(this);
    
public booleancontainsKey(java.lang.Object name)
Returns true if this Map contains the specified attribute name (key).

param
name the attribute name
return
true if this Map contains the specified attribute name

	return map.containsKey(name);
    
public booleancontainsValue(java.lang.Object value)
Returns true if this Map maps one or more attribute names (keys) to the specified value.

param
value the attribute value
return
true if this Map maps one or more attribute names to the specified value

	return map.containsValue(value);
    
public java.util.SetentrySet()
Returns a Collection view of the attribute name-value mappings contained in this Map.

	return map.entrySet();
    
public booleanequals(java.lang.Object o)
Compares the specified Attributes object with this Map for equality. Returns true if the given object is also an instance of Attributes and the two Attributes objects represent the same mappings.

param
o the Object to be compared
return
true if the specified Object is equal to this Map

	return map.equals(o);
    
public java.lang.Objectget(java.lang.Object name)
Returns the value of the specified attribute name, or null if the attribute name was not found.

param
name the attribute name
return
the value of the specified attribute name, or null if not found.

	return map.get(name);
    
public java.lang.StringgetValue(java.lang.String name)
Returns the value of the specified attribute name, specified as a string, or null if the attribute was not found. The attribute name is case-insensitive.

This method is defined as:

return (String)get(new Attributes.Name((String)name));

param
name the attribute name as a string
return
the String value of the specified attribute name, or null if not found.
throws
IllegalArgumentException if the attribute name is invalid

        return (String)get(new Attributes.Name((String)name));
    
public java.lang.StringgetValue(java.util.jar.Attributes$Name name)
Returns the value of the specified Attributes.Name, or null if the attribute was not found.

This method is defined as:

return (String)get(name);

param
name the Attributes.Name object
return
the String value of the specified Attribute.Name, or null if not found.

	return (String)get(name);
    
public inthashCode()
Returns the hash code value for this Map.

	return map.hashCode();
    
public booleanisEmpty()
Returns true if this Map contains no attributes.

	return map.isEmpty();
    
public java.util.SetkeySet()
Returns a Set view of the attribute names (keys) contained in this Map.

	return map.keySet();
    
public java.lang.Objectput(java.lang.Object name, java.lang.Object value)
Associates the specified value with the specified attribute name (key) in this Map. If the Map previously contained a mapping for the attribute name, the old value is replaced.

param
name the attribute name
param
value the attribute value
return
the previous value of the attribute, or null if none
exception
ClassCastException if the name is not a Attributes.Name or the value is not a String

        return map.put((Attributes.Name)name, (String)value);
    
public voidputAll(java.util.Map attr)
Copies all of the attribute name-value mappings from the specified Attributes to this Map. Duplicate mappings will be replaced.

param
attr the Attributes to be stored in this map
exception
ClassCastException if attr is not an Attributes

	// ## javac bug?
	if (!Attributes.class.isInstance(attr))
	    throw new ClassCastException();
	for (Map.Entry<?,?> me : (attr).entrySet())
	    put(me.getKey(), me.getValue());
    
public java.lang.StringputValue(java.lang.String name, java.lang.String value)
Associates the specified value with the specified attribute name, specified as a String. The attributes name is case-insensitive. If the Map previously contained a mapping for the attribute name, the old value is replaced.

This method is defined as:

return (String)put(new Attributes.Name(name), value);

param
name the attribute name as a string
param
value the attribute value
return
the previous value of the attribute, or null if none
exception
IllegalArgumentException if the attribute name is invalid

	return (String)put(new Name(name), value);
    
voidread(java.util.jar.Manifest$FastInputStream is, byte[] lbuf)

	String name = null, value = null;
        byte[] lastline = null;

	int len;
	while ((len = is.readLine(lbuf)) != -1) {
            boolean lineContinued = false;
	    if (lbuf[--len] != '\n") {
		throw new IOException("line too long");
	    }
	    if (len > 0 && lbuf[len-1] == '\r") {
		--len;
	    }
	    if (len == 0) {
		break;
	    }
	    int i = 0;
	    if (lbuf[0] == ' ") {
		// continuation of previous line
		if (name == null) {
		    throw new IOException("misplaced continuation line");
		}
                lineContinued = true;
                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 (is.peek() == ' ") {
                    lastline = buf;
                    continue;
                }
		value = new String(buf, 0, buf.length, "UTF8");
                lastline = null;
	    } else {
                while (lbuf[i++] != ':") {
                    if (i >= len) {
			throw new IOException("invalid header field");
                    }
                }
                if (lbuf[i++] != ' ") {
		    throw new IOException("invalid header field");
                }
                name = new String(lbuf, 0, 0, i - 2);
                if (is.peek() == ' ") {
                    lastline = new byte[len - i];
                    System.arraycopy(lbuf, i, lastline, 0, len - i);
                    continue;
                }
                value = new String(lbuf, i, len - i, "UTF8");
            }
	    try {
		if ((putValue(name, value) != null) && (!lineContinued)) {
                    Logger.getLogger("java.util.jar").warning(
                                     "Duplicate name in Manifest: " + name
                                     + ".\n"
                                     + "Ensure that the manifest does not "
                                     + "have duplicate entries, and\n"
                                     + "that blank lines separate "
                                     + "individual sections in both your\n"
                                     + "manifest and in the META-INF/MANIFEST.MF "
                                     + "entry in the jar file.");
                } 
	    } catch (IllegalArgumentException e) {
		throw new IOException("invalid header field name: " + name);
	    }
	}
    
public java.lang.Objectremove(java.lang.Object name)
Removes the attribute with the specified name (key) from this Map. Returns the previous attribute value, or null if none.

param
name attribute name
return
the previous value of the attribute, or null if none

	return map.remove(name);
    
public intsize()
Returns the number of attributes in this Map.

	return map.size();
    
public java.util.Collectionvalues()
Returns a Collection view of the attribute values contained in this Map.

	return map.values();
    
voidwrite(java.io.DataOutputStream os)

	Iterator it = entrySet().iterator();
	while (it.hasNext()) {
	    Map.Entry e = (Map.Entry)it.next();
            StringBuffer buffer = new StringBuffer(
                                        ((Name)e.getKey()).toString());
	    buffer.append(": ");

            String value = (String)e.getValue();
            if (value != null) {
                byte[] vb = value.getBytes("UTF8");
                value = new String(vb, 0, 0, vb.length);
            }
            buffer.append(value);

	    buffer.append("\r\n");
            Manifest.make72Safe(buffer);
            os.writeBytes(buffer.toString());
	}
	os.writeBytes("\r\n");
    
voidwriteMain(java.io.DataOutputStream out)

	// write out the *-Version header first, if it exists
	String vername = Name.MANIFEST_VERSION.toString();
	String version = getValue(vername);
	if (version == null) {
	    vername = Name.SIGNATURE_VERSION.toString();
	    version = getValue(vername);
	}

	if (version != null) {
	    out.writeBytes(vername+": "+version+"\r\n");
	}

	// write out all attributes except for the version
	// we wrote out earlier
	Iterator it = entrySet().iterator();
	while (it.hasNext()) {
	    Map.Entry e = (Map.Entry)it.next();
	    String name = ((Name)e.getKey()).toString();
	    if ((version != null) && ! (name.equalsIgnoreCase(vername))) {

                StringBuffer buffer = new StringBuffer(name);
		buffer.append(": ");

                String value = (String)e.getValue();
                if (value != null) {
                    byte[] vb = value.getBytes("UTF8");
                    value = new String(vb, 0, 0, vb.length);
                }
                buffer.append(value);

		buffer.append("\r\n");
                Manifest.make72Safe(buffer);
                out.writeBytes(buffer.toString());
	    }
	}
	out.writeBytes("\r\n");