View Javadoc

1   /*
2    * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved.
3    *
4    * This software is distributable under the BSD license. See the terms of the
5    * BSD license in the documentation provided with this software.
6    */
7   package jline;
8   
9   import java.io.*;
10  import java.util.*;
11  
12  /***
13   *  An {@link InputStream} implementation that wraps a {@link ConsoleReader}.
14   *  It is useful for setting up the {@link System#in} for a generic
15   *  console.
16   *  @author  <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
17   */
18  public class ConsoleReaderInputStream extends SequenceInputStream {
19      private static InputStream systemIn = System.in;
20  
21      public static void setIn() throws IOException {
22          setIn(new ConsoleReader());
23      }
24  
25      public static void setIn(final ConsoleReader reader) {
26          System.setIn(new ConsoleReaderInputStream(reader));
27      }
28  
29      /***
30       *  Restore the original {@link System#in} input stream.
31       */
32      public static void restoreIn() {
33          System.setIn(systemIn);
34      }
35  
36      public ConsoleReaderInputStream(final ConsoleReader reader) {
37          super(new ConsoleEnumeration(reader));
38      }
39  
40      private static class ConsoleEnumeration implements Enumeration {
41          private final ConsoleReader reader;
42          private ConsoleLineInputStream next = null;
43          private ConsoleLineInputStream prev = null;
44  
45          public ConsoleEnumeration(final ConsoleReader reader) {
46              this.reader = reader;
47          }
48  
49          public Object nextElement() {
50              if (next != null) {
51                  InputStream n = next;
52                  prev = next;
53                  next = null;
54  
55                  return n;
56              }
57  
58              return new ConsoleLineInputStream(reader);
59          }
60  
61          public boolean hasMoreElements() {
62              // the last line was null
63              if ((prev != null) && (prev.wasNull == true)) {
64                  return false;
65              }
66  
67              if (next == null) {
68                  next = (ConsoleLineInputStream) nextElement();
69              }
70  
71              return next != null;
72          }
73      }
74  
75      private static class ConsoleLineInputStream extends InputStream {
76          private final ConsoleReader reader;
77          private String line = null;
78          private int index = 0;
79          private boolean eol = false;
80          protected boolean wasNull = false;
81  
82          public ConsoleLineInputStream(final ConsoleReader reader) {
83              this.reader = reader;
84          }
85  
86          public int read() throws IOException {
87              if (eol) {
88                  return -1;
89              }
90  
91              if (line == null) {
92                  line = reader.readLine();
93              }
94  
95              if (line == null) {
96                  wasNull = true;
97                  return -1;
98              }
99  
100             if (index >= line.length()) {
101                 eol = true;
102                 return '\n'; // lines are ended with a newline
103             }
104 
105             return line.charAt(index++);
106         }
107     }
108 }