📄 attributes.java
字号:
/* * @(#)Attributes.java 1.45 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.util.jar;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.util.HashMap;import java.util.Map;import java.util.Set;import java.util.Collection;import java.util.AbstractSet;import java.util.Iterator;import java.util.logging.Logger; /** * 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 * <a href="../../../../guide/jar/jar.html">JAR File Specification</a> * for more information about valid attribute names and values. * * @author David Connelly * @version 1.45, 01/23/03 * @see Manifest * @since 1.2 */public class Attributes implements Map, Cloneable { /** * The attribute name-value mappings. */ protected Map map; /** * Constructs a new, empty Attributes object with default size. */ public Attributes() { this(11); } /** * Constructs a new, empty Attributes object with the specified * initial size. * * @param size the initial number of attributes */ public Attributes(int size) { map = new HashMap(size); } /** * Constructs a new Attributes object with the same attribute name-value * mappings as in the specified Attributes. * * @param attr the specified Attributes */ public Attributes(Attributes attr) { map = new HashMap(attr); } /** * 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. */ public Object get(Object name) { return map.get(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. * <p> * This method is defined as: * <pre> * return (String)get(new Attributes.Name((String)name)); * </pre> * * @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 */ public String getValue(String name) { return (String)get(new Attributes.Name((String)name)); } /** * Returns the value of the specified Attributes.Name, or null if the * attribute was not found. * <p> * This method is defined as: * <pre> * return (String)get(name); * </pre> * * @param name the Attributes.Name object * @return the String value of the specified Attribute.Name, or null if * not found. */ public String getValue(Name name) { return (String)get(name); } /** * 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 */ public Object put(Object name, Object value) { return map.put((Attributes.Name)name, (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. * <p> * This method is defined as: * <pre> * return (String)put(new Attributes.Name(name), value); * </pre> * * @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 */ public String putValue(String name, String value) { return (String)put(new Name(name), value); } /** * 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 */ public Object remove(Object name) { return map.remove(name); } /** * 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 */ public boolean containsValue(Object value) { return map.containsValue(value); } /** * 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 */ public boolean containsKey(Object name) { return map.containsKey(name); } /** * 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 */ public void putAll(Map attr) { map.putAll((Attributes)attr); } /** * Removes all attributes from this Map. */ public void clear() { map.clear(); } /** * Returns the number of attributes in this Map. */ public int size() { return map.size(); } /** * Returns true if this Map contains no attributes. */ public boolean isEmpty() { return map.isEmpty(); } /** * Returns a Set view of the attribute names (keys) contained in this Map. */ public Set keySet() { return map.keySet(); } /** * Returns a Collection view of the attribute values contained in this Map. */ public Collection values() { return map.values(); } /** * Returns a Collection view of the attribute name-value mappings * contained in this Map. */ public Set entrySet() { return map.entrySet(); } /** * 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 */ public boolean equals(Object o) { return map.equals(o); } /** * Returns the hash code value for this Map. */ public int hashCode() { return map.hashCode(); } /** * Returns a copy of the Attributes, implemented as follows: * <pre> * public Object clone() { return new Attributes(this); } * </pre> * Since the attribute names and values are themselves immutable, * the Attributes returned can be safely modified without affecting * the original. */ public Object clone() { return new Attributes(this); } /* * Writes the current attributes to the specified data output stream. * XXX Need to handle UTF8 values and break up lines longer than 72 bytes */ void write(DataOutputStream os) throws IOException { 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"); } /* * Writes the current attributes to the specified data output stream, * make sure to write out the MANIFEST_VERSION or SIGNATURE_VERSION * attributes first. * * XXX Need to handle UTF8 values and break up lines longer than 72 bytes */ void writeMain(DataOutputStream out) throws IOException { // 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"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -