author | jlahoda |
Thu, 13 Dec 2018 08:26:07 +0100 | |
changeset 53003 | ff1c86e85d02 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
34752
9c262a013456
8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents:
33362
diff
changeset
|
25 |
|
33362 | 26 |
package jdk.internal.jshell.tool; |
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.util.function.Consumer; |
|
31 |
||
32 |
public final class StopDetectingInputStream extends InputStream { |
|
33 |
public static final int INITIAL_SIZE = 128; |
|
34 |
||
35 |
private final Runnable stop; |
|
36 |
private final Consumer<Exception> errorHandler; |
|
37 |
||
38 |
private boolean initialized; |
|
39 |
private int[] buffer = new int[INITIAL_SIZE]; |
|
40 |
private int start; |
|
41 |
private int end; |
|
42 |
private State state = State.WAIT; |
|
43 |
||
44 |
public StopDetectingInputStream(Runnable stop, Consumer<Exception> errorHandler) { |
|
45 |
this.stop = stop; |
|
46 |
this.errorHandler = errorHandler; |
|
47 |
} |
|
48 |
||
49 |
public synchronized InputStream setInputStream(InputStream input) { |
|
50 |
if (initialized) |
|
51 |
throw new IllegalStateException("Already initialized."); |
|
52 |
initialized = true; |
|
53 |
||
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
54 |
Thread reader = new Thread(() -> { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
55 |
try { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
56 |
int read; |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
57 |
while (true) { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
58 |
//to support external terminal editors, the "cmdin.read" must not run when |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
59 |
//an external editor is running. At the same time, it needs to run while the |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
60 |
//user's code is running (so Ctrl-C is detected). Hence waiting here until |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
61 |
//there is a confirmed need for input. |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
62 |
State currentState = waitInputNeeded(); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
63 |
if (currentState == State.CLOSED) { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
64 |
break; |
33362 | 65 |
} |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
66 |
if ((read = input.read()) == (-1)) { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
67 |
break; |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
68 |
} |
43135
0c7c13fa7bee
8171385: jshell tool: unresponsive to ctrl-C in input wait on Windows
jlahoda
parents:
42827
diff
changeset
|
69 |
if (read == 3 && getState() == State.BUFFER) { |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
70 |
stop.run(); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
71 |
} else { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
72 |
write(read); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
73 |
} |
33362 | 74 |
} |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
75 |
} catch (IOException ex) { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
76 |
errorHandler.accept(ex); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
77 |
} finally { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
78 |
shutdown(); |
33362 | 79 |
} |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
41518
diff
changeset
|
80 |
}); |
33362 | 81 |
reader.setDaemon(true); |
82 |
reader.start(); |
|
83 |
||
84 |
return this; |
|
85 |
} |
|
86 |
||
87 |
@Override |
|
88 |
public synchronized int read() { |
|
89 |
while (start == end) { |
|
90 |
if (state == State.CLOSED) { |
|
91 |
return -1; |
|
92 |
} |
|
93 |
if (state == State.WAIT) { |
|
94 |
state = State.READ; |
|
95 |
} |
|
96 |
notifyAll(); |
|
97 |
try { |
|
98 |
wait(); |
|
99 |
} catch (InterruptedException ex) { |
|
100 |
//ignore |
|
101 |
} |
|
102 |
} |
|
103 |
try { |
|
104 |
return buffer[start]; |
|
105 |
} finally { |
|
106 |
start = (start + 1) % buffer.length; |
|
107 |
} |
|
108 |
} |
|
109 |
||
38909
80e42e2d475b
8158174: jshell tool: leaks threads waiting on StopDetectingInputStream
jlahoda
parents:
34752
diff
changeset
|
110 |
public synchronized void shutdown() { |
80e42e2d475b
8158174: jshell tool: leaks threads waiting on StopDetectingInputStream
jlahoda
parents:
34752
diff
changeset
|
111 |
state = State.CLOSED; |
80e42e2d475b
8158174: jshell tool: leaks threads waiting on StopDetectingInputStream
jlahoda
parents:
34752
diff
changeset
|
112 |
notifyAll(); |
80e42e2d475b
8158174: jshell tool: leaks threads waiting on StopDetectingInputStream
jlahoda
parents:
34752
diff
changeset
|
113 |
} |
80e42e2d475b
8158174: jshell tool: leaks threads waiting on StopDetectingInputStream
jlahoda
parents:
34752
diff
changeset
|
114 |
|
33362 | 115 |
public synchronized void write(int b) { |
53003
ff1c86e85d02
8215243: JShell tests failing intermitently with \"Problem cleaning up the following threads:\"
jlahoda
parents:
47216
diff
changeset
|
116 |
if (state == State.READ) { |
33362 | 117 |
state = State.WAIT; |
118 |
} |
|
119 |
int newEnd = (end + 1) % buffer.length; |
|
120 |
if (newEnd == start) { |
|
121 |
//overflow: |
|
122 |
int[] newBuffer = new int[buffer.length * 2]; |
|
123 |
int rightPart = (end > start ? end : buffer.length) - start; |
|
124 |
int leftPart = end > start ? 0 : start - 1; |
|
125 |
System.arraycopy(buffer, start, newBuffer, 0, rightPart); |
|
126 |
System.arraycopy(buffer, 0, newBuffer, rightPart, leftPart); |
|
127 |
buffer = newBuffer; |
|
128 |
start = 0; |
|
129 |
end = rightPart + leftPart; |
|
130 |
newEnd = end + 1; |
|
131 |
} |
|
132 |
buffer[end] = b; |
|
133 |
end = newEnd; |
|
134 |
notifyAll(); |
|
135 |
} |
|
136 |
||
137 |
public synchronized void setState(State state) { |
|
41518
8fd0057d88f3
8166890: JShell: locks forever when input is piped
jlahoda
parents:
38909
diff
changeset
|
138 |
if (this.state != State.CLOSED) { |
8fd0057d88f3
8166890: JShell: locks forever when input is piped
jlahoda
parents:
38909
diff
changeset
|
139 |
this.state = state; |
8fd0057d88f3
8166890: JShell: locks forever when input is piped
jlahoda
parents:
38909
diff
changeset
|
140 |
notifyAll(); |
8fd0057d88f3
8166890: JShell: locks forever when input is piped
jlahoda
parents:
38909
diff
changeset
|
141 |
} |
33362 | 142 |
} |
143 |
||
43135
0c7c13fa7bee
8171385: jshell tool: unresponsive to ctrl-C in input wait on Windows
jlahoda
parents:
42827
diff
changeset
|
144 |
private synchronized State getState() { |
0c7c13fa7bee
8171385: jshell tool: unresponsive to ctrl-C in input wait on Windows
jlahoda
parents:
42827
diff
changeset
|
145 |
return state; |
0c7c13fa7bee
8171385: jshell tool: unresponsive to ctrl-C in input wait on Windows
jlahoda
parents:
42827
diff
changeset
|
146 |
} |
0c7c13fa7bee
8171385: jshell tool: unresponsive to ctrl-C in input wait on Windows
jlahoda
parents:
42827
diff
changeset
|
147 |
|
38909
80e42e2d475b
8158174: jshell tool: leaks threads waiting on StopDetectingInputStream
jlahoda
parents:
34752
diff
changeset
|
148 |
private synchronized State waitInputNeeded() { |
33362 | 149 |
while (state == State.WAIT) { |
150 |
try { |
|
151 |
wait(); |
|
152 |
} catch (InterruptedException ex) { |
|
153 |
//ignore |
|
154 |
} |
|
155 |
} |
|
38909
80e42e2d475b
8158174: jshell tool: leaks threads waiting on StopDetectingInputStream
jlahoda
parents:
34752
diff
changeset
|
156 |
|
80e42e2d475b
8158174: jshell tool: leaks threads waiting on StopDetectingInputStream
jlahoda
parents:
34752
diff
changeset
|
157 |
return state; |
33362 | 158 |
} |
159 |
||
160 |
public enum State { |
|
161 |
/* No reading from the input should happen. This is the default state. The StopDetectingInput |
|
162 |
* must be in this state when an external editor is being run, so that the external editor |
|
163 |
* can read from the input. |
|
164 |
*/ |
|
165 |
WAIT, |
|
166 |
/* A single input character should be read. Reading from the StopDetectingInput will move it |
|
167 |
* into this state, and the state will be automatically changed back to WAIT when a single |
|
168 |
* input character is obtained. This state is typically used while the user is editing the |
|
169 |
* input line. |
|
170 |
*/ |
|
171 |
READ, |
|
172 |
/* Continuously read from the input. Forward Ctrl-C ((int) 3) to the "stop" Runnable, buffer |
|
173 |
* all other input. This state is typically used while the user's code is running, to provide |
|
174 |
* the ability to detect Ctrl-C in order to stop the execution. |
|
175 |
*/ |
|
176 |
BUFFER, |
|
177 |
/* The input is closed. |
|
178 |
*/ |
|
179 |
CLOSED |
|
180 |
} |
|
181 |
||
182 |
} |