Skip to content

Commit e602b56

Browse files
Revert "Don't close process.getInputStream() on failure (#6541)" (#6542)
This reverts commit 5087896.
1 parent 5087896 commit e602b56

2 files changed

Lines changed: 9 additions & 25 deletions

File tree

rewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -221,22 +221,6 @@ public static String readFully(InputStream inputStream, Charset charset) {
221221
}
222222
}
223223

224-
public static String readFullyWithoutClosing(InputStream is, Charset charset) {
225-
try {
226-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
227-
byte[] buffer = new byte[4096];
228-
int n;
229-
while ((n = is.read(buffer)) != -1) {
230-
bos.write(buffer, 0, n);
231-
}
232-
233-
byte[] bytes = bos.toByteArray();
234-
return new String(bytes, charset);
235-
} catch (IOException e) {
236-
throw new UnsupportedOperationException(e);
237-
}
238-
}
239-
240224
public static String capitalize(String value) {
241225
if (value.isEmpty()) {
242226
return value;

rewrite-core/src/main/java/org/openrewrite/rpc/RewriteRpcProcess.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,13 @@
3434
import java.io.IOException;
3535
import java.io.InputStream;
3636
import java.io.UncheckedIOException;
37-
import java.nio.charset.StandardCharsets;
3837
import java.nio.file.Path;
3938
import java.nio.file.Paths;
4039
import java.util.LinkedHashMap;
4140
import java.util.Map;
4241
import java.util.concurrent.TimeUnit;
4342

4443
import static org.openrewrite.internal.StringUtils.readFully;
45-
import static org.openrewrite.internal.StringUtils.readFullyWithoutClosing;
4644

4745
/**
4846
* A client for spawning and communicating with a subprocess that implements Rewrite RPC.
@@ -97,16 +95,18 @@ public void run() {
9795
public @Nullable RuntimeException getLivenessCheck() {
9896
if (process != null && !process.isAlive()) {
9997
int exitCode = process.exitValue();
100-
String message = "JavaScript RPC process shut down early with exit code " + exitCode;
101-
10298
String errorOutput = "", stdOutput = "";
103-
try {
104-
errorOutput = readFullyWithoutClosing(process.getErrorStream(), StandardCharsets.UTF_8);
105-
stdOutput = readFullyWithoutClosing(process.getInputStream(), StandardCharsets.UTF_8);
106-
} catch (UnsupportedOperationException e) {
107-
message += "\nError retrieving output/error stream: " + e.getMessage();
99+
100+
// Read any remaining output from the process
101+
try (InputStream errorStream = process.getErrorStream();
102+
InputStream inputStream = process.getInputStream()) {
103+
errorOutput = readFully(errorStream);
104+
stdOutput = readFully(inputStream);
105+
} catch (IOException | UnsupportedOperationException e) {
106+
// Ignore errors reading final output
108107
}
109108

109+
String message = "JavaScript RPC process shut down early with exit code " + exitCode;
110110
if (!stdOutput.isEmpty()) {
111111
message += "\nStandard output:\n " + stdOutput.replace("\n", "\n ");
112112
}

0 commit comments

Comments
 (0)