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 />

Again, these do not change the data in any way and may be used exactly like any other<br />

write() method. They simply update the internal checksum with the bytes written as they're<br />

written. Both CheckedOutputStream and CheckedInputStream have a getChecksum()<br />

method that returns the Checksum object for the stream. You can use this Checksum object to<br />

get the current value of the checksum for the stream.<br />

public Checksum getChecksum()<br />

These methods return a reference to the actual Checksum object that's being used to calculate<br />

the checksum. It is not copied first. Thus, if a separate thread is accessing this stream, the<br />

value in the checksum may change while you're working with the Checksum object.<br />

Conversely, if you invoke one of this Checksum object's update() methods, it affects the<br />

value of the checksum for the stream as well.<br />

9.5 JAR Files<br />

<strong>Java</strong> 1.1 added support for <strong>Java</strong> ARchive files, JAR files for short. JAR files bundle the many<br />

different classes, images, and sound files an applet requires into a single file. It is generally<br />

faster for a web browser to download one JAR file than to download the individual files the<br />

archive contains, since only one HTTP connection is required. An applet stored in a JAR file,<br />

instead of as merely loose .class files, is embedded in a web page with an tag with<br />

an archive attribute pointing to the JAR file. For example:<br />

<br />

<br />

The code attribute still says that the main class of this applet is called NavigationMenu.<br />

However, a <strong>Java</strong> 1.1 web browser, rather than asking the web server for the file<br />

NavigationMenu.class as a <strong>Java</strong> 1.0 web browser would, asks the web server for the file<br />

NavigationMenu.jar. Then the browser looks inside NavigationMenu.jar to find the file<br />

NavigationMenu.class. Only if it doesn't find NavigationMenu.class inside<br />

NavigationMenu.jar does it then go back to the web server and ask for NavigationMenu.class.<br />

Now suppose the NavigationMenu applet tries to load an image called menu.gif. The applet<br />

will look for this file inside the JAR archive too. It only has to make a new connection to the<br />

web server if it can't find menu.gif in the archive.<br />

Sun wisely decided not to attempt to define a new file format for JAR files. Instead, they<br />

stuck with the tried-and-true zip format. This means that the classes, images, sounds, and<br />

other files stored inside a JAR archive can be compressed, making the applet even faster to<br />

download. This also means that standard tools like PKZip and standard zip libraries like<br />

java.util.zip can work with JAR files.<br />

JAR files have also become <strong>Java</strong>'s preferred means of distributing <strong>Java</strong> Beans and class<br />

libraries. For instance, the <strong>Java</strong> Cryptography Extension, discussed in the next chapter, is<br />

mostly a set of classes packed up in the file jce12-ea-dom.jar. Since the library is distributed<br />

as a single file rather than a collection of nested folders, it's harder for one file to get moved or<br />

deleted. The overall system is more robust. To make the files contained in the archive<br />

available to <strong>Java</strong>, the complete path to the archive itself is added to the class path. For<br />

176

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

Saved successfully!

Ooh no, something went wrong!