Last active
November 21, 2021 11:34
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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