OutputReader's readFully now compatible with the blocking sleeping policy

This commit is contained in:
Vyacheslav Karpukhin
2015-07-14 20:44:18 +02:00
parent 1f38a85342
commit bbcf2ccca9
2 changed files with 28 additions and 6 deletions
@@ -127,7 +127,7 @@ public abstract class BaseOutputReader extends BaseDataReader {
}
}
private void sendLine(StringBuilder line) {
protected void sendLine(@NotNull StringBuilder line) {
onTextAvailable(line.toString());
line.setLength(0);
}
@@ -28,6 +28,7 @@ import java.nio.charset.Charset;
public abstract class OutputReader extends BaseOutputReader {
private static final Logger LOG = Logger.getInstance("#com.intellij.util.io.OutputReader");
private static final int READ_FULLY_TIMEOUT = 10;
private final Semaphore myReadFullySemaphore = new Semaphore();
@@ -80,13 +81,34 @@ public abstract class OutputReader extends BaseOutputReader {
}
}
@Override
protected void sendLine(@NotNull StringBuilder line) {
super.sendLine(line);
if (mySleepingPolicy == SleepingPolicy.BLOCKING) {
myReadFullySemaphore.up();
}
}
public void readFully() throws InterruptedException {
myReadFullySemaphore.down();
while (!myReadFullySemaphore.waitForUnsafe(10)) {
if (isStopped) {
waitFor();
return;
if (mySleepingPolicy != SleepingPolicy.BLOCKING) {
myReadFullySemaphore.down();
while (!myReadFullySemaphore.waitForUnsafe(READ_FULLY_TIMEOUT)) {
if (isStopped) {
waitFor();
return;
}
}
}
else {
do {
myReadFullySemaphore.down();
}
while (myReadFullySemaphore.waitForUnsafe(READ_FULLY_TIMEOUT));
myReadFullySemaphore.up();
if (isStopped) waitFor();
}
}
}