Java Reference
In-Depth Information
this were not so, a malicious Java applet embedded in a web page could trash your hard disk. An IOExcep-
tion is normally thrown by any attempted operation on disk files on the local machine in a Java applet. The
directory containing the class file for the applet and its subdirectories are freely accessible to the applet.
Also, the security features in Java can be used to control what an applet (and an application running under
a Security Manager) can access so that an applet can access only files or other resources for which it has
explicit permission .
The main reason for using a stream as the basis for input and output operations is to make your program
code for these operations independent of the device involved. This has two advantages. First, you don't have
to worry about the detailed mechanics of each device, which are taken care of behind the scenes. Second,
your program works for a variety of input/output devices without any changes to the code.
Stream input and output methods generally permit very small amounts of data, such as a single character
or byte, to be written or read in a single operation. Transferring data to or from a stream like this may be
extremely inefficient, so a stream is often equipped with a buffer in memory, in which case it is called a
buffered stream . A buffer is simply a block of memory that is used to batch up the data that is transferred to
or from an external device. Reading or writing a stream in reasonably large chunks reduces the number of
input/output operations necessary and thus makes the process more efficient.
When you write to a buffered output stream, the data is sent to the buffer and not to the external device.
The amount of data in the buffer is tracked automatically, and the data is usually sent to the device when the
buffer is full. However, you will sometimes want the data in the buffer to be sent to the device before the
buffer is full, and methods are provided to do this. This operation is usually termed flushing the buffer.
Buffered input streams work in a similar way. Any read operation on a buffered input stream reads data
from the buffer. A read operation for the device that is the source of data for the stream is read only when
the buffer is empty and the program has requested data. When this occurs, a complete buffer-full of data is
read into the buffer automatically from the device, or less if insufficient data is available.
Binary and Character Streams
The java.io package supports two types of streams — binary streams , which contain binary data, and
character streams , which contain character data. Binary streams are sometimes referred to as byte streams .
These two kinds of streams behave in different ways when you read and write data.
When you write data to a binary stream, the data is written to the stream as a series of bytes, exactly as
it appears in memory. No transformation of the data takes place. Binary numerical values are just written as
a series of bytes, 4 bytes for each value of type int , 8 bytes for each value of type long , 8 bytes for each
value of type double , and so on. As you saw in Chapter 2, Java stores its characters internally as Unicode
characters, which are 16-bit characters, so each Unicode character is written to a binary stream as 2 bytes,
the high byte being written first. Supplementary Unicode characters that are surrogates consist of two suc-
cessive 16-bit characters, in which case the two sets of 2 bytes are written in sequence to the binary stream
with the high byte written first in each case.
Character streams are used for storing and retrieving text. You may also use character streams to read text
files not written by a Java program. All binary numeric data has to be converted to a textual representation
before being written to a character stream. This involves generating a character representation of the origin-
al binary data value. Reading numeric data from a stream that contains text involves much more work than
reading binary data. When you read a value of type int from a binary stream, you know that it consists of 4
bytes. When you read an integer from a character stream, you have to determine how many characters from
the stream make up the value. For each numerical value you read from a character stream, you have to be
Search WWH ::




Custom Search