KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > Pipe


1 /*
2   Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.util;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.OutputStreamWriter JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.io.Writer JavaDoc;
28 import org.apache.log4j.Logger;
29
30
31 /**
32  * A Pipe is a thread which reads some data from a Reader and writes
33  * it to a Writer line by line until EOF is reached in the Reader.
34  */

35 public class Pipe extends Thread JavaDoc
36 {
37     static Logger logger = Logger.getLogger("util.pipe");
38
39     /**
40      * Creates a new pipe.
41      * @param input the input of the pipe
42      * @param output the ouput of a pipe
43      */

44     public Pipe(Reader JavaDoc input, Writer JavaDoc output) {
45         this.input = new BufferedReader JavaDoc(input);
46         this.output = new PrintWriter JavaDoc(output);
47     }
48
49     /**
50     * Creates a new pipe.
51     * @param input the input of the pipe
52     * @param output the ouput of a pipe
53     */

54     public Pipe(InputStream JavaDoc input, OutputStream JavaDoc output) {
55         this.input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(input));
56         this.output = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(output));
57     }
58
59     BufferedReader JavaDoc input;
60     PrintWriter JavaDoc output;
61
62     public void run() {
63         try {
64             String JavaDoc line;
65             while ((line=input.readLine())!=null) {
66                 System.out.println(line);
67                 // this does not work, why ?
68
//output.println(line);
69
}
70         } catch(Exception JavaDoc e) {
71             logger.error("Caught exception in Pipe thread: "+e);
72         }
73     }
74 }
75
Popular Tags