23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

9.5.7 Manifest<br />

<strong>Java</strong> I/O<br />

What the java.util.jar classes add to the superclasses in java.util.zip is the ability to<br />

read the attributes of each JAR entry as well as the manifest for the entire JAR archive. Recall<br />

that a JAR archive should have exactly one manifest file. That manifest file has entries that<br />

apply to the entire file as well as entries for some (though perhaps not all) of the files stored in<br />

the archive. Although physically the manifest file belongs to the entire archive, logically parts<br />

of it belong to different entries in the archive. The java.util.jar.Manifest class represents<br />

this manifest file.<br />

public class Manifest extends Object implements Cloneable // <strong>Java</strong> 2<br />

It has methods to get the entries and attributes of a manifest, to write the manifest onto an<br />

output stream or to read entries from an input stream, and an assortment of utility methods.<br />

The Manifest class has three constructors:<br />

public Manifest() // <strong>Java</strong> 2<br />

public Manifest(InputStream in) throws <strong>IO</strong>Exception // <strong>Java</strong> 2<br />

public Manifest(Manifest man) // <strong>Java</strong> 2<br />

The first constructor creates an empty manifest (one with no entries); the second reads the<br />

manifest from the given stream; the third copies the manifest from the Manifest object<br />

passed as an argument. However, all three are mostly for the internal use of <strong>Java</strong>. Instead,<br />

client programmers use the getManifest() method of JarFile to retrieve the Manifest<br />

object for the manifest file in a particular archive. For example:<br />

JarFile jf = new JarFile("classes.jar");<br />

Manifest m = jf.getManifest();<br />

The Manifest class has three methods that return a map of the entries in the manifest.<br />

getEntries() returns an unspecified Map (a HashMap object in JDK 1.2) in which the keys<br />

are the entry names and the values are the Attributes objects for the entry:<br />

public Map getEntries() // <strong>Java</strong> 2<br />

The getMainAttributes() method returns an Attributes object representing the attributes<br />

in the manifest file that apply to the file as a whole rather than to any individual entry in the<br />

file, such as Manifest-Version:<br />

public Attributes getMainAttributes() // <strong>Java</strong> 2<br />

The getAttributes() method returns an Attributes object containing the name/value pairs<br />

of the named entry. The Name attribute is not included in this list:<br />

public Attributes getAttributes(String entryName) // <strong>Java</strong> 2<br />

The clear() method removes all entries and attributes from the manifest so that it can be<br />

reused; client programmers have little reason to call this method:<br />

public void clear() // <strong>Java</strong> 2<br />

185

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!