Skip to content

Commit 86b9850

Browse files
committed
Replace home-grown buffering with BufferedReader
1 parent 552f876 commit 86b9850

File tree

1 file changed

+7
-34
lines changed

1 file changed

+7
-34
lines changed

pmd-scm/src/main/java/net/sourceforge/pmd/scm/invariants/AbstractForkServerAwareProcessInvariant.java

+7-34
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.beust.jcommander.Parameter;
88
import org.apache.commons.lang3.SystemUtils;
99

10+
import java.io.BufferedReader;
1011
import java.io.IOException;
1112
import java.io.InputStreamReader;
1213
import java.io.OutputStream;
@@ -116,8 +117,8 @@ protected abstract static class AbstractFactoryWithForkServer extends AbstractFa
116117

117118
private Process forkServer;
118119
private OutputStream forkServerStdin;
119-
private InputStreamReader forkServerStdout;
120-
private InputStreamReader forkServerStderr;
120+
private BufferedReader forkServerStdout;
121+
private BufferedReader forkServerStderr;
121122

122123
/**
123124
* Misc files to be cleaned up just before JVM termination.
@@ -156,33 +157,6 @@ protected Charset getCharset() {
156157
return Charset.defaultCharset();
157158
}
158159

159-
/**
160-
* Reads a line of text until '\n', <b>byte-by-byte</b>.
161-
*
162-
* This is used to avoid deadlocks: more output may be read from the forkserver,
163-
* but only after writing request to <b>stdin</b>. But first we should know that
164-
* the forkserver is ready -- by reading its output streams.
165-
* @param reader A stream to read from
166-
* @param lineBuffer A scratch buffer
167-
* @return A single line of output until '\n' (excluding) or EOF (if non-empty)
168-
* or <code>null</code>, if got EOF as the very first character
169-
* @throws IOException
170-
*/
171-
private String readString(InputStreamReader reader, char[] lineBuffer) throws IOException {
172-
int lineLength;
173-
for (lineLength = 0; lineLength < lineBuffer.length; ++lineLength) {
174-
int ch = reader.read();
175-
if (ch == -1 && lineLength == 0) {
176-
return null;
177-
}
178-
if (ch == -1 || ch == '\n') {
179-
break;
180-
}
181-
lineBuffer[lineLength] = (char) ch;
182-
}
183-
return new String(lineBuffer, 0, lineLength);
184-
}
185-
186160
/**
187161
* Reads all lines of output until the {@link #FORKSERVER_TO_SCM_MARKER}.
188162
*
@@ -193,10 +167,9 @@ private String readString(InputStreamReader reader, char[] lineBuffer) throws IO
193167
* or <code>null</code> on unexpected EOF
194168
* @throws IOException
195169
*/
196-
private String readUntilMarker(InputStreamReader reader, List<String> stdoutContents) throws IOException {
197-
char[] lineBuffer = new char[MAX_LINE_LENGTH];
170+
private String readUntilMarker(BufferedReader reader, List<String> stdoutContents) throws IOException {
198171
while (true) {
199-
String line = readString(reader, lineBuffer);
172+
String line = reader.readLine();
200173
if (line == null) {
201174
return null;
202175
}
@@ -274,8 +247,8 @@ public void initialize(InvariantOperations ops) throws IOException {
274247
preloadedObject = compilePreloadedObject();
275248
forkServer = createProcessBuilder().start();
276249
forkServerStdin = forkServer.getOutputStream();
277-
forkServerStdout = new InputStreamReader(forkServer.getInputStream(), getCharset());
278-
forkServerStderr = new InputStreamReader(forkServer.getErrorStream(), getCharset());
250+
forkServerStdout = new BufferedReader(new InputStreamReader(forkServer.getInputStream(), getCharset()));
251+
forkServerStderr = new BufferedReader(new InputStreamReader(forkServer.getErrorStream(), getCharset()));
279252

280253
// Read startup messages
281254
List<String> stdoutContents = new ArrayList<>();

0 commit comments

Comments
 (0)