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.

public int read() throws <strong>IO</strong>Exception<br />

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

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

public long skip(long n) throws <strong>IO</strong>Exception<br />

public int available() throws <strong>IO</strong>Exception<br />

public void close() throws <strong>IO</strong>Exception<br />

public synchronized void mark(int readlimit)<br />

public synchronized void reset() throws <strong>IO</strong>Exception<br />

public boolean markSupported()<br />

For FilterOutputStream, these are:<br />

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

public void write(int b) throws <strong>IO</strong>Exception<br />

public void write(byte[] data) throws <strong>IO</strong>Exception<br />

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

public void flush() throws <strong>IO</strong>Exception<br />

public void close() throws <strong>IO</strong>Exception<br />

Each of these methods merely passes its arguments to the corresponding method in the<br />

underlying stream. For example, the skip() method in FilterInputStream behaves like<br />

this:<br />

public long skip(long n) throws <strong>IO</strong>Exception {<br />

in.skip(n);<br />

}<br />

The close() method in FilterOutputStream behaves like this:<br />

public void close() throws <strong>IO</strong>Exception {<br />

out.close();<br />

}<br />

Thus, closing a filter stream closes the underlying stream. You cannot close one filter stream<br />

and then open up another on the same underlying stream, nor can you close one filter stream<br />

in a chain but still read from the underlying stream or other streams in the chain. Attempts to<br />

do so will throw <strong>IO</strong>Exceptions. Once a stream is closed—no matter by which filter stream<br />

it's chained to—it's closed for good.<br />

Since the constructors are protected, you don't use these classes directly. Instead, you create<br />

subclasses and use those. Since FilterOutputStream does not have a no-argument<br />

constructor, it's essential to give all subclasses explicit constructors and use super() to<br />

invoke the FilterOutputStream constructor. Your subclass will probably also want to<br />

override the write(int b) and write(byte[] data, int offset, int length) methods to<br />

perform its filtering. The write(byte[] data) method merely invokes write(data, 0,<br />

data.length), so if you've overridden the three-argument write() method, you probably<br />

don't need to also override write(byte[] data). Depending on circumstances, you may or<br />

may not need to override some of the other methods.<br />

The PrintableOutputStream class shown in Example 6.1 is an example subclass of<br />

FilterOutputStream that truncates all data to the range of printable ASCII characters: byte<br />

values 32-126, plus 9, 10, and 13 (tab, linefeed, and carriage return). Every time a byte in that<br />

range is passed to write(), it is written onto the underlying output stream, out. Every time a<br />

byte outside that range is passed to write(), a question mark is written onto the underlying<br />

76

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

Saved successfully!

Ooh no, something went wrong!