001    /*
002      Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.util;
019    
020    import java.io.BufferedReader;
021    import java.io.InputStream;
022    import java.io.InputStreamReader;
023    import java.io.OutputStream;
024    import java.io.OutputStreamWriter;
025    import java.io.PrintWriter;
026    import java.io.Reader;
027    import java.io.Writer;
028    import org.apache.log4j.Logger;
029    
030    
031    /**
032     * A Pipe is a thread which reads some data from a Reader and writes
033     * it to a Writer line by line until EOF is reached in the Reader.
034     */
035    public class Pipe extends Thread
036    {
037        static Logger logger = Logger.getLogger("util.pipe");
038    
039        /**
040         * Creates a new pipe.
041         * @param input the input of the pipe
042         * @param output the ouput of a pipe
043         */
044        public Pipe(Reader input, Writer output) {
045            this.input = new BufferedReader(input);
046            this.output = new PrintWriter(output);
047        }
048    
049        /**
050        * Creates a new pipe.
051        * @param input the input of the pipe
052        * @param output the ouput of a pipe
053        */
054        public Pipe(InputStream input, OutputStream output) {
055            this.input = new BufferedReader(new InputStreamReader(input));
056            this.output = new PrintWriter(new OutputStreamWriter(output));
057        }
058    
059        BufferedReader input;
060        PrintWriter output;
061    
062        public void run() {
063            try {
064                String line;
065                while ((line=input.readLine())!=null) {
066                    System.out.println(line);
067                    // this does not work, why ?
068                    //output.println(line);
069                }
070            } catch(Exception e) {
071                logger.error("Caught exception in Pipe thread: "+e);
072            }
073        }
074    }