By Elliotte Rusty Harold
Book Price: $39.95 USD
£28.50 GBP
PDF Price: $31.99
Cover | Table of Contents | Colophon
SecurityManager, this restriction is a property of
the Java language itself and the byte code verifier.System.exec( ) or
Runtime.exec( ).System.getProperty( ) in a way that reveals
information about the user or the user's machine,
such as a username or home directory. It may use
http://www.faqs.org/rfc/ and http://www.ietf.org/rfc.html. For the most
part RFCs, particularly standards-oriented RFCs, are very technical,
turgid, and nearly incomprehensible. Nonetheless, they are often the
only complete and reliable source of information about a particular
protocol.
scheme:scheme-specific-part
scheme:scheme-specific-part
<H1> in HTML).
Likewise, you don't say that a word should be placed
in italics. Rather, you say it should be emphasized
(<EM> in HTML). It is left to the browser to
determine how to best display headings or emphasized text.<STRONG> is the same as
<strong> is the same as
<Strong> is the same as
<StrONg>. Some tags have a matching end-tag
to define a region of text. An end-tag is the same as the start-tag,
except that the opening angle bracket is followed by a
/. For example: <STRONG>this text
is
strong</STRONG>;
<EM>this text is
emphasized</EM>. The entire text from the
beginning of the start-tag to the end of the end-tag is called an
element
.
Thus, <STRONG>this text is
strong</STRONG> is a STRONG
element.<STRONG><EM>Jack and Jill went up the hill</EM></STRONG> <STRONG><EM>to fetch a pail of water</STRONG></EM>
<H1>
tag and most other paragraph-level tags may have an
GET /index.html HTTP/1.0
GET specifies the operation being requested. The
operation requested here is for the server to return a representation
of a resource. /index.html is a relative URL that
identifies the resource requested from the server. This resource is
assumed to reside on the machine that receives the request, so there
is no need to prefix it with
http://www.thismachine.com/.HTTP/1.0
is the version of the protocol that the client understands. The
request is terminated with two carriage return/linefeed pairs
(\r\n\r\n in Java parlance), regardless of how
lines are terminated on the client or server platform.GET line is all that is required, a
client request can include other information as well. This takes the
following form:
Keyword: Value
Accept, which
tells the server what kinds of data the client can handle (though
servers often ignore this). For example, the following line says that
the client can handle four MIME media types, corresponding to HTML
documents, plain text, and JPEG and GIF images:text/html; the type is
text, and the subtype is html.
The content type for a GIF image is image/gif; the
type is image, and the subtype is
gif. Table 3-2 lists the more common defined
content types. On most systems, a simple text file maintains a
mapping between MIME types and the application used to process that
type of data; on Unix, this file is called
mime.types. The most current list of registered
MIME types is available from http://www.iana.org/assignments/media-types/.
For more on MIME, see the comp.mail.mime FAQ at http://www.uni-giessen.de/faq/archiv/mail.mime-faq.part1-9/.text/html and text/plain, and
two image formats, image/gif and
image/jpeg. More recent browsers also understand
application/xml and several other image formats.
Java relies on MIME types to pick the appropriate content handler for
a particular stream of data.java.io.FileInputStream and
sun.net.TelnetOutputStream, 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.java.io.DataOutputStream class
provides a method that converts an int to four
bytes and writes those bytes onto its underlying output stream.java.io.OutputStream:public abstract class OutputStream
public abstract void write(int b) throws IOException public void write(byte[] data) throws IOException public void write(byte[] data, int offset, int length) throws IOException public void flush( ) throws IOException public void close( ) throws IOException
OutputStream use these methods to
write data onto particular media. For instance, a
FileOutputStream uses these methods to write data
into a file. A TelnetOutputStream uses these
methods to write data onto a network connection. A
ByteArrayOutputStream uses these methods to write
data into an expandable byte array. But whichever medium
you're writing to, you mostly use only these same
five methods. Sometimes you may not even know exactly what kind of
stream you're writing onto. For instance, you
won't find TelnetOutputStream in
the Java class library documentation. It's
deliberately hidden inside the sun packages.
It's returned by various methods in various classes
in java.net, like the getOutputStream() method of java.net.Socket. However,
these methods are declared to return only
OutputStream, not the more specific subclass
TelnetOutputStream. That's the
power of polymorphism. If you know how to use the superclass, you
know how to use all the subclasses, too.OutputStream's fundamental method
is write(int
b). This method
takes an integer from 0 to 255 as an argument and writes the
corresponding byte to the output stream. This method is declared
abstract because subclasses need to change it to handle their
particular medium. For instance, a
ByteArrayOutputStream can implement this method
with pure Java code that copies the byte into its array. However, a
FileOutputStream will need to use native code that
understands how to write data in files on the host platform.intjava.io.InputStream:public abstract class InputStream
public abstract int read( ) throws IOException public int read(byte[] input) throws IOException public int read(byte[] input, int offset, int length) throws IOException public long skip(long n) throws IOException public int available( ) throws IOException public void close( ) throws IOException
InputStream use these
methods to read data from particular media. For instance, a
FileInputStream reads data from a file. A
TelnetInputStream reads data from a network
connection. A ByteArrayInputStream reads data from
an array of bytes. But whichever source you're
reading, you mostly use only these same six methods. Sometimes you
don't know exactly what kind of stream
you're reading from. For instance,
TelnetInputStream is an undocumented class hidden
inside the sun.net package. Instances of it are
returned by various methods in the java.net
package: for example, the openStream( ) method of
java.net.URL. However, these methods are declared
to return only InputStream, not the more specific
subclass TelnetInputStream.
That's polymorphism at work once again. The instance
of the subclass can be used transparently as an instance of its
superclass. No specific knowledge of the subclass is required.InputStream is the noargs
read( )
method. This method reads a single byte of data from the input
stream's source and returns it as an
int from 0 to 255. End of stream is signified by
returning -1. The read( ) method waits and blocks
execution of any code that follows it until a byte of data is
available and ready to be read. Input and output can be slow, so if
your program is doing anything else of importance, try to put I/O in
its own thread.read( ) method is declared abstract because
subclasses need to change it to handle their particular medium. For
instance, a InputStream
and OutputStream
are fairly raw classes. They read and write bytes singly or in
groups, but that's all. Deciding what those bytes
mean—whether they're integers or IEEE 754
floating point numbers or Unicode text—is completely up to the
programmer and the code. However, there are certain extremely common
data formats that can benefit from a solid implementation in the
class library. For example, many integers passed as parts of network
protocols are 32-bit big-endian integers. Much text sent over the Web
is either 7-bit ASCII, 8-bit Latin-1, or multi-byte UTF-8. Many files
transferred by FTP are stored in the zip format. Java provides a
number of filter classes you can attach to raw streams to translate
the raw bytes to and from these and other formats.TelnetInputStream or a
FileOutputStream or other filter streams. Readers
and writers can be layered on top of raw streams, filter streams, or
other readers and writers. However, filter streams cannot be placed
on top of a reader or a writer, so we'll start with
filter streams and address readers and writers in the next section.TelnetInputStream. A
BufferedInputStream buffers the data to speed up
the entire process. A CipherInputStream decrypts
the data. A GZIPInputStream decompresses the
deciphered data. An java.io.Reader class specifies the API by which
characters are read. The java.io.Writer class
specifies the API by which characters are written. Wherever input and
output streams use bytes, readers and writers use Unicode characters.
Concrete subclasses of Reader and
Writer allow particular sources to be read and
targets to be written. Filter readers and writers can be attached to
other readers and writers to provide additional services or
interfaces.Reader
and Writer are the
InputStreamReader
and the
OutputStreamWriter classes. An
InputStreamReader contains an underlying input
stream from which it reads raw bytes. It translates these bytes into
Unicode characters according to a specified encoding. An
OutputStreamWriter receives Unicode characters
from a running program. It then translates those characters into
bytes using a specified encoding and writes the bytes onto an
underlying output stream.java.io
package provides several raw reader and writer classes that read
characters without directly requiring an underlying input stream,
including:% ftp eunl.java.sun.com Connected to eunl.javasoft.com. 220 softwarenl FTP server (wu-2.4.2-academ[BETA- 16]+opie-2.32(1) 981105) ready. Name (eunl.java.sun.com:elharo): anonymous 530- 530- Server is busy. Please try again later or try one of our other 530- ftp servers at ftp.java.sun.com. Thank you. 530- 530 User anonymous access denied. Login failed.
Thread with
a capital T is an instance of the
java.lang.Thread
class. There is a one-to-one relationship
between threads executing in the virtual machine and
Thread objects constructed by the virtual machine.
Most of the time it's obvious from the context which
one is meant if the difference is really important. To start a new
thread running in the virtual machine, you construct an instance of
the Thread class and invoke its start() method, like this:Thread t = new Thread( ); t.start( );
Thread class and override its run() method, or implement the Runnable
interface and pass the Runnable object to the
Thread constructor. I generally prefer the second
option since it separates the task that the thread performs from the
thread itself more cleanly, but you will see both techniques used in
this book and elsewhere. In both cases, the key is the run()
method, which has this signature:public void run( )
run() method completes, the thread dies. In essence, the
run( ) method is to a thread what the
main( ) method is to a traditional nonthreaded
program. A single-threaded program exits when the main() method returns. A multithreaded program exits when both
the main( ) method and the run() methods of all nondaemon threads return. (Daemon threads
perform background tasks such as garbage collection and
don't prevent the virtual machine from exiting.)