Corrections to Chapter 6 of Java I/O, Filter Streams

p. 78: The FilterOutputStream constructor is public, not protected. Surpisingly the FilterInputStream constructor is protected. I'm not sure why there's this discrepancy between the two.

pp. 82-83: Example 6-3, PrintableInputStream, eats the end of file indicator, -1. Here's a corrected version that also adds some test code and better comments:

package com.macfaq.io;

import java.io.*;


public class PrintableInputStream extends FilterInputStream {

  public PrintableInputStream(InputStream in) {
    super(in);
  }

  public int read() throws IOException {
  
    int b = in.read();
    // printing, ASCII characters
    if (b >= 32 && b <= 126) return b;
    // carriage return, linefeed, tab, and end of file
    else if (b == 10 || b == 13 || b == 9 || b == -1) return b;
    // non-printing characters
    else return '?'; 

  }

  public int read(byte[] data, int offset, int length) throws IOException {
  
    int result = in.read(data, offset, length);
    for (int i = offset; i < offset+result; i++) {
      // do nothing with the printing characters
      // carriage return, linefeed, tab, and end of file
      if (data[i] == 10 || data[i] == 13 || data[i] == 9 || data[i] == -1) ;
      // non-printing characters
      else if (data[i] < 32 || data[i] > 126) data[i] = (byte) '?';
    }
    return result;
    
  }

  // test the stream 
  public static void main (String[] args) throws IOException {
	
    for (int i = 0; i < args.length; i++) {
	  FileInputStream fin = new FileInputStream(args[i]);
      PrintableInputStream pin = new PrintableInputStream(fin);
      while (true) {
        int c = pin.read();
        if (c == -1) break;
        System.out.write(c);
      }
			
    }
	
  }

}
p. 86: In the second paragraph on the page "the copy method" should be "the copy() method".

p. 87: In the second paragraph, "the index of the next byte that will be returned by read()" should be "the index of the next place in the array where a byte will be stored"

p. 100: In Example 6-12:

    if (args.length < 1) {
      System.err.println("Usage: java FileDumper [-ahd] file1 file2...");
    }
should be

    if (args.length < 1) {
      System.err.println("Usage: java FileDumper2 [-ahd] file1 file2...");
      return;
    }

[ Java I/O Corrections | Java I/O Home Page | Table of Contents | Examples | Order from Amazon ] ]

Copyright 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified October 13, 2001