Java Reference
In-Depth Information
CHAPTER 2
Streams
A large part of what network programs do is simple input and output: moving bytes
from one system to another. Bytes are bytes; to a large extent, reading data a server sends
you is not all that different from reading a file. Sending text to a client is not that different
from writing a file. However, input and output (I/O) in Java is organized differently than
it is in most other languages, such as Fortran, C, and C++. Consequently, I'll take a few
pages to summarize Java's unique approach to I/O.
I/O in Java is built on streams . Input streams read data; output streams write data.
Different stream classes, like java.io.FileInputStream and sun.net.TelnetOutput
Stream , read and write particular sources of data. However, all output streams have the
same basic methods to write data and all input streams use the same basic methods to
read data. After a stream is created, you can often ignore the details of exactly what it
is you're reading or writing.
Filter streams can be chained to either an input stream or an output stream. Filters can
modify the data as it's read or written—for instance, by encrypting or compressing it—
or they can simply provide additional methods for converting the data that's read or
written into other formats. For instance, the java.io.DataOutputStream class provides
a method that converts an int to four bytes and writes those bytes onto its underlying
output stream.
Readers and writers can be chained to input and output streams to allow programs to
read and write text (i.e., characters) rather than bytes. Used properly, readers and writers
can handle a wide variety of character encodings, including multibyte character sets
such as SJIS and UTF-8.
Streams are synchronous; that is, when a program (really a thread) asks a stream to read
or write a piece of data, it waits for the data to be read or written before it does anything
else. Java also offers nonblocking I/O using channels and buffers. Nonblocking I/O is a
little more complicated, but can be much faster in some high-volume applications, such
 
Search WWH ::




Custom Search