Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This noncompliant code example creates multiple BufferedInputStream wrappers on System.in, even though there is only one declaration of a BufferedInputStream. The getChar() method creates a new BufferedInputStream each time it is called. Data that are is read from the underlying stream and placed in the buffer during execution of one call cannot be replaced in the underlying stream so that a second call has access to themit. Consequently, data that remain remains in the buffer at the end of a particular execution of getChar() are is lost. Although this noncompliant code example uses a BufferedInputStream, any buffered wrapper is unsafe; this condition is also exploitable when using a Scanner, for example.

Code Block
bgColor#FFCCCC
public final class InputLibrary {
  public static char getChar() throws EOFException, IOException {
    BufferedInputStream in = new BufferedInputStream(System.in); // wrapper
    int input = in.read();
    if (input == -1) {
      throw new EOFException();
    }
    // Down casting is permitted because InputStream guarantees read() in range
    // 0..255 if it is not -1
    return (char) input;
  }

  public static void main(String[] args) {
    try {
      // Either redirect input from the console or use
      // System.setIn(new FileInputStream("input.dat"));
      System.out.print("Enter first initial: ");
      char first = getChar();
      System.out.println("Your first initial is " + first);
      System.out.print("Enter last initial: ");
      char last = getChar();
      System.out.println("Your last initial is " + last);
    } catch (EOFException e) {
      System.err.println("ERROR");
      // Forward to handler
    } catch (IOException e) {
      System.err.println("ERROR");
      // Forward to handler
    }
  }
}
Implementation Details (POSIX)

This program was When compiled under Java 1.6.0 . When and run from the command line, the this program successfully takes two characters as input and prints them out. However, when run with a file redirected to standard input, the program throws an EOFException because the second call to getChar() finds no characters to read upon encountering the end of the stream.

...

Code Block
bgColor#ccccff
public final class InputLibrary {
  private static BufferedInputStream in =
      new BufferedInputStream(System.in);

  public static char getChar() throws EOFException, IOException {
    int input = in.read();
    if (input == -1) {
      throw new EOFException();
    }
    in.skip(1); // This statement is to advance to the next line
                // The noncompliant code example deceptively
                // appeared to work without it (in some cases)
    return (char) input;
  }

  public static void main(String[] args) {
    try {
      System.out.print("Enter first initial: ");
      char first = getChar();
      System.out.println("Your first initial is " + first);
      System.out.print("Enter last initial: ");
      char last = getChar();
      System.out.println("Your last initial is " + last);
    } catch (EOFException e) {
      System.err.println("ERROR");
      // Forward to handler
    } catch (IOException e) {
       System.err.println("ERROR");
       // Forward to handler
    }
  }
}
Implementation Details (POSIX)

This program was When compiled under Java 1.6.0 . When and run from the command line, the this program successfully takes two characters as input and prints them out. Unlike the noncompliant code example, this program also produces correct output when run with a file redirected to standard input.

Compliant Solution (Accessible Class Variable)

If a program intends to use This compliant solution uses both System.in and the InputLibrary class which creates a buffered wrapper around System.in as well as . Because the InputLibrary class and the remainder of the program must share a single buffered wrapper, the program the InputLibrary class must export a reference to that wrapper. Code outside the InputLibrary class must use the same buffered wrapper as does this class exported wrapper rather than creating and using its own additional buffered wrapper . Consequently, the InputLibrary class must make available a reference to the buffered wrapper to support this functionalityaround System.in.

Code Block
bgColor#ccccff
public final class InputLibrary {
  private static BufferedInputStream in =
     new BufferedInputStream(System.in);

  static BufferedInputStream getBufferedWrapper() {
    return in;
  }

  // ...other methods
}


// Some code that requires user input from System.in
class AppCode {
  private static BufferedInputStream in;

  AppCode() {
    in = InputLibrary.getBufferedWrapper();
  }

  // ...other methods
}

Note that reading from a stream is not a thread-safe operation by default; consequently, this scheme may not work well compliant solution may be inappropriate in multithreaded environments. In such cases, explicit synchronization is required.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="6ec8ff256997b249-b40d60d3-49134729-a12f848f-44e7f771d86f8b1749f8de6a"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

[Method read

http://java.sun.com/javase/6/docs/api/java/io/InputStream.html#read()]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7b2912995d03db6a-0f1621e3-469f478b-8aa9a857-353baaa668e54daf3376f50f"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

[Class BufferedInputStream

http://java.sun.com/javase/6/docs/api/java/io/BufferedInputStream.html]

]]></ac:plain-text-body></ac:structured-macro>

...