Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
StreamPumper |
|
| 1.5555555555555556;1,556 |
1 | package org.codehaus.plexus.util.cli; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | import org.codehaus.plexus.util.IOUtil; | |
23 | ||
24 | import java.io.BufferedReader; | |
25 | import java.io.InputStream; | |
26 | import java.io.InputStreamReader; | |
27 | import java.io.PrintWriter; | |
28 | ||
29 | /** | |
30 | * Class to pump the error stream during Process's runtime. Copied from the Ant | |
31 | * built-in task. | |
32 | * | |
33 | * @author <a href="mailto:fvancea@maxiq.com">Florin Vancea </a> | |
34 | * @author <a href="mailto:pj@thoughtworks.com">Paul Julius </a> | |
35 | * @since June 11, 2001 | |
36 | */ | |
37 | public class StreamPumper | |
38 | extends Thread | |
39 | { | |
40 | private BufferedReader in; | |
41 | ||
42 | 0 | private StreamConsumer consumer = null; |
43 | ||
44 | 0 | private PrintWriter out = null; |
45 | ||
46 | private static final int SIZE = 1024; | |
47 | ||
48 | boolean done; | |
49 | ||
50 | public StreamPumper( InputStream in ) | |
51 | 0 | { |
52 | 0 | this.in = new BufferedReader( new InputStreamReader( in ), SIZE ); |
53 | 0 | } |
54 | ||
55 | public StreamPumper( InputStream in, StreamConsumer consumer ) | |
56 | { | |
57 | 0 | this( in ); |
58 | ||
59 | 0 | this.consumer = consumer; |
60 | 0 | } |
61 | ||
62 | public StreamPumper( InputStream in, PrintWriter writer ) | |
63 | { | |
64 | 0 | this( in ); |
65 | ||
66 | 0 | out = writer; |
67 | 0 | } |
68 | ||
69 | public StreamPumper( InputStream in, PrintWriter writer, StreamConsumer consumer ) | |
70 | { | |
71 | 0 | this( in ); |
72 | 0 | this.out = writer; |
73 | 0 | this.consumer = consumer; |
74 | 0 | } |
75 | ||
76 | public void run() | |
77 | { | |
78 | try | |
79 | { | |
80 | 0 | String s = in.readLine(); |
81 | ||
82 | 0 | while ( s != null ) |
83 | { | |
84 | 0 | consumeLine( s ); |
85 | ||
86 | 0 | if ( out != null ) |
87 | { | |
88 | 0 | out.println( s ); |
89 | ||
90 | 0 | out.flush(); |
91 | } | |
92 | ||
93 | 0 | s = in.readLine(); |
94 | } | |
95 | 0 | } |
96 | 0 | catch ( Throwable e ) |
97 | { | |
98 | // Catched everything so the streams will be closed and flagged as done. | |
99 | 0 | } |
100 | finally | |
101 | { | |
102 | 0 | IOUtil.close( in ); |
103 | ||
104 | 0 | done = true; |
105 | ||
106 | 0 | synchronized ( this ) |
107 | { | |
108 | 0 | this.notifyAll(); |
109 | 0 | } |
110 | 0 | } |
111 | 0 | } |
112 | ||
113 | public void flush() | |
114 | { | |
115 | 0 | if ( out != null ) |
116 | { | |
117 | 0 | out.flush(); |
118 | } | |
119 | 0 | } |
120 | ||
121 | public void close() | |
122 | { | |
123 | 0 | IOUtil.close( out ); |
124 | 0 | } |
125 | ||
126 | public boolean isDone() | |
127 | { | |
128 | 0 | return done; |
129 | } | |
130 | ||
131 | private void consumeLine( String line ) | |
132 | { | |
133 | 0 | if ( consumer != null ) |
134 | { | |
135 | 0 | consumer.consumeLine( line ); |
136 | } | |
137 | 0 | } |
138 | } |