Skip to content

Commit

Permalink
Make sure all the stream is read before sending EOF, #267
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed May 15, 2018
1 parent a67d60f commit 85b08f4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,29 @@ public int read() throws IOException {
// expected
}
}

@Test
public void testReadUntilEof() throws IOException, InterruptedException {
String str = "test 1\n" +
"test 2\n" +
"test 3\n" +
"exit\n";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream in = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
Terminal term = TerminalBuilder.builder().system(false).streams(in, baos).build();
Thread.sleep(100);
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
byte[] buffer = new byte[1014];
int l;
for (;;) {
l = term.input().read(buffer);
if (l >= 0) {
baos2.write(buffer, 0, l);
} else {
break;
}
};
String str2 = new String(baos2.toByteArray(), StandardCharsets.UTF_8);
assertEquals(str, str2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ public void pump() {
}
}
try {
close();
} catch (Throwable t) {
// Ignore
slaveInput.close();
} catch (IOException e) {
// ignore
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public OutputStream getOutputStream() {
return this.output;
}

private int wait(ByteBuffer buffer, long timeout) throws InterruptedIOException {
private int wait(ByteBuffer buffer, long timeout) throws IOException {
boolean isInfinite = (timeout <= 0L);
long end = 0;
if (!isInfinite) {
Expand All @@ -55,17 +55,23 @@ private int wait(ByteBuffer buffer, long timeout) throws InterruptedIOException
notifyAll();
try {
wait(timeout);
if (ioException != null) {
throw ioException;
}
} catch (InterruptedException e) {
if (ioException != null) {
throw ioException;
}
throw new InterruptedIOException();
}
if (!isInfinite) {
timeout = end - System.currentTimeMillis();
}
}
return closed
? EOF
: buffer.hasRemaining()
? 0
return buffer.hasRemaining()
? 0
: closed
? EOF
: READ_EXPIRED;
}

Expand Down Expand Up @@ -108,6 +114,7 @@ public synchronized int read(long timeout, boolean isPeek) throws IOException {

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

synchronized void write(byte[] cbuf, int off, int len) throws IOException {
Expand Down

0 comments on commit 85b08f4

Please sign in to comment.