extends FilterOutputStream for printable characters : FilterOutputStream « File « Java Tutorial






import java.io.FileInputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MainClass {

  public static void main(String[] args) throws Exception {

    InputStream in = new FileInputStream("test.txt");
    OutputStream out = System.out;

    PrintableOutputStream pout = new PrintableOutputStream(out);
    for (int c = in.read(); c != -1; c = in.read()) {
      pout.write(c);
    }
    out.close();
  }
}

class PrintableOutputStream extends FilterOutputStream {

  public PrintableOutputStream(OutputStream out) {
    super(out);
  }

  public void write(int b) throws IOException {

    // carriage return, linefeed, and tab
    if (b == '\n' || b == '\r' || b == '\t')
      out.write(b);
    // non-printing characters
    else if (b < 32 || b > 126)
      out.write('?');
    // printing, ASCII characters
    else
      out.write(b);
  }

  public void write(byte[] data, int offset, int length) throws IOException {
    for (int i = offset; i < offset + length; i++) {
      this.write(data[i]);
    }
  }
}








11.21.FilterOutputStream
11.21.1.extends FilterOutputStream for printable characters
11.21.2.Capture System.out into a JFrame
11.21.3.extends FilterOutputStream
11.21.4.Count the number of bytes written to the output stream.
11.21.5.Rollover FileOutputStream
11.21.6.Provide a debug trace of the stuff thats being written out into the DataOutputStream
11.21.7.Adds extra dot if dot occurs in message body at beginning of line (according to RFC1939)
11.21.8.Apply a ASCII Hex encoding to the stream