Review
Product Description
All of Java's Input/Output (I/O) facilities are based on streams, which provide simple ways to read and write data of different types. Java provides many different kinds of streams, each with its own application. The universe of streams is divided into four large categories: input streams and output streams, for reading and writing binary data; and readers and writers, for reading and writing textual (character) data. You're almost certainly familiar with the basic kinds of streams--but did you know that there's a CipherInputStream for reading encrypted data? And a ZipOutputStream for automatically compressing data? Do you know how to use buffered streams effectively to make your I/O operations more efficient? Java I/O tells you all you ever need to know about streams--and probably more.
A discussion of I/O wouldn't be complete without treatment of character sets and formatting. Java supports the UNICODE standard, which provides definitions for the character sets of most written languages. Consequently, Java is the first programming language that lets you do I/O in virtually any language. Java also provides a sophisticated model for formatting textual and numeric data. Java I/O shows you how to control number formatting, use characters aside from the standard (but outdated) ASCII character set, and get a head start on writing truly multilingual software.
Java I/O includes:
- Coverage of all I/O classes and related classes
- In-depth coverage of Java's number formatting facilities and its support for International character sets
From the Publisher
About the Author
Excerpted from Java I/O by Elliotte Rusty Harold. Copyright © 1999. Reprinted by permission. All rights reserved.
In this chapter:
URLs
URL Connections
Sockets
Server Sockets
URLViewer
From its first days, Java has had the network in mind, more so than any other common programming language. Java is the first programming language to provide as much support for network I/O as it does for file I/O, perhaps even more--Java's URL, URLConnection, Socket, and ServerSocket classes are all fertile sources of streams. The exact type of the stream used by a network connection is typically hidden inside the undocumented sun classes. Thus, network I/O relies primarily on the basic InputStream and OutputStream methods, which you can wrap with any higher-level stream that suits your needs: buffering, cryptography, compression, or whatever your application requires.
URLs
The java.net.URL class represents a Uniform Resource Locator. Each URL unambiguously identifies the location of a resource on the Internet. The URL class has four constructors. All are declared to throw MalformedURLException, a subclass of IOException.
public URL(String u) throws MalformedURLException
public URL(String protocol, String host, String file)
throws MalformedURLException
public URL(String protocol, String host, int port, String file)
throws MalformedURLException
public URL(URL context, String u) throws MalformedURLException
A MalformedURLException is thrown if the constructor's arguments do not specify a valid URL. Often this means a particular Java implementation does not have the right protocol handler installed.
You can also construct the URL object by passing its pieces to the constructor.
You don't normally need to specify a port for a URL; most protocols have default ports. For instance, the HTTP port is 80.
Finally, many HTML files contain relative URLs. The fourth constructor in the previous code creates URLs relative to a given URL and is particularly useful when parsing HTML.
Once a URL object has been constructed, there are two ways to retrieve its data. The openStream() method returns a raw stream of bytes from the source. The getContent() method returns a Java object that represents the data. When you call getContent(), Java looks for a content handler that matches the MIME type of the data. It is the openStream() method that is of concern in this book.
The openStream() method makes a socket connection to the server and port specified in the URL. It returns an input stream from which you can read the data at that URL, allowing you to download data from the server. Any headers that come before the actual data or file requested are stripped off before the stream is opened. You get only raw data.
Most network connections, even on LANs, are slower and less reliable sources of data than files. Connections across the Internet are even slower and less reliable, and connections through a modem are slower and less reliable still. One way to enhance performance under these conditions is to buffer the data: to read as much data as you can into a temporary storage array inside the class, then parcel it out as needed. In the next chapter, you'll learn about the BufferedInputStream class that does exactly this.