001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: CommandLine.java$
021: * $FileID: 4335$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 4/23/03 5:23 PM$
026: * $Comment: Replaced GPL header with LGPL header.$
027: *
028: * $KeyWordsOff: $
029: */
030:
031: package org.sourcejammer.util;
032:
033: import java.io.InputStream;
034: import java.io.PrintStream;
035: import java.io.IOException;
036:
037: /**
038: * Title: SourceJammer v 0.1.0
039: * Description:
040: * Copyright: Copyright (c) 2001
041: * Company:
042: * @author Robert MacGrogan
043: * @version $Revision: 1.3 $
044: */
045:
046: public class CommandLine {
047:
048: private InputStream mstmIn;
049: private PrintStream mstmOut;
050:
051: /**
052: * Create a CommandLine object getting input from default
053: * input stream
054: */
055: public CommandLine() {
056: mstmIn = System.in;
057: mstmOut = System.out;
058: }
059:
060: /**
061: * Create a CommandLine object getting input from the passed
062: * in stream.
063: */
064: public CommandLine(InputStream stmIn, PrintStream stmOut) {
065: mstmIn = stmIn;
066: mstmOut = stmOut;
067: }
068:
069: /**
070: * Print a line to this CommandLine's output stream.
071: */
072: public void println(String s) {
073: mstmOut.println(s);
074: }
075:
076: public void print(String s) {
077: mstmOut.print(s);
078: }
079:
080: public PrintStream getPrintStream() {
081: return mstmOut;
082: }
083:
084: /**
085: * Promts users at System.out with prompt message, waits for
086: * user input, and returns user input as String.
087: */
088: public String getUserInput(String prompt) throws IOException {
089:
090: mstmOut.print(prompt + ">");
091: StringBuffer strInput = new StringBuffer();
092: boolean bKeepReading = true;
093: while (bKeepReading) {
094: int iChar = mstmIn.read();
095: char ch = (char) iChar;
096: if (ch != '\n') {
097: strInput.append(ch);
098: } else {
099: bKeepReading = false;
100: }
101: }
102: return strInput.toString();
103: }
104:
105: }
|