Skip to content

Commit

Permalink
Fixed the high latency which results when using a PipedWriter/PipedOu…
Browse files Browse the repository at this point in the history
  • Loading branch information
luontola committed Jun 15, 2010
1 parent f1a7016 commit 841876a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
Expand Up @@ -75,20 +75,20 @@ private void startServer() throws IOException {
);
serverProcess = builder.start();

PipedOutputStream toWatcher = streamToWatcher();
OutputStream toWatcher = streamToWatcher();
redirectStream(serverProcess.getInputStream(), System.out, toWatcher);
redirectStream(serverProcess.getErrorStream(), System.err, toWatcher);
}

private PipedOutputStream streamToWatcher() throws IOException {
private OutputStream streamToWatcher() throws IOException {
assert outputWatcher == null;
PipedInputStream in = new PipedInputStream();
PipedOutputStream toWatcher = new PipedOutputStream(in);
PipedOutputStream toWatcher = new LowLatencyPipedOutputStream(in);
outputWatcher = new StreamWatcher(new InputStreamReader(in));
return toWatcher;
}

private static void redirectStream(InputStream input, OutputStream systemOut, PipedOutputStream toWatcher) {
private static void redirectStream(InputStream input, OutputStream systemOut, OutputStream toWatcher) {
redirectStream(new TeeInputStream(input, toWatcher), new CloseShieldOutputStream(systemOut));
}

Expand Down
@@ -0,0 +1,32 @@
// Copyright © 2008-2010 Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://dimdwarf.sourceforge.net/LICENSE

package net.orfjackal.dimdwarf.test.util;

import java.io.*;

/**
* {@link java.io.PipedOutputStream} should be flushed after every write, or otherwise
* there will be a one-second latency for the reader. This class does that automatically.
* <a href="http://stackoverflow.com/questions/2843555/better-alternative-for-pipedreader-pipedwriter/2844689#2844689">More information</a>.
*/
public class LowLatencyPipedOutputStream extends PipedOutputStream {

public LowLatencyPipedOutputStream(PipedInputStream snk) throws IOException {
super(snk);
}

public LowLatencyPipedOutputStream() {
}

public void write(int b) throws IOException {
super.write(b);
flush();
}

public void write(byte[] b, int off, int len) throws IOException {
super.write(b, off, len);
flush();
}
}
@@ -0,0 +1,32 @@
// Copyright © 2008-2010 Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://dimdwarf.sourceforge.net/LICENSE

package net.orfjackal.dimdwarf.test.util;

import java.io.*;

/**
* {@link java.io.PipedWriter} should be flushed after every write, or otherwise
* there will be a one-second latency for the reader. This class does that automatically.
* <a href="http://stackoverflow.com/questions/2843555/better-alternative-for-pipedreader-pipedwriter/2844689#2844689">More information</a>.
*/
public class LowLatencyPipedWriter extends PipedWriter {

public LowLatencyPipedWriter(PipedReader snk) throws IOException {
super(snk);
}

public LowLatencyPipedWriter() {
}

public void write(int c) throws IOException {
super.write(c);
flush();
}

public void write(char[] cbuf, int off, int len) throws IOException {
super.write(cbuf, off, len);
flush();
}
}
Expand Up @@ -19,8 +19,8 @@ public class StreamWatcherTest {

@Before
public void setUp() throws IOException {
PipedWriter w = new PipedWriter();
PipedReader r = new PipedReader(w);
PipedReader r = new PipedReader();
PipedWriter w = new LowLatencyPipedWriter(r);
stream = new PrintWriter(w);
watcher = new StreamWatcher(r);
}
Expand Down

0 comments on commit 841876a

Please sign in to comment.