49 public synchronized InputStream setInputStream(InputStream input) { |
49 public synchronized InputStream setInputStream(InputStream input) { |
50 if (initialized) |
50 if (initialized) |
51 throw new IllegalStateException("Already initialized."); |
51 throw new IllegalStateException("Already initialized."); |
52 initialized = true; |
52 initialized = true; |
53 |
53 |
54 Thread reader = new Thread() { |
54 Thread reader = new Thread(() -> { |
55 @Override |
55 try { |
56 public void run() { |
56 int read; |
57 try { |
57 while (true) { |
58 int read; |
58 //to support external terminal editors, the "cmdin.read" must not run when |
59 while (true) { |
59 //an external editor is running. At the same time, it needs to run while the |
60 //to support external terminal editors, the "cmdin.read" must not run when |
60 //user's code is running (so Ctrl-C is detected). Hence waiting here until |
61 //an external editor is running. At the same time, it needs to run while the |
61 //there is a confirmed need for input. |
62 //user's code is running (so Ctrl-C is detected). Hence waiting here until |
62 State currentState = waitInputNeeded(); |
63 //there is a confirmed need for input. |
63 if (currentState == State.CLOSED) { |
64 StopDetectingInputStream.State currentState = waitInputNeeded(); |
64 break; |
65 if (currentState == StopDetectingInputStream.State.CLOSED) { |
|
66 break; |
|
67 } |
|
68 if ((read = input.read()) == (-1)) { |
|
69 break; |
|
70 } |
|
71 if (read == 3 && currentState == StopDetectingInputStream.State.BUFFER) { |
|
72 stop.run(); |
|
73 } else { |
|
74 write(read); |
|
75 } |
|
76 } |
65 } |
77 } catch (IOException ex) { |
66 if ((read = input.read()) == (-1)) { |
78 errorHandler.accept(ex); |
67 break; |
79 } finally { |
68 } |
80 shutdown(); |
69 if (read == 3 && currentState == State.BUFFER) { |
|
70 stop.run(); |
|
71 } else { |
|
72 write(read); |
|
73 } |
81 } |
74 } |
|
75 } catch (IOException ex) { |
|
76 errorHandler.accept(ex); |
|
77 } finally { |
|
78 shutdown(); |
82 } |
79 } |
83 }; |
80 }); |
84 reader.setDaemon(true); |
81 reader.setDaemon(true); |
85 reader.start(); |
82 reader.start(); |
86 |
83 |
87 return this; |
84 return this; |
88 } |
85 } |