Piped Input & Output Streams
I have been doing java for a long time, and I know what piping is and what it's good for, but for the life of me I've never been able to figure out PipedInputStream and PipedOutputStream, how to create them and how to get them to pipe data.
Does anyone have code where they use the Piping streams? Can anyone explain to me how to use them and when to use them?
[375 byte] By [
tjacobs01a] at [2007-11-27 8:55:04]

> ...
> Does anyone have code where they use the Piping
> streams? Can anyone explain to me how to use them and
> when to use them?
I don't, but exampledepot.com has an example:
http://www.exampledepot.com/egs/javax.swing.text/ta_Console.html
No idea when this (functionality) can be useful though...
Some minor tweaks:
import java.awt.*;
import java.io.*;
import javax.swing.*;
/**
* This example creates a console window using a JTextArea which shows
* all output printed to System.out and System.err. System.out and
* System.err are replaced with piped io streams. Two reader threads
* are created that retrieve the output from these piped io streams
* and append it to the JTextArea component.
*
* @author http://www.exampledepot.com/egs/javax.swing.text/ta_Console.html
*/
@SuppressWarnings("serial")
public class MyConsole extends JFrame {
PipedInputStream piOut;
PipedInputStream piErr;
PipedOutputStream poOut;
PipedOutputStream poErr;
JTextArea textArea = new JTextArea();
public MyConsole() throws IOException {
// Set up System.out
piOut = new PipedInputStream();
poOut = new PipedOutputStream(piOut);
System.setOut(new PrintStream(poOut, true));
// Set up System.err
piErr = new PipedInputStream();
poErr = new PipedOutputStream(piErr);
System.setErr(new PrintStream(poErr, true));
// Add a scrolling text area
textArea.setEditable(false);
textArea.setRows(20);
textArea.setColumns(50);
super.getContentPane().add(new JScrollPane(textArea), BorderLayout.CENTER);
super.pack();
super.setVisible(true);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create reader threads
new ReaderThread(piOut).start();
new ReaderThread(piErr).start();
}
class ReaderThread extends Thread {
PipedInputStream pi;
ReaderThread(PipedInputStream pi) {
this.pi = pi;
}
public void run() {
final byte[] buf = new byte[1024];
try {
while (true) {
final int len = pi.read(buf);
if (len == -1) {
break;
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(new String(buf, 0, len));
// Make sure the last line is always visible
textArea.setCaretPosition(textArea.getDocument().getLength());
// Keep the text area down to a certain character size
int idealSize = 1000;
int maxExcess = 500;
int excess = textArea.getDocument().getLength() - idealSize;
if (excess >= maxExcess) {
textArea.replaceRange("", 0, excess);
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
new MyConsole();
System.err.println("err -> Does it work?");
System.out.println("out -> Apparently it does!");
} catch (IOException e) {
e.printStackTrace();
}
}
}