Skip to content

Commit

Permalink
Improvement the ExternalTerminal to read multiple bytes in one go
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jun 20, 2018
1 parent 27de765 commit 12b992f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,29 @@ public int read() throws IOException {
}
return r;
}
public int read(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
int c = read();
if (c == -1) {
return -1;
}
b[off] = (byte)c;
int i = 1;
for (; i < len ; i++) {
c = read();
if (c == -1) {
break;
}
b[off + i] = (byte)c;
}
return i;
}
};
Terminal term = TerminalBuilder.builder().system(false).streams(in, baos).build();
Thread.sleep(100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ public boolean paused() {

public void pump() {
try {
byte[] buf = new byte[1024];
while (true) {
int c = masterInput.read();
int c = masterInput.read(buf);
if (c >= 0) {
processInputByte((char) c);
processInputBytes(buf, 0, c);
}
if (c < 0 || closed.get()) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,36 +177,60 @@ public void raise(Signal signal) {
* @throws IOException if anything wrong happens
*/
public void processInputByte(int c) throws IOException {
boolean flushOut = doProcessInputByte(c);
slaveInputPipe.flush();
if (flushOut) {
masterOutput.flush();
}
}

public void processInputBytes(byte[] input) throws IOException {
processInputBytes(input, 0, input.length);
}

public void processInputBytes(byte[] input, int offset, int length) throws IOException {
boolean flushOut = false;
for (int i = 0; i < length; i++) {
flushOut |= doProcessInputByte(input[offset + i]);
}
slaveInputPipe.flush();
if (flushOut) {
masterOutput.flush();
}
}

protected boolean doProcessInputByte(int c) throws IOException {
if (attributes.getLocalFlag(LocalFlag.ISIG)) {
if (c == attributes.getControlChar(ControlChar.VINTR)) {
raise(Signal.INT);
return;
return false;
} else if (c == attributes.getControlChar(ControlChar.VQUIT)) {
raise(Signal.QUIT);
return;
return false;
} else if (c == attributes.getControlChar(ControlChar.VSUSP)) {
raise(Signal.TSTP);
return;
return false;
} else if (c == attributes.getControlChar(ControlChar.VSTATUS)) {
raise(Signal.INFO);
}
}
if (c == '\r') {
if (attributes.getInputFlag(InputFlag.IGNCR)) {
return;
return false;
}
if (attributes.getInputFlag(InputFlag.ICRNL)) {
c = '\n';
}
} else if (c == '\n' && attributes.getInputFlag(InputFlag.INLCR)) {
c = '\r';
}
boolean flushOut = false;
if (attributes.getLocalFlag(LocalFlag.ECHO)) {
processOutputByte(c);
masterOutput.flush();
flushOut = true;
}
slaveInputPipe.write(c);
slaveInputPipe.flush();
return flushOut;
}

/**
Expand Down

0 comments on commit 12b992f

Please sign in to comment.