author | mchung |
Sat, 24 Feb 2018 09:41:42 -0800 (2018-02-24) | |
changeset 48984 | b5d1fb0701d4 |
parent 48347 | 4f9683bf0923 |
child 50338 | 1d5694c1aa03 |
permissions | -rw-r--r-- |
44459 | 1 |
/* |
2 |
* Copyright (c) 2017, 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. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
import java.io.IOException; |
|
25 |
import java.io.InputStream; |
|
26 |
import java.io.OutputStream; |
|
27 |
import java.io.OutputStreamWriter; |
|
28 |
import java.io.PrintStream; |
|
29 |
import java.io.Writer; |
|
44811
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
30 |
import java.text.MessageFormat; |
44459 | 31 |
import java.util.HashMap; |
32 |
import java.util.Locale; |
|
44811
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
33 |
import java.util.ResourceBundle; |
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44570
diff
changeset
|
34 |
import java.util.logging.Level; |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44570
diff
changeset
|
35 |
import java.util.logging.Logger; |
44459 | 36 |
import java.util.regex.Matcher; |
37 |
import java.util.regex.Pattern; |
|
38 |
||
44811
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
39 |
import jdk.jshell.JShell; |
44459 | 40 |
import jdk.jshell.tool.JavaShellToolBuilder; |
41 |
||
44570
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
42 |
public class UITesting { |
44459 | 43 |
|
48275
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
47216
diff
changeset
|
44 |
protected static final String TAB = "\011"; |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
47216
diff
changeset
|
45 |
protected static final String INTERRUPT = "\u0003"; |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
47216
diff
changeset
|
46 |
protected static final String BELL = "\u0007"; |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
47216
diff
changeset
|
47 |
protected static final String PROMPT = "\u0005"; |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
47216
diff
changeset
|
48 |
protected static final String REDRAW_PROMPT = "\n\r" + PROMPT; |
45757
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
49 |
private final boolean laxLineEndings; |
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
50 |
|
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
51 |
public UITesting() { |
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
52 |
this(false); |
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
53 |
} |
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
54 |
|
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
55 |
public UITesting(boolean laxLineEndings) { |
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
56 |
this.laxLineEndings = laxLineEndings; |
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
57 |
} |
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
58 |
|
44570
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
59 |
protected void doRunTest(Test test) throws Exception { |
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44570
diff
changeset
|
60 |
// turn on logging of launch failures |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44570
diff
changeset
|
61 |
Logger.getLogger("jdk.jshell.execution").setLevel(Level.ALL); |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44570
diff
changeset
|
62 |
|
44459 | 63 |
PipeInputStream input = new PipeInputStream(); |
64 |
StringBuilder out = new StringBuilder(); |
|
65 |
PrintStream outS = new PrintStream(new OutputStream() { |
|
66 |
@Override public void write(int b) throws IOException { |
|
67 |
synchronized (out) { |
|
68 |
System.out.print((char) b); |
|
69 |
out.append((char) b); |
|
70 |
out.notifyAll(); |
|
71 |
} |
|
72 |
} |
|
44821
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
73 |
@Override public void write(byte[] b, int off, int len) throws IOException { |
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
74 |
synchronized (out) { |
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
75 |
String data = new String(b, off, len); |
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
76 |
System.out.print(data); |
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
77 |
out.append(data); |
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
78 |
out.notifyAll(); |
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
79 |
} |
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
80 |
} |
44459 | 81 |
}); |
82 |
Thread runner = new Thread(() -> { |
|
83 |
try { |
|
84 |
JavaShellToolBuilder.builder() |
|
85 |
.in(input, input) |
|
86 |
.out(outS) |
|
87 |
.err(outS) |
|
88 |
.promptCapture(true) |
|
89 |
.persistence(new HashMap<>()) |
|
90 |
.locale(Locale.US) |
|
91 |
.run("--no-startup"); |
|
92 |
} catch (Exception ex) { |
|
93 |
throw new IllegalStateException(ex); |
|
94 |
} |
|
95 |
}); |
|
96 |
||
97 |
Writer inputSink = new OutputStreamWriter(input.createOutput()) { |
|
98 |
@Override |
|
99 |
public void write(String str) throws IOException { |
|
100 |
super.write(str); |
|
101 |
flush(); |
|
102 |
} |
|
103 |
}; |
|
104 |
||
105 |
runner.start(); |
|
106 |
||
107 |
try { |
|
48275
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
47216
diff
changeset
|
108 |
waitOutput(out, PROMPT); |
44459 | 109 |
test.test(inputSink, out); |
110 |
} finally { |
|
48275
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
47216
diff
changeset
|
111 |
inputSink.write(INTERRUPT + INTERRUPT + "/exit"); |
44459 | 112 |
|
113 |
runner.join(1000); |
|
114 |
if (runner.isAlive()) { |
|
115 |
runner.stop(); |
|
116 |
} |
|
117 |
} |
|
118 |
} |
|
119 |
||
44570
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
120 |
protected interface Test { |
44459 | 121 |
public void test(Writer inputSink, StringBuilder out) throws Exception; |
122 |
} |
|
123 |
||
124 |
private static final long TIMEOUT; |
|
125 |
||
126 |
static { |
|
127 |
long factor; |
|
128 |
||
129 |
try { |
|
130 |
factor = (long) Double.parseDouble(System.getProperty("test.timeout.factor", "1")); |
|
131 |
} catch (NumberFormatException ex) { |
|
132 |
factor = 1; |
|
133 |
} |
|
134 |
TIMEOUT = 60_000 * factor; |
|
135 |
} |
|
136 |
||
44570
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
137 |
protected void waitOutput(StringBuilder out, String expected) { |
48347
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
138 |
waitOutput(out, expected, null); |
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
139 |
} |
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
140 |
|
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
141 |
// Return true if expected is found, false if secondary is found, |
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
142 |
// otherwise, time out with an IllegalStateException |
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
143 |
protected boolean waitOutput(StringBuilder out, String expected, String secondary) { |
45757
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
144 |
expected = expected.replaceAll("\n", laxLineEndings ? "\r?\n" : System.getProperty("line.separator")); |
44459 | 145 |
Pattern expectedPattern = Pattern.compile(expected, Pattern.DOTALL); |
48347
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
146 |
Pattern secondaryPattern = null; |
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
147 |
if (secondary != null) { |
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
148 |
secondary = secondary.replaceAll("\n", laxLineEndings ? "\r?\n" : System.getProperty("line.separator")); |
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
149 |
secondaryPattern = Pattern.compile(secondary, Pattern.DOTALL); |
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
150 |
} |
44459 | 151 |
synchronized (out) { |
152 |
long s = System.currentTimeMillis(); |
|
153 |
||
154 |
while (true) { |
|
155 |
Matcher m = expectedPattern.matcher(out); |
|
156 |
if (m.find()) { |
|
46923
0172e4350f65
8182297: jshell tool: pasting multiple lines of code truncated
jlahoda
parents:
45757
diff
changeset
|
157 |
out.delete(0, m.end()); |
48347
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
158 |
return true; |
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
159 |
} |
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
160 |
if (secondaryPattern != null) { |
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
161 |
m = secondaryPattern.matcher(out); |
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
162 |
if (m.find()) { |
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
163 |
out.delete(0, m.end()); |
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
164 |
return false; |
4f9683bf0923
8188894: jdk/jshell/ToolShiftTabTest.java failed with IllegalStateException
rfield
parents:
48275
diff
changeset
|
165 |
} |
44459 | 166 |
} |
167 |
long e = System.currentTimeMillis(); |
|
168 |
if ((e - s) > TIMEOUT) { |
|
169 |
throw new IllegalStateException("Timeout waiting for: " + quote(expected) + ", actual output so far: " + quote(out.toString())); |
|
170 |
} |
|
171 |
try { |
|
172 |
out.wait(TIMEOUT); |
|
173 |
} catch (InterruptedException ex) { |
|
174 |
ex.printStackTrace(); |
|
175 |
} |
|
176 |
} |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
private String quote(String original) { |
|
181 |
StringBuilder output = new StringBuilder(); |
|
182 |
||
183 |
for (char c : original.toCharArray()) { |
|
184 |
if (c < 32) { |
|
185 |
output.append(String.format("\\u%04X", (int) c)); |
|
186 |
} else { |
|
187 |
output.append(c); |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
return output.toString(); |
|
192 |
} |
|
193 |
||
44570
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
194 |
protected String clearOut(String what) { |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
195 |
return backspace(what.length()) + space(what.length()) + backspace(what.length()); |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
196 |
} |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
197 |
|
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
198 |
protected String backspace(int n) { |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
199 |
return fill(n, '\010'); |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
200 |
} |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
201 |
|
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
202 |
protected String space(int n) { |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
203 |
return fill(n, ' '); |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
204 |
} |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
205 |
|
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
206 |
private String fill(int n, char c) { |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
207 |
StringBuilder result = new StringBuilder(n); |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
208 |
|
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
209 |
while (n-- > 0) |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
210 |
result.append(c); |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
211 |
|
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
212 |
return result.toString(); |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
213 |
} |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
214 |
|
44811
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
215 |
private final ResourceBundle resources; |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
216 |
{ |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
217 |
resources = ResourceBundle.getBundle("jdk.internal.jshell.tool.resources.l10n", Locale.US, JShell.class.getModule()); |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
218 |
} |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
219 |
|
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
220 |
protected String getResource(String key) { |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
221 |
return resources.getString(key); |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
222 |
} |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
223 |
|
48275
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
47216
diff
changeset
|
224 |
protected String resource(String key) { |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
47216
diff
changeset
|
225 |
return Pattern.quote(getResource(key).replaceAll("\t", " ")); |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
47216
diff
changeset
|
226 |
} |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
47216
diff
changeset
|
227 |
|
44811
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
228 |
protected String getMessage(String key, Object... args) { |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
229 |
return MessageFormat.format(resources.getString(key), args); |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
230 |
} |
44459 | 231 |
private static class PipeInputStream extends InputStream { |
232 |
||
233 |
private static final int INITIAL_SIZE = 128; |
|
234 |
private int[] buffer = new int[INITIAL_SIZE]; |
|
235 |
private int start; |
|
236 |
private int end; |
|
237 |
private boolean closed; |
|
238 |
||
239 |
@Override |
|
240 |
public synchronized int read() throws IOException { |
|
241 |
if (start == end && !closed) { |
|
242 |
inputNeeded(); |
|
243 |
} |
|
244 |
while (start == end) { |
|
245 |
if (closed) { |
|
246 |
return -1; |
|
247 |
} |
|
248 |
try { |
|
249 |
wait(); |
|
250 |
} catch (InterruptedException ex) { |
|
251 |
//ignore |
|
252 |
} |
|
253 |
} |
|
254 |
try { |
|
255 |
return buffer[start]; |
|
256 |
} finally { |
|
257 |
start = (start + 1) % buffer.length; |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
@Override |
|
262 |
public synchronized int read(byte[] b, int off, int len) throws IOException { |
|
263 |
if (b == null) { |
|
264 |
throw new NullPointerException(); |
|
265 |
} else if (off < 0 || len < 0 || len > b.length - off) { |
|
266 |
throw new IndexOutOfBoundsException(); |
|
267 |
} else if (len == 0) { |
|
268 |
return 0; |
|
269 |
} |
|
270 |
||
271 |
int c = read(); |
|
272 |
if (c == -1) { |
|
273 |
return -1; |
|
274 |
} |
|
275 |
b[off] = (byte)c; |
|
276 |
||
277 |
int totalRead = 1; |
|
278 |
while (totalRead < len && start != end) { |
|
279 |
int r = read(); |
|
280 |
if (r == (-1)) |
|
281 |
break; |
|
282 |
b[off + totalRead++] = (byte) r; |
|
283 |
} |
|
284 |
return totalRead; |
|
285 |
} |
|
286 |
||
287 |
protected void inputNeeded() throws IOException {} |
|
288 |
||
289 |
private synchronized void write(int b) { |
|
290 |
if (closed) { |
|
291 |
throw new IllegalStateException("Already closed."); |
|
292 |
} |
|
293 |
int newEnd = (end + 1) % buffer.length; |
|
294 |
if (newEnd == start) { |
|
295 |
//overflow: |
|
296 |
int[] newBuffer = new int[buffer.length * 2]; |
|
297 |
int rightPart = (end > start ? end : buffer.length) - start; |
|
298 |
int leftPart = end > start ? 0 : start - 1; |
|
299 |
System.arraycopy(buffer, start, newBuffer, 0, rightPart); |
|
300 |
System.arraycopy(buffer, 0, newBuffer, rightPart, leftPart); |
|
301 |
buffer = newBuffer; |
|
302 |
start = 0; |
|
303 |
end = rightPart + leftPart; |
|
304 |
newEnd = end + 1; |
|
305 |
} |
|
306 |
buffer[end] = b; |
|
307 |
end = newEnd; |
|
308 |
notifyAll(); |
|
309 |
} |
|
310 |
||
311 |
@Override |
|
312 |
public synchronized void close() { |
|
313 |
closed = true; |
|
314 |
notifyAll(); |
|
315 |
} |
|
316 |
||
317 |
public OutputStream createOutput() { |
|
318 |
return new OutputStream() { |
|
319 |
@Override public void write(int b) throws IOException { |
|
320 |
PipeInputStream.this.write(b); |
|
321 |
} |
|
322 |
@Override |
|
323 |
public void write(byte[] b, int off, int len) throws IOException { |
|
324 |
for (int i = 0 ; i < len ; i++) { |
|
325 |
write(Byte.toUnsignedInt(b[off + i])); |
|
326 |
} |
|
327 |
} |
|
328 |
@Override |
|
329 |
public void close() throws IOException { |
|
330 |
PipeInputStream.this.close(); |
|
331 |
} |
|
332 |
}; |
|
333 |
} |
|
334 |
||
335 |
} |
|
336 |
||
337 |
} |