1 /*
2 * db2sa: DB2 Syntax Assistant
3 * Copyright (C) Andres Gomez Casanova
4 *
5 * This file is part of db2sa.
6 *
7 * db2sa is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * db2sa is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, see <http://www.gnu.org/licenses/>.
19 *
20 * Contact:
21 * a n g o c a at y a h o o dot c o m
22 * Cra. 45 No 61 - 31, Bogota, Colombia.
23 *
24 * Author: $LastChangedBy: angoca $:
25 * Date: $LastChangedDate: 2009-06-29 23:12:30 +0200 (Mon, 29 Jun 2009) $:
26 * Revision: $LastChangedRevision: 258 $:
27 * URL: $HeadURL: https://db2sa.svn.sourceforge.net/svnroot/db2sa/branches/db2sa_beta/source-code/src/main/java/name/angoca/db2sa/cli/system/SystemInputReader.java $:
28 */
29 package name.angoca.db2sa.cli.system;
30
31 import java.io.IOException;
32
33 import name.angoca.db2sa.cli.InputReader;
34 import name.angoca.db2sa.cli.OutputWriter;
35 import name.angoca.db2sa.cli.exceptions.InputReaderException;
36 import name.angoca.db2sa.cli.exceptions.OutputWriterException;
37
38 /**
39 * Implementation of the input reader using the System.In option. This class
40 * flushes the stream before start to read.<br/>
41 * <b>Control Version</b><br />
42 * <ul>
43 * <li>0.0.1 Class creation.</li>
44 * <li>0.0.2 No override.</li>
45 * <li>0.0.3 finals.</li>
46 * <li>1.0.0 Moved to version 1.</li>
47 * </ul>
48 *
49 * @author Andres Gomez Casanova <a
50 * href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
51 * @version 1.0.0 2009-07-19
52 */
53 public final class SystemInputReader implements InputReader {
54
55 /**
56 * Prompt to show in the console.
57 */
58 private final transient String m_prompt;
59 /**
60 * OutputWriter to write the prompt.
61 */
62 private final transient OutputWriter m_output;
63
64 /**
65 * Constructor that flushes the stream.
66 *
67 * @param outputWriter
68 * OutputWriter to write the prompt.
69 * @param prompt
70 * Prompt to show in each line of the console.
71 * @throws InputReaderException
72 * If there is a problem flushing the stream.
73 */
74 public SystemInputReader(final OutputWriter/* ! */outputWriter,
75 final String /* ! */prompt) throws InputReaderException {
76 try {
77 this.flush();
78 } catch (final IOException exception) {
79 throw new InputReaderException(exception);
80 }
81 this.m_output = outputWriter;
82 this.m_prompt = prompt;
83 }
84
85 /**
86 * Method that flushes the stream. It is a good idea to call it before to
87 * start to read.
88 *
89 * @throws IOException
90 * If there is a problem when reading.
91 */
92 private void flush() throws IOException {
93 while ((System.in.available()) != 0) {
94 System.in.read();
95 }
96 }
97
98 /*
99 * (non-Javadoc)
100 *
101 * @see name.angoca.db2sa.cli.InputReader#readString()
102 */
103 public String/* ! */readString() throws InputReaderException {
104 try {
105 this.m_output.writeString(this.m_prompt);
106 } catch (OutputWriterException exception) {
107 throw new InputReaderException(exception);
108 }
109
110 int character;
111 final StringBuffer string = new StringBuffer();
112 boolean finished = false;
113 try {
114 while (!finished) {
115 character = System.in.read();
116
117 if (character < 0 || (char) character == '\n') {
118 finished = true;
119 } else if ((char) character != '\r') {
120 string.append((char) character);
121 }
122 }
123 } catch (final IOException exception) {
124 throw new InputReaderException(exception);
125 }
126 return string.toString();
127 }
128 }