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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5<br />

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

From its first days, <strong>Java</strong> has always had the network in mind, more so than any other<br />

common programming language. <strong>Java</strong> is the first programming language to provide as<br />

much support for network I/O as it does for file I/O, perhaps even more. Chapter 5<br />

introduces <strong>Java</strong>'s URL, URLConnection, Socket, and ServerSocket classes, all fertile<br />

sources of streams. Typically the exact type of the stream used by a network<br />

connection is hidden inside the undocumented sun classes. Thus network I/O relies<br />

primarily on the basic InputStream and OutputStream methods. Examples in this<br />

chapter include several simple web and email clients.<br />

Part III: Filter Streams<br />

Chapter 6<br />

Chapter 7<br />

Chapter 8<br />

Chapter 6 introduces filter streams. Filter input streams read data from a preexisting<br />

input stream like a FileInputStream, and have an opportunity to work with or<br />

change the data before it is delivered to the client program. Filter output streams write<br />

data to a preexisting output stream such as a FileOutputStream, and have an<br />

opportunity to work with or change the data before it is written onto the underlying<br />

stream. Multiple filters can be chained onto a single underlying stream to provide the<br />

functionality offered by each filter. Filters streams are used for encryption,<br />

compression, translation, buffering, and much more. At the end of this chapter, the<br />

File Viewer program is redesigned around filter streams to make it more extensible.<br />

Chapter 7 introduces data streams, which are useful for writing strings, integers,<br />

floating-point numbers, and other data that's commonly presented at a level higher<br />

than mere bytes. The DataInputStream and DataOutputStream classes read and<br />

write the primitive <strong>Java</strong> data types (boolean, int, double, etc.) and strings in a<br />

particular, well-defined, platform-independent format. Since DataInputStream and<br />

DataOutputStream use the same formats, they're complementary. What a data output<br />

stream writes, a data input stream can read, and vice versa. These classes are<br />

especially useful when you need to move data between platforms that may use<br />

different native formats for integers or floating-point numbers. Along the way, you'll<br />

develop classes to read and write little-endian numbers, and you'll extend the File<br />

Viewer program to handle big- and little-endian integers and floating-point numbers of<br />

varying widths.<br />

Chapter 8 shows you how streams can move data from one part of a running <strong>Java</strong><br />

program to another. There are three main ways to do this. Sequence input streams<br />

chain several input streams together so that they appear as a single stream. Byte array<br />

streams allow output to be stored in byte arrays and input to be read from byte arrays.<br />

Finally, piped input and output streams allow output from one thread to become input<br />

for another thread.<br />

4

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

Saved successfully!

Ooh no, something went wrong!