Skip to content

Commit

Permalink
IOExceptions thrown from streams is not propagated to LineReader#read…
Browse files Browse the repository at this point in the history
…Line(), fixed #267
  • Loading branch information
gnodet committed May 13, 2018
1 parent 69471c7 commit f8894f3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
*/
package org.jline.terminal.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.charset.StandardCharsets;
Expand All @@ -25,6 +28,7 @@
import org.jline.terminal.Attributes.OutputFlag;
import org.jline.terminal.Cursor;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.junit.Ignore;
import org.junit.Test;

Expand Down Expand Up @@ -133,4 +137,27 @@ public void testCursorPosition() throws IOException {
assertEquals('f', console.reader().read());
}

@Test
public void testExceptionOnInputStream() throws IOException, InterruptedException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayInputStream bais = new ByteArrayInputStream("abcdefghijklmnopqrstuvwxyz".getBytes());
InputStream in = new FilterInputStream(bais) {
@Override
public int read() throws IOException {
int r = super.read();
if (r == 'm') {
throw new IOException("Inject IOException");
}
return r;
}
};
Terminal term = TerminalBuilder.builder().system(false).streams(in, baos).build();
Thread.sleep(100);
try {
term.input().read();
fail("Should have thrown an exception");
} catch (IOException error) {
// expected
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void pump() {
}
}
} catch (IOException e) {
// Ignore
processIOException(e);
} finally {
synchronized (lock) {
pumpThread = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.jline.terminal.Size;
import org.jline.terminal.Terminal;
import org.jline.utils.NonBlocking;
import org.jline.utils.NonBlockingInputStream;
import org.jline.utils.NonBlockingPumpInputStream;
import org.jline.utils.NonBlockingReader;

Expand Down Expand Up @@ -76,7 +75,7 @@ public class LineDisciplineTerminal extends AbstractTerminal {
/*
* Slave streams
*/
protected final NonBlockingInputStream slaveInput;
protected final NonBlockingPumpInputStream slaveInput;
protected final NonBlockingReader slaveReader;
protected final PrintWriter slaveWriter;
protected final OutputStream slaveOutput;
Expand Down Expand Up @@ -230,6 +229,10 @@ protected void processOutputByte(int c) throws IOException {
masterOutput.write(c);
}

protected void processIOException(IOException ioException) {
this.slaveInput.setIoException(ioException);
}

public void close() throws IOException {
try {
slaveReader.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class NonBlockingPumpInputStream extends NonBlockingInputStream {

private boolean closed;

private IOException ioException;

public NonBlockingPumpInputStream() {
this(DEFAULT_BUFFER_SIZE);
}
Expand Down Expand Up @@ -92,6 +94,9 @@ public synchronized int available() {

@Override
public synchronized int read(long timeout, boolean isPeek) throws IOException {
if (ioException != null) {
throw ioException;
}
// Blocks until more input is available or the reader is closed.
int res = wait(readBuffer, timeout);
if (res >= 0) {
Expand All @@ -101,6 +106,10 @@ public synchronized int read(long timeout, boolean isPeek) throws IOException {
return res;
}

public synchronized void setIoException(IOException exception) {
this.ioException = exception;
}

synchronized void write(byte[] cbuf, int off, int len) throws IOException {
while (len > 0) {
// Blocks until there is new space available for buffering or the
Expand Down

0 comments on commit f8894f3

Please sign in to comment.