提交 c2b93793 编写于 作者: J jlaskey

8015911: $EXEC does not handle large outputs

Reviewed-by: sundar, attila
Contributed-by: james.laskey@oracle.com
上级 7e2a6506
...@@ -32,9 +32,8 @@ import static jdk.nashorn.internal.lookup.Lookup.MH; ...@@ -32,9 +32,8 @@ import static jdk.nashorn.internal.lookup.Lookup.MH;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStreamWriter;
import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import java.util.Map; import java.util.Map;
...@@ -165,36 +164,61 @@ public final class ScriptingFunctions { ...@@ -165,36 +164,61 @@ public final class ScriptingFunctions {
// Start the process. // Start the process.
final Process process = processBuilder.start(); final Process process = processBuilder.start();
final IOException exception[] = new IOException[2];
// Collect output.
final StringBuilder outBuffer = new StringBuilder();
Thread outThread = new Thread(new Runnable() {
@Override
public void run() {
char buffer[] = new char[1024];
try (final InputStreamReader inputStream = new InputStreamReader(process.getInputStream())) {
for (int length; (length = inputStream.read(buffer, 0, buffer.length)) != -1; ) {
outBuffer.append(buffer, 0, length);
}
} catch (IOException ex) {
exception[0] = ex;
}
}
}, "$EXEC output");
// Collect errors.
final StringBuilder errBuffer = new StringBuilder();
Thread errThread = new Thread(new Runnable() {
@Override
public void run() {
char buffer[] = new char[1024];
try (final InputStreamReader inputStream = new InputStreamReader(process.getErrorStream())) {
for (int length; (length = inputStream.read(buffer, 0, buffer.length)) != -1; ) {
outBuffer.append(buffer, 0, length);
}
} catch (IOException ex) {
exception[1] = ex;
}
}
}, "$EXEC error");
// Start gathering output.
outThread.start();
errThread.start();
// If input is present, pass on to process. // If input is present, pass on to process.
try (OutputStream outputStream = process.getOutputStream()) { try (OutputStreamWriter outputStream = new OutputStreamWriter(process.getOutputStream())) {
if (input != UNDEFINED) { if (input != UNDEFINED) {
outputStream.write(JSType.toString(input).getBytes()); String in = JSType.toString(input);
outputStream.write(in, 0, in.length());
} }
} catch (IOException ex) {
// Process was not expecting input. May be normal state of affairs.
} }
// Wait for the process to complete. // Wait for the process to complete.
final int exit = process.waitFor(); final int exit = process.waitFor();
outThread.join();
errThread.join();
// Collect output. final String out = outBuffer.toString();
String out; final String err = errBuffer.toString();
try (InputStream inputStream = process.getInputStream()) {
final StringBuilder outBuffer = new StringBuilder();
for (int ch; (ch = inputStream.read()) != -1; ) {
outBuffer.append((char)ch);
}
out = outBuffer.toString();
}
// Collect errors.
String err;
try (InputStream errorStream = process.getErrorStream()) {
final StringBuilder errBuffer = new StringBuilder();
for (int ch; (ch = errorStream.read()) != -1; ) {
errBuffer.append((char)ch);
}
err = errBuffer.toString();
}
// Set globals for secondary results. // Set globals for secondary results.
final boolean isStrict = global.isStrictContext(); final boolean isStrict = global.isStrictContext();
...@@ -202,6 +226,13 @@ public final class ScriptingFunctions { ...@@ -202,6 +226,13 @@ public final class ScriptingFunctions {
global.set(ERR_NAME, err, isStrict); global.set(ERR_NAME, err, isStrict);
global.set(EXIT_NAME, exit, isStrict); global.set(EXIT_NAME, exit, isStrict);
// Propagate exception if present.
for (int i = 0; i < exception.length; i++) {
if (exception[i] != null) {
throw exception[i];
}
}
// Return the result from stdout. // Return the result from stdout.
return out; return out;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册