|
16 | 16 |
|
17 | 17 | package org.spockframework.util; |
18 | 18 |
|
| 19 | +import org.codehaus.groovy.runtime.ResourceGroovyMethods; |
19 | 20 | import org.spockframework.runtime.SpockException; |
20 | 21 | import org.spockframework.runtime.extension.builtin.ThreadDumpUtility; |
21 | 22 | import org.spockframework.runtime.extension.builtin.ThreadDumpUtilityType; |
22 | 23 |
|
23 | 24 | import java.io.*; |
24 | 25 | import java.lang.management.ManagementFactory; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import java.nio.file.Files; |
25 | 28 | import java.nio.file.Path; |
26 | 29 | import java.nio.file.Paths; |
27 | 30 | import java.util.List; |
@@ -66,21 +69,23 @@ public void appendThreadDumpOfCurrentJvm(StringBuilder builder) throws IOExcepti |
66 | 69 | .append(TextUtil.repeatChar('-', utilityName.length())) |
67 | 70 | .append("\n"); |
68 | 71 |
|
| 72 | + |
| 73 | + File threadDumpFile = Files.createTempFile("Spock-threaddump", ".txt").toFile(); |
| 74 | + |
69 | 75 | Process process = new ProcessBuilder(command) |
70 | 76 | .redirectErrorStream(true) |
| 77 | + .redirectOutput(ProcessBuilder.Redirect.to(threadDumpFile)) |
71 | 78 | .start(); |
72 | 79 |
|
73 | | - captureProcessOutput(process, builder); |
74 | 80 | process.waitFor(); |
| 81 | + |
| 82 | + readAndDeleteFile(builder, threadDumpFile); |
75 | 83 | } |
76 | 84 |
|
77 | | - private void captureProcessOutput(Process process, StringBuilder builder) throws IOException { |
78 | | - try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { |
79 | | - String line; |
80 | | - while ((line = reader.readLine()) != null) { |
81 | | - builder.append(line).append("\n"); |
82 | | - } |
83 | | - } |
| 85 | + private void readAndDeleteFile(StringBuilder builder, File threadDumpFile) throws IOException { |
| 86 | + builder.append(ResourceGroovyMethods.getText(threadDumpFile, StandardCharsets.UTF_8.name())); |
| 87 | + //noinspection ResultOfMethodCallIgnored |
| 88 | + threadDumpFile.delete(); // We do not care if the temp file could not be deleted. |
84 | 89 | } |
85 | 90 |
|
86 | 91 | private static Path getJavaHome() { |
|
0 commit comments