Your browser does not support JavaScript and this site utilizes JavaScript to build content and provide links to additional information. You should either enable JavaScript in your browser settings or use a browser that supports JavaScript in order to take full advantage of this site.
1 36 package jline; 37 38 import java.io.*; 39 import java.util.*; 40 41 42 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 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 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 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 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'; } 160 161 return line.charAt (index++); 162 } 163 } 164 } 165 166
| Popular Tags
|