Skip to content

Commit

Permalink
Replace home-grown buffering with BufferedReader
Browse files Browse the repository at this point in the history
  • Loading branch information
atrosinenko committed Jan 6, 2020
1 parent 552f876 commit 86b9850
Showing 1 changed file with 7 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.beust.jcommander.Parameter;
import org.apache.commons.lang3.SystemUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
Expand Down Expand Up @@ -116,8 +117,8 @@ protected abstract static class AbstractFactoryWithForkServer extends AbstractFa

private Process forkServer;
private OutputStream forkServerStdin;
private InputStreamReader forkServerStdout;
private InputStreamReader forkServerStderr;
private BufferedReader forkServerStdout;
private BufferedReader forkServerStderr;

/**
* Misc files to be cleaned up just before JVM termination.
Expand Down Expand Up @@ -156,33 +157,6 @@ protected Charset getCharset() {
return Charset.defaultCharset();
}

/**
* Reads a line of text until '\n', <b>byte-by-byte</b>.
*
* This is used to avoid deadlocks: more output may be read from the forkserver,
* but only after writing request to <b>stdin</b>. But first we should know that
* the forkserver is ready -- by reading its output streams.
* @param reader A stream to read from
* @param lineBuffer A scratch buffer
* @return A single line of output until '\n' (excluding) or EOF (if non-empty)
* or <code>null</code>, if got EOF as the very first character
* @throws IOException
*/
private String readString(InputStreamReader reader, char[] lineBuffer) throws IOException {
int lineLength;
for (lineLength = 0; lineLength < lineBuffer.length; ++lineLength) {
int ch = reader.read();
if (ch == -1 && lineLength == 0) {
return null;
}
if (ch == -1 || ch == '\n') {
break;
}
lineBuffer[lineLength] = (char) ch;
}
return new String(lineBuffer, 0, lineLength);
}

/**
* Reads all lines of output until the {@link #FORKSERVER_TO_SCM_MARKER}.
*
Expand All @@ -193,10 +167,9 @@ private String readString(InputStreamReader reader, char[] lineBuffer) throws IO
* or <code>null</code> on unexpected EOF
* @throws IOException
*/
private String readUntilMarker(InputStreamReader reader, List<String> stdoutContents) throws IOException {
char[] lineBuffer = new char[MAX_LINE_LENGTH];
private String readUntilMarker(BufferedReader reader, List<String> stdoutContents) throws IOException {
while (true) {
String line = readString(reader, lineBuffer);
String line = reader.readLine();
if (line == null) {
return null;
}
Expand Down Expand Up @@ -274,8 +247,8 @@ public void initialize(InvariantOperations ops) throws IOException {
preloadedObject = compilePreloadedObject();
forkServer = createProcessBuilder().start();
forkServerStdin = forkServer.getOutputStream();
forkServerStdout = new InputStreamReader(forkServer.getInputStream(), getCharset());
forkServerStderr = new InputStreamReader(forkServer.getErrorStream(), getCharset());
forkServerStdout = new BufferedReader(new InputStreamReader(forkServer.getInputStream(), getCharset()));
forkServerStderr = new BufferedReader(new InputStreamReader(forkServer.getErrorStream(), getCharset()));

// Read startup messages
List<String> stdoutContents = new ArrayList<>();
Expand Down

0 comments on commit 86b9850

Please sign in to comment.