-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the non-blocking input stream implementation to the Pty, #140
- Loading branch information
Showing
10 changed files
with
111 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
terminal/src/main/java/org/jline/terminal/impl/AbstractPty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package org.jline.terminal.impl; | ||
|
||
import org.jline.terminal.Attributes; | ||
import org.jline.terminal.spi.Pty; | ||
import org.jline.utils.NonBlockingInputStream; | ||
|
||
import java.io.IOError; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InterruptedIOException; | ||
|
||
public abstract class AbstractPty implements Pty { | ||
|
||
private Attributes current; | ||
|
||
@Override | ||
public void setAttr(Attributes attr) throws IOException { | ||
current = new Attributes(attr); | ||
doSetAttr(attr); | ||
} | ||
|
||
@Override | ||
public InputStream getSlaveInput() throws IOException { | ||
InputStream si = doGetSlaveInput(); | ||
if (Boolean.parseBoolean(System.getProperty("org.jline.terminal.pty.nonBlockingReads", "true"))) { | ||
return new PtyInputStream(si); | ||
} else { | ||
return si; | ||
} | ||
} | ||
|
||
protected abstract void doSetAttr(Attributes attr) throws IOException; | ||
|
||
protected abstract InputStream doGetSlaveInput() throws IOException; | ||
|
||
protected void checkInterrupted() throws InterruptedIOException { | ||
if (Thread.interrupted()) { | ||
throw new InterruptedIOException(); | ||
} | ||
} | ||
|
||
class PtyInputStream extends NonBlockingInputStream { | ||
final InputStream in; | ||
int c = 0; | ||
|
||
PtyInputStream(InputStream in) { | ||
this.in = in; | ||
} | ||
|
||
@Override | ||
public int read(long timeout, boolean isPeek) throws IOException { | ||
checkInterrupted(); | ||
if (c != 0) { | ||
int r = c; | ||
if (!isPeek) { | ||
c = 0; | ||
} | ||
return r; | ||
} else { | ||
setNonBlocking(); | ||
long start = System.currentTimeMillis(); | ||
while (true) { | ||
int r = in.read(); | ||
if (r >= 0) { | ||
if (isPeek) { | ||
c = r; | ||
} | ||
return r; | ||
} | ||
checkInterrupted(); | ||
long cur = System.currentTimeMillis(); | ||
if (timeout > 0 && cur - start > timeout) { | ||
return NonBlockingInputStream.READ_EXPIRED; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void setNonBlocking() { | ||
if (current == null | ||
|| current.getControlChar(Attributes.ControlChar.VMIN) != 0 | ||
|| current.getControlChar(Attributes.ControlChar.VTIME) != 1) { | ||
try { | ||
Attributes attr = getAttr(); | ||
attr.setControlChar(Attributes.ControlChar.VMIN, 0); | ||
attr.setControlChar(Attributes.ControlChar.VTIME, 1); | ||
setAttr(attr); | ||
} catch (IOException e) { | ||
throw new IOError(e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters