3

How to implement pipe connectors, like Yahoo pipes, using Java Swing. Or any other kind of wiring in Java Swing, for that matter.

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138

1 Answers1

5

Here is an example of using PipedReader and PipedWriter in java:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedReader;
import java.io.PipedWriter;
/**
 * @date 1st May, 2011
 * @author sanju.org
 *
 * To demonstrate the use of piped character streams.
 * Example program for piped character stream in java.
 *
 */

public class PipedReaderExample {

    public static void main(String args[]) throws IOException{
        PipedReader reader = new PipedReader();
        PipedWriter writer = new PipedWriter(reader);
        Thread readerThread = new Thread(new ReaderThread(writer));
        Thread writerThread = new Thread(new WriterThread(reader));
        readerThread.start();
        writerThread.start();
    }

}

class ReaderThread implements Runnable{
    PipedWriter writer;
    public ReaderThread(PipedWriter writer){
        this.writer = writer;
    }

    public void run() {
        InputStreamReader streamReader = new InputStreamReader(System.in);
        BufferedReader bufferedReader = new BufferedReader(streamReader);
        try {
            while (true) {
                //sample implementation reading from console
                //real implementation can be reading from a socket or a file
                //or from server side code
                String line = bufferedReader.readLine();
                writer.write(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class WriterThread implements Runnable{
    PipedReader reader;
    public WriterThread(PipedReader reader){
        this.reader = reader;
    }

    public void run() {
        while(true){
            try {
                char c;
                while( (c = (char)reader.read()) != -1){
                    //write your business logic here
                    //could be writing into a file
                    //could be processing the date
                    System.out.println(c);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

As for using yahoo with PipedReader/Writer class I guess you'd read their API to connect to url etc

Here is a list of other links relating to PipedReader and PipedWriter (mix of tutorials and information) that could be of help:

As for the Swing side of things, to display the data received from a pipe you would need to use a TextComponent like one of these:

each have a setText(...) which will allow you to set their content

Reference:

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138