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.

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

The Manifest class also contains methods to read a Manifest object from an input stream<br />

and write one onto an output stream. These are mostly for <strong>Java</strong>'s private use.<br />

public void write(OutputStream out) throws <strong>IO</strong>Exception // <strong>Java</strong> 2<br />

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

The write() method is particularly useless, since there's no good way to create a Manifest<br />

object and add attributes to it from within <strong>Java</strong>. [5] Most commonly, you'll simply work with<br />

Manifest objects returned by getManifest().<br />

Finally, the Manifest class overrides the default implementations of equals() and clone():<br />

public boolean equals(Object o) // <strong>Java</strong> 2<br />

public Object clone() // <strong>Java</strong> 2<br />

9.5.8 JarInputStream<br />

JarInputStream is a subclass of ZipInputStream that reads data from JAR archives.<br />

public class JarInputStream extends ZipInputStream // <strong>Java</strong> 2<br />

There are two constructors that chain the JAR input stream to an underlying input stream.<br />

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

public JarInputStream(InputStream in, boolean verify)<br />

throws <strong>IO</strong>Exception // <strong>Java</strong> 2<br />

By default, any signatures present in the JAR archive will be verified and an <strong>IO</strong>Exception<br />

thrown if verification fails. However, you can turn this behavior off by passing false as the<br />

second argument to the constructor. For example:<br />

FileInputStream fin = new FileInputStream("javaio.jar");<br />

JarInputStream jin = new JarInputStream(fin, false);<br />

When the JarInputStream object is constructed, the manifest, if present, is read from the<br />

stream and stored inside the class as a Manifest object. You do not get an opportunity to read<br />

the manifest from the stream yourself. However, you can retrieve the Manifest object with<br />

the getManifest() method:<br />

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

Otherwise, a JAR input stream is used almost exactly like a zip input stream. You position the<br />

stream on a particular entry in the file, then read data from it using the normal read()<br />

methods. Any necessary inflation is performed transparently. When you've finished reading<br />

an entry, you close it, then position the stream on the next entry. Two methods,<br />

getNextEntry() and read(), are overridden so that verification of signatures can be<br />

performed. A getNextJarEntry() method that returns a JarEntry instead of ZipEntry is<br />

also available. This method can be used in place of getNextEntry(), if you like.<br />

5<br />

I suppose you could write a manifest file on a byte array output stream, create a byte array input stream from the output stream's byte array, then<br />

read it back in, but that's really a kludge.<br />

186

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

Saved successfully!

Ooh no, something went wrong!