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.

;<br />

}<br />

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

public int read(byte[] data, int offset, int length) throws <strong>IO</strong>Exception {<br />

}<br />

int result = in.read(data, offset, length);<br />

for (int i = offset; i < offset+result; i++) {<br />

// Do nothing with the printing characters.<br />

if (data[i] == 10 || data[i] == 13 || data[i] == 9 || data[i] == -1)<br />

// nonprinting characters<br />

else if (data[i] < 32 || data[i] > 126) data[i] = (byte) '?';<br />

}<br />

return result;<br />

6.2 The Filter Stream Subclasses<br />

The java.io package contains many useful filter stream classes. The BufferedInputStream<br />

and BufferedOutputStream classes buffer reads and writes by first putting data into a buffer<br />

(an internal array of bytes). Thus, an application can read or write bytes to the stream without<br />

necessarily calling the underlying native methods. The data is read from or written into the<br />

buffer in blocks; subsequent accesses go straight to the buffer. This improves performance in<br />

many situations. Buffered input streams also allow the reader to back up and reread data.<br />

The java.io.PrintStream class, which System.out and System.err are instances of,<br />

allows very simple printing of primitive values, objects, and string literals. It uses the<br />

platform's default character encoding to convert characters into bytes. This class traps all<br />

<strong>IO</strong>Exceptions and is primarily intended for debugging. System.out and System.err are the<br />

most popular examples of the PrintStream class, but you can connect a PrintStream filter<br />

to other output streams as well. For example, you can chain a PrintStream to a<br />

FileOutputStream to easily write text into a file.<br />

The PushbackInputStream class has a one-byte pushback buffer so a program can "unread"<br />

the last character read. The next time data is read from the stream, the unread character is<br />

reread.<br />

The DataInputStream and DataOutputStream classes read and write primitive <strong>Java</strong> data<br />

types and strings in a machine-independent way. (Big-endian for integer types, IEEE-754 for<br />

floats and doubles, UTF-8 for Unicode.) These are important enough to justify a chapter of<br />

their own and will be discussed in the next chapter. The ObjectInputStream and<br />

ObjectOutputStream classes extend DataInputStream and DataOutputStream with<br />

methods to read and write arbitrary <strong>Java</strong> objects as well as primitive data types. These will be<br />

taken up in Chapter 11.<br />

The java.util.zip package also includes several filter stream classes. The filter input<br />

streams in this package decompress compressed data; the filter output streams compress raw<br />

data. These will be discussed in Chapter 9.<br />

The java.util.security package contains the DigestInputStream and<br />

DigestOutputStream filter streams; these calculate message digests of the data that passes<br />

through them. Installing the <strong>Java</strong> Cryptography Extension (JCE) adds two more filter streams<br />

to this package, CipherInputStream and CipherOutputStream , which can encrypt or<br />

decrypt data using a variety of algorithms. These will be discussed in Chapter 10.<br />

80

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

Saved successfully!

Ooh no, something went wrong!