Corrections to Chapter 3 of Java I/O, Input Streams

p. 38: The while loop in the code fragment at the bottom of the page is missing a closing }. It should be

try {
  byte[] b = new byte[100];
  int offset = 0;
  while (offset < b.length) {
    int a = System.in.available();
    int bytesRead = System.in.read(b, offset, a);
    if (bytesRead == -1) break; // end of stream
    offset += bytesRead;
  }
}
catch (IOException e) {
  System.err.println("Couldn't read from System.in!");
}
p. 40: In the first code example in.strip should of course be in.skip.

p. 43, 44: Delete the last paragraph on p. 44. It's incorrect. Also delete the comment in the copy() method on p. 43. You may also want to remove the synchronization from the copy() method like this:

  public static void copy(InputStream in, OutputStream out) 
   throws IOException {

    byte[] buffer = new byte[256];
    while (true) {
      int bytesRead = in.read(buffer);
      if (bytesRead == -1) break;
      out.write(buffer, 0, bytesRead);
    }
  }
It really doesn't help. See the June 3, 1999 Question of the Week for an explanation.


[ 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 August 5, 1999