1 /**
2 * jline - Java console input library
3 * Copyright (c) 2002, 2003, 2004, 2005, Marc Prud'hommeaux <mwp1@cornell.edu>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with
16 * the distribution.
17 *
18 * Neither the name of JLine nor the names of its contributors
19 * may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
26 * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34 * OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 package jline;
37
38 import java.io.IOException;
39 import java.io.InputStream;
40
41
42 /**
43 * An {@link InputStream} implementation that wraps a {@link ConsoleReader}.
44 * It is useful for setting up the {@link System#in} for a generic
45 * console.
46 *
47 * @author <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
48 */
49 public class ConsoleReaderInputStream
50 extends InputStream
51 {
52 private static InputStream systemIn = System.in;
53
54 private final ConsoleReader reader;
55 private String line = null;
56 private int index = 0;
57 private boolean eol = false;
58 private String prompt = null;
59
60 public static void setIn ()
61 throws IOException
62 {
63 setIn (new ConsoleReader ());
64 }
65
66
67 public static void setIn (final ConsoleReader reader)
68 {
69 System.setIn (new ConsoleReaderInputStream (reader));
70 }
71
72 /**
73 * Restore the original {@link System#in} input stream.
74 */
75 public static void restoreIn ()
76 {
77 System.setIn (systemIn);
78 }
79
80
81 public ConsoleReaderInputStream (final ConsoleReader reader)
82 {
83 this.reader = reader;
84 }
85
86 /**
87 * @see java.io.InputStream#available()
88 */
89 public int available() throws IOException {
90 if ( line == null ) {
91 return 0;
92 } else if ( eol ) {
93 return line.length() - index;
94 } else {
95
96
97 return line.length() - index + 1;
98 }
99 }
100
101 /**
102 * @see java.io.InputStream#read()
103 */
104 public int read ()
105 throws IOException
106 {
107 if (eol || line == null)
108 {
109 line = reader.readLine ( (String) prompt );
110 index = 0;
111 eol = false;
112 }
113
114 if (line == null)
115 {
116 return -1;
117 }
118
119 if (index >= line.length ())
120 {
121 eol = true;
122 return '\n';
123 }
124
125 return line.charAt (index++);
126 }
127
128 /**
129 * @see java.io.InputStream#read(byte[], int, int)
130 */
131 public int read( byte[] b, int off, int len ) throws IOException {
132 if (b == null)
133 throw new NullPointerException();
134 if (off < 0 || len < 0 || off + len > b.length)
135 throw new IndexOutOfBoundsException();
136 if (len == 0)
137 return 0;
138
139 if (eol || line == null)
140 {
141 line = reader.readLine( (String) prompt );
142 index = 0;
143 eol = false;
144 }
145
146 if (line == null)
147 {
148 return -1;
149 }
150
151 int bytes = 0;
152 while (index < line.length() && bytes < len) {
153 bytes++;
154 b[off++] = (byte) line.charAt(index++);
155 }
156
157 if (index >= line.length () && bytes < len)
158 {
159 eol = true;
160 bytes++;
161 b[off++] = '\n';
162 }
163
164 return bytes;
165 }
166
167 /**
168 * Sets the prompt for this input stream. This can be changed at any point
169 * and the prompt for subsequent lines will reflect the change.
170 *
171 * @param p the new prompt
172 */
173 public void setPrompt( final String prompt ) {
174 this.prompt = prompt;
175 }
176
177 }
178