KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > StreamPumper


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 /*
38  * The Apache Software License, Version 1.1
39  *
40  * Copyright (c) 2000,2002 The Apache Software Foundation. All rights
41  * reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  *
47  * 1. Redistributions of source code must retain the above copyright
48  * notice, this list of conditions and the following disclaimer.
49  *
50  * 2. Redistributions in binary form must reproduce the above copyright
51  * notice, this list of conditions and the following disclaimer in
52  * the documentation and/or other materials provided with the
53  * distribution.
54  *
55  * 3. The end-user documentation included with the redistribution, if
56  * any, must include the following acknowlegement:
57  * "This product includes software developed by the
58  * Apache Software Foundation (http://www.apache.org/)."
59  * Alternately, this acknowlegement may appear in the software itself,
60  * if and wherever such third-party acknowlegements normally appear.
61  *
62  * 4. The names "The Jakarta Project", "Ant", and "Apache Software
63  * Foundation" must not be used to endorse or promote products derived
64  * from this software without prior written permission. For written
65  * permission, please contact apache@apache.org.
66  *
67  * 5. Products derived from this software may not be called "Apache"
68  * nor may "Apache" appear in their names without prior written
69  * permission of the Apache Group.
70  *
71  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
72  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
73  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
74  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
75  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
76  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
77  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
78  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
79  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
80  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
81  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
82  * SUCH DAMAGE.
83  * ====================================================================
84  *
85  * This software consists of voluntary contributions made by many
86  * individuals on behalf of the Apache Software Foundation. For more
87  * information on the Apache Software Foundation, please see
88  * <http://www.apache.org/>.
89  */

90 package net.sourceforge.cruisecontrol.util;
91
92 import java.io.BufferedReader JavaDoc;
93 import java.io.IOException JavaDoc;
94 import java.io.InputStream JavaDoc;
95 import java.io.InputStreamReader JavaDoc;
96 import java.io.PrintWriter JavaDoc;
97
98 /**
99  * Class to pump the error stream during Process's runtime. Copied from
100  * the Ant built-in task.
101  *
102  * @since June 11, 2001
103  * @author <a HREF="mailto:fvancea@maxiq.com">Florin Vancea</a>
104  * @author <a HREF="mailto:pj@thoughtworks.com">Paul Julius</a>
105  */

106 public class StreamPumper implements Runnable JavaDoc {
107
108     private BufferedReader JavaDoc in;
109     private StreamConsumer consumer = null;
110     private PrintWriter JavaDoc out = new PrintWriter JavaDoc(System.out);
111     private static final int SIZE = 1024;
112
113     public StreamPumper(InputStream JavaDoc in, PrintWriter JavaDoc writer) {
114         this(in);
115         out = writer;
116     }
117
118     public StreamPumper(InputStream JavaDoc in) {
119         this.in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in), SIZE);
120     }
121
122     public StreamPumper(InputStream JavaDoc in, StreamConsumer consumer) {
123         this(in);
124         this.consumer = consumer;
125     }
126
127     public StreamPumper(InputStream JavaDoc in, PrintWriter JavaDoc writer,
128                         StreamConsumer consumer) {
129         this(in);
130         this.out = writer;
131         this.consumer = consumer;
132     }
133
134     public void run() {
135         try {
136             String JavaDoc s = in.readLine();
137             while (s != null) {
138                 consumeLine(s);
139                 if (out != null) {
140                     out.println(s);
141                     out.flush();
142                 }
143
144                 s = in.readLine();
145             }
146         } catch (IOException JavaDoc e) {
147             // do nothing
148
} finally {
149             try {
150                 in.close();
151             } catch (IOException JavaDoc e) {
152                 // do nothing
153
}
154         }
155     }
156
157     public void flush() {
158         out.flush();
159     }
160
161     public void close() {
162         flush();
163         out.close();
164     }
165
166     private void consumeLine(String JavaDoc line) {
167         if (consumer != null) {
168             consumer.consumeLine(line);
169         }
170     }
171 }
172
Popular Tags