KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jline > ConsoleReaderInputStream


1 /**
2  * jline - Java console input library
3  * Copyright (c) 2002-2006, 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.*;
39 import java.util.*;
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 SequenceInputStream
51 {
52     private static InputStream systemIn = System.in;
53
54
55     public static void setIn ()
56         throws IOException
57     {
58         setIn (new ConsoleReader ());
59     }
60
61
62     public static void setIn (final ConsoleReader reader)
63     {
64         System.setIn (new ConsoleReaderInputStream (reader));
65     }
66
67
68     /**
69      * Restore the original {@link System#in} input stream.
70      */

71     public static void restoreIn ()
72     {
73         System.setIn (systemIn);
74     }
75
76
77     public ConsoleReaderInputStream (final ConsoleReader reader)
78     {
79         super (new ConsoleEnumeration (reader));
80     }
81
82
83     private static class ConsoleEnumeration
84         implements Enumeration
85     {
86         private final ConsoleReader reader;
87         private ConsoleLineInputStream next = null;
88         private ConsoleLineInputStream prev = null;
89
90
91         public ConsoleEnumeration (final ConsoleReader reader)
92         {
93             this.reader = reader;
94         }
95
96
97         public Object JavaDoc nextElement ()
98         {
99             if (next != null)
100             {
101                 InputStream n = next;
102                 prev = next;
103                 next = null;
104                 return n;
105             }
106
107             return new ConsoleLineInputStream (reader);
108         }
109
110
111         public boolean hasMoreElements ()
112         {
113             // the last line was null
114
if (prev != null && prev.wasNull == true)
115                 return false;
116
117             if (next == null)
118                 next = (ConsoleLineInputStream)nextElement ();
119
120             return next != null;
121         }
122     }
123
124
125     private static class ConsoleLineInputStream
126         extends InputStream
127     {
128         private final ConsoleReader reader;
129         private String JavaDoc line = null;
130         private int index = 0;
131         private boolean eol = false;
132         protected boolean wasNull = false;
133
134         public ConsoleLineInputStream (final ConsoleReader reader)
135         {
136             this.reader = reader;
137         }
138
139
140         public int read ()
141             throws IOException
142         {
143             if (eol)
144                 return -1;
145
146             if (line == null)
147                 line = reader.readLine ();
148
149             if (line == null)
150             {
151                 wasNull = true;
152                 return -1;
153             }
154
155             if (index >= line.length ())
156             {
157                 eol = true;
158                 return '\n'; // lines are ended with a newline
159
}
160
161             return line.charAt (index++);
162         }
163     }
164 }
165
166
Popular Tags