001    /*
002     * $Id: Console.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $
003     *
004     * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group.
005     * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the
006     * CS101 homepage</a> or email <las@ai.mit.edu>.
007     *
008     * Copyright (C) 1998 Massachusetts Institute of Technology.
009     * Please do not redistribute without obtaining permission.
010     */
011    package cs101.io;
012    import java.io.*;
013    
014    /**
015     * A helper class for Java Console IO.  Contains appropriate console read
016     * and print methods.
017     *
018     * <P>Exists mostly to make System.In palatable by transforming it into a 
019     * readLine-able BufferedReader.  Out is included for completeness
020     * and uniformity.
021     *
022     * <P>Copyright 1998 Massachusetts Institute of Technology
023     *
024     * @see      java.lang.System
025     *
026     * @author   Lynn Andrea Stein, las@ai.mit.edu
027     * @version  $Id: Console.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $
028     * 
029     */
030    public class Console {
031    
032      /** A console Reader; e.g., allows you to readLine() from the console. */
033      private static final BufferedReader in = 
034        new BufferedReader(new InputStreamReader(System.in));
035      
036      /** A console Writer; e.g., allows you to println() to the console */
037      private static final PrintWriter out = 
038        new PrintWriter ( System.out , true);
039      
040      /** The special console Writer for error messages. */
041      private static final PrintWriter err = 
042        new PrintWriter( System.err , true);
043      
044      /** Read a line from Console.  The String returned is not terminated
045       * with a newline. */  
046      public static final String readln() {
047        try {
048          return Console.in.readLine();
049        } catch (java.io.IOException ioe) {
050          // Something strange has happened.  Report error and continue.
051          Console.err.println("Console.readLine() threw IOException.  Has System.in been redirected?");
052          return "";
053        }
054      }
055    
056      /** Write a line to Console.  The String writen is concatenated with a 
057       * newline. */
058      public static final void println(String s) {
059        Console.out.println(s);
060      }
061    
062      /** Write a blank line to Console. */
063      public static final void println() {
064        Console.out.println();
065      }
066    
067      /** Write a line to Console.  The String written is not concatenated with
068       * a newline. */
069      public static final void print(String s) {
070        Console.out.print(s);
071        Console.out.flush();
072      }
073    
074      /**
075       * Prevent instantiation
076       */
077      private Console() {}
078    }
079    
080    /* Comments:
081     *
082     * History:
083     *     $Log: Console.java,v $
084     *     Revision 1.1.1.1  2002/06/05 21:56:32  root
085     *     CS101 comes to Olin finally.
086     *
087     *     Revision 1.10  1999/07/28 22:22:04  las
088     *     Added files providing utility classes for stringTransformer pset
089     *     (mostly interfaces concerning connectors).  Also modified Console to
090     *     fix a bug in print (i.e., needed to force flush()).
091     *
092     *     Revision 1.9  1999/01/25 15:30:03  tparnell
093     *     fix javadoc to reflect file movement between packages
094     *
095     *     Revision 1.8  1998/08/11 22:30:54  tparnell
096     *     added println() [no args]
097     *
098     *     Revision 1.7  1998/07/28 18:55:54  tparnell
099     *     removed refrences to (now defunct) java.io.FriendlyReader
100     *
101     *     Revision 1.6  1998/07/24 17:07:29  tparnell
102     *     Placate new javadoc behavior
103     *
104     *     Revision 1.5  1998/07/22 20:24:46  tparnell
105     *     made in, out, and err private
106     *
107     *     Revision 1.4  1998/07/22 15:37:13  tparnell
108     *     moved to cs101.io
109     *
110     *     Revision 1.3  1998/07/21 19:24:54  tparnell
111     *     added private Console()
112     *
113     *     Revision 1.2  1998/07/20 21:57:02  tparnell
114     *     Added readLine(), print(), and println()
115     *
116     *     Revision 1.1  1998/06/08 18:11:52  tparnell
117     *     added files from Lynn
118     *
119     */
120