Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions modules/tlc2/overrides/IOUtils.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*******************************************************************************
* Copyright (c) 2020 Microsoft Research. All rights reserved.
* Copyright (c) 2020 Microsoft Research. All rights reserved.
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
Expand All @@ -25,8 +25,10 @@
******************************************************************************/
package tlc2.overrides;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import tlc2.output.EC;
Expand Down Expand Up @@ -86,8 +88,8 @@ public static Value ioExec(final Value parameter) throws IOException, Interrupte
// 3. Run command-line and receive its output.
final Process process = new ProcessBuilder(command)/*.inheritIO()*/.start();

final StringValue stdout = new StringValue(new String(process.getInputStream().readAllBytes()));
final StringValue stderr = new StringValue(new String(process.getErrorStream().readAllBytes()));
final StringValue stdout = new StringValue(stringFromInputStream(process.getInputStream()));
final StringValue stderr = new StringValue(stringFromInputStream(process.getErrorStream()));
final IntValue exitCode = IntValue.gen(process.waitFor());

return new RecordValue(EXEC_NAMES, new Value[] {exitCode, stdout, stderr}, false);
Expand Down Expand Up @@ -127,13 +129,23 @@ public static Value ioExecTemplate(final Value commandTemplate, final Value para
// 3. Run command-line and receive its output.
final Process process = new ProcessBuilder(command)/*.inheritIO()*/.start();

final StringValue stdout = new StringValue(new String(process.getInputStream().readAllBytes()));
final StringValue stderr = new StringValue(new String(process.getErrorStream().readAllBytes()));
final StringValue stdout = new StringValue(stringFromInputStream(process.getInputStream()));
final StringValue stderr = new StringValue(stringFromInputStream(process.getErrorStream()));
final IntValue exitCode = IntValue.gen(process.waitFor());

return new RecordValue(EXEC_NAMES, new Value[] {exitCode, stdout, stderr}, false);
}

private static String stringFromInputStream(InputStream inputStream) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString();
}

private static String convert(IValue v) {
if (! (v instanceof StringValue)) {
// XXX Proper exception
Expand Down