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.

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

System.out, System.err, and some (but not all) other print streams automatically flush after<br />

each call to println() and after each time a new line character ('\n') appears in the string<br />

being written. Whether auto-flushing is enabled can be set in the PrintStream constructor.<br />

2.5 Subclassing OutputStream<br />

OutputStream is an abstract class that mainly describes the operations available with any<br />

particular OutputStream object. Specific subclasses know how to write bytes to particular<br />

destinations. For instance, a FileOutputStream uses native code to write data in files. A<br />

ByteArrayOutputStream uses pure <strong>Java</strong> to write its output in a potentially expanding byte<br />

array.<br />

Recall that there are three overloaded variants of the write() method in OutputStream, one<br />

abstract, two concrete:<br />

public abstract 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 />

Subclasses must implement the abstract write(int b) method. They often choose to override<br />

the third variant, write(byte[], data int offset, int length), for reasons of<br />

performance. The implementation of the three-argument version of the write() method in<br />

OutputStream simply invokes write(int b) repeatedly; that is:<br />

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

for (int i = offset; i < offset+length; i++) write(data[i]);<br />

}<br />

Most subclasses can provide more efficient implementations of this method. The oneargument<br />

variant of write() merely invokes write(data, 0, data.length); if the threeargument<br />

variant has been overridden, this method will perform reasonably well. However, a<br />

few subclasses may override it anyway.<br />

Example 2.3 is a simple program called NullOutputStream that mimics the behavior of<br />

/dev/null on Unix operating systems. Data written into a null output stream is lost.<br />

Example 2.3. The NullOutputStream Class<br />

package com.macfaq.io;<br />

import java.io.*;<br />

public class NullOutputStream extends OutputStream {<br />

}<br />

public void write(int b) { }<br />

public void write(byte[] data) { }<br />

public void write(byte[] data, int offset, int length) { }<br />

By redirecting System.out and System.err to a null output stream in the shipping version of<br />

your program, you can disable any debugging messages that might have slipped through<br />

quality assurance. For example:<br />

38

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

Saved successfully!

Ooh no, something went wrong!