Attributes

The java.util.jar.Attributes class is mostly just a concrete implementation of the java.util.Map interface from the Collections API.

public class Attributes implements Map, Cloneable

An Attributes object is a container for an entry in a manifest file. Recall that the entry is composed of name/value pairs; the keys of the map are the names and the values of the entries are the values of the map. The Attributes class is accessed almost entirely through the methods of the Map interface and has three public constructors:

public Attributes( ) public Attributes(int size) public Attributes(Attributes a)

However, these constructors are primarily for Java's internal use. Most of the time, you'll simply retrieve Attributes objects from the getAttributes( ) method of JarEntry or the getAttributes( ) and getMainAttributes( ) methods of Manifest.

The Attributes class implements all the usual Map methods:

public Object get(Object name) public Object put(Object name, Object value) public Object remove(Object name) public boolean containsValue(Object value) public boolean containsKey(Object name) public void putAll(Map attr) public void clear( ) public int size( ) public boolean isEmpty( ) public Set keySet( ) public Collection values( ) public Set entrySet( ) public boolean equals(Object o) public int hashCode( )

The keys for this map should all be Attributes.Name objects. Attributes.Name is a public inner class called Name inside the Attributes class. However, it's simplest to just think of it as another class in java.util.jar with a somewhat funny name. This class has a single constructor:

public Attributes.Name(String name)

The Attributes.Name class represents the name half of the name/value pairs in a manifest file. Attribute names are restricted to the upper- and lowercase letters AZ, the digits 09, the underscore, and the hyphen. The Attributes.Name( ) constructor checks to make sure that the name is legal and throws an IllegalArgumentException if it isn't.

Attributes.Name overrides the equals( ), hashCode( ), and toString( ) methods but has no other methods. It exists only to be a key in the Attributes map.

The Attributes.Name class defines some mnemonic constants that identify particular attribute names found in some kinds of JAR files. These are all Attributes.Name objects:

Attributes.Name.MANIFEST_VERSION // "Manifest-Version" Attributes.Name.SIGNATURE_VERSION // "Signature-Version" Attributes.Name.CONTENT_TYPE // "Content-Type" Attributes.Name.CLASS_PATH // "Class-Path" Attributes.Name.MAIN_CLASS // "Main-Class" Attributes.Name.SEALED // "Sealed" Attributes.Name.IMPLEMENTATION_TITLE // "Implementation-Title" Attributes.Name.IMPLEMENTATION_VERSION // "Implementation-Version" Attributes.Name.IMPLEMENTATION_VENDOR // "Implementation-Vendor" Attributes.Name.IMPLEMENTATION_VENDOR_ID // "Implementation-Vendor-Id" Attributes.Name.IMPLEMENTATION_URL // "Implementation-Vendor-URL" Attributes.Name.SPECIFICATION_TITLE // "Specification-Title" Attributes.Name.SPECIFICATION_VERSION // "Specification-Version" Attributes.Name.SPECIFICATION_VENDOR // "Specification-Vendor" Attributes.Name.SIGNATURE_VERSION // "Signature-Version" Attributes.Name.EXTENSION_LIST // "Extension-List" Attributes.Name.EXTENSION_NAME // "Extension-Name" Attributes.Name.EXTENSION_INSTALLATION // "Extension-Installation"

Since Attributes implements Cloneable as well as Map, it also provides a clone( ) method:

public Object clone( )

Unlike maps in general, Attributes maps contain only strings, raw strings as values, and strings embedded in Attributes.Name objects. Therefore, the Attributes class contains three extra map-like methods for getting and putting strings into the map:

public String putValue(String name, String value) public String getValue(String name) public String getValue(Attributes.Name name)

The last one takes an Attributes.Name object as an argument. Example 11-1 is a revised version of the FancyZipLister from Example 10-9. This program works with JAR files and prints the attributes of each entry as well as the information seen previously.

Example 11-1. JarLister

import java.util.*; import java.util.zip.*; import java.util.jar.*; import java.io.*; public class JarLister { public static void main(String[] args) throws IOException { JarFile jf = new JarFile(args[0]); Enumeration e = jf.entries( ); while (e.hasMoreElements( )) { JarEntry je = (JarEntry) e.nextElement( ); String name = je.getName( ); Date lastModified = new Date(je.getTime( )); long uncompressedSize = je.getSize( ); long compressedSize = je.getCompressedSize( ); long crc = je.getCrc( ); int method = je.getMethod( ); String comment = je.getComment( ); if (method == ZipEntry.STORED) { System.out.println(name + " was stored at " + lastModified); System.out.println("with a size of " + uncompressedSize + " bytes"); } else if (method == ZipEntry.DEFLATED) { System.out.println(name + " was deflated at " + lastModified); System.out.println("from " + uncompressedSize + " bytes to " + compressedSize + " bytes, a savings of " + (100.0 - 100.0*compressedSize/uncompressedSize) + "%"); } else { System.out.println(name + " was compressed using an unrecognized method at " + lastModified); System.out.println("from " + uncompressedSize + " bytes to " + compressedSize + " bytes, a savings of " + (100.0 - 100.0*compressedSize/uncompressedSize) + "%"); } System.out.println("Its CRC is " + crc); if (comment != null && !comment.equals("")) { System.out.println(comment); } if (je.isDirectory( )) { System.out.println(name + " is a directory"); } Attributes a = je.getAttributes( ); if (a != null) { Object[] nameValuePairs = a.entrySet().toArray( ); for (int j = 0; j < nameValuePairs.length; j++) { System.out.println(nameValuePairs[j]); } } System.out.println( ); } } }

Категории