Skip to content

Instantly share code, notes, and snippets.

@whitfin
Last active November 21, 2021 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save whitfin/497a88800d6d03b8cd7f to your computer and use it in GitHub Desktop.
Save whitfin/497a88800d6d03b8cd7f to your computer and use it in GitHub Desktop.
A simple Thread extension to control a redirection of an InputStream to an OutputStream.
package com.zackehh.example;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* A Thread extension dedicated to redirecting InputStreams
* to given OutputStreams. Feeds into the given OutputStream
* during the lifetime of this Thread.
*
* @author Isaac Whitfield
* @version 10/05/2014
*/
public class DirectedStream extends Thread {
public DirectedStream(final InputStream in, final OutputStream out){
super(handler(in, out));
start();
}
public DirectedStream(final InputStream in, final OutputStream out, final boolean daemon){
super(handler(in, out));
setDaemon(daemon);
start();
}
private static Runnable handler(final InputStream in, final OutputStream out){
return new Runnable() {
@Override
public void run() {
try {
int d;
while ((d = in.read()) != -1) {
out.write(d);
}
} catch (IOException ex) {
// Stream failure
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment