Java I/O and over one million other books are available for Amazon Kindle . Learn more

Have one to sell? Sell yours here
Java I/O
 
 
Start reading Java I/O on your Kindle in under a minute.

Don't have a Kindle? Get your Kindle here, or download a FREE Kindle Reading App.

Java I/O [Paperback]

Elliotte Rusty Harold
4.0 out of 5 stars  See all reviews (1 customer review)

Available from these sellers.


‹  Return to Product Overview

Product Description

Review

'If I had to decide the best technical book ever read by me, this would be a real candidate. In my opinion a good programming book should limit itself to covering some well-defined part of its (usually) exhaustive topic. It should be easy to read with well-chose and short code-samples, especially for the critical parts and optionally, the code should grow throughout the chapters and evolve to full working programs at the end. This title fulfils it all... There aren't many illustrations throughout, but the reader will not miss them. The 'in-depth-notes' at strategic places are interesting and reveals a deep knowledge of the subject. So, if you want a fundamental understanding of streams, and data communication and /or a deep understanding of the Java I/O-model, buy it.' - Christer Loefving, Cvue, January 2000

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

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 I/O, 2nd Edition tells you all you need to know about the four main categories of streams and uncovers less-known features to help make your I/O operations more efficient. You'll also learn how to control number formatting, use characters aside from the standard ASCII character set, and get a head start on writing truly multilingual software. --This text refers to an out of print or unavailable edition of this title.

About the Author

Elliotte Rusty Harold is a noted writer and programmer, both on and off the Internet. He started by writing FAQ lists for the Macintosh newsgroups on Usenet, and has since branched out into books, Web sites, and newsletters. He's currently fascinated by Java, which is beginning to consume his life. His Cafe Au Lait Web site at http://sunsite.unc.edu/javafaq/ is one of the most popular independent Java sites on the Net. Elliotte resides in New York City with his wife Beth and cats Charm (named after the quark) and Marjorie (named after his mother-in-law). When not writing about Java, he enjoys genealogy, mathematics, and quantum mechanics, and has been known to try to incorporate these subjects into his computer books (when he can slip them past his editors). So far he hasn't been able to, but he suspects that a short, last-minute biography might not be inspected as closely as the rest of a manuscript. His other books include The Java Developer's Resource (Prentice Hall), Java Network Programming (O'Reilly), Java Secrets (IDG Books), and JavaBeans (IDG Books)

Excerpted from Java I/O by Elliotte Rusty Harold. Copyright © 1999. Reprinted by permission. All rights reserved.

Chapter 5 - Network Streams

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.

‹  Return to Product Overview