1 package org.apache.maven.shared.utils.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 java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.PrintWriter;
27 import java.io.Reader;
28 import java.nio.charset.Charset;
29
30 import javax.annotation.Nullable;
31
32 import org.apache.maven.shared.utils.io.IOUtil;
33
34 /**
35 * Class to pump the error stream during Process's runtime. Copied from the Ant built-in task.
36 *
37 * @author <a href="mailto:fvancea@maxiq.com">Florin Vancea </a>
38 * @author <a href="mailto:pj@thoughtworks.com">Paul Julius </a>
39 */
40 public class StreamPumper
41 extends AbstractStreamHandler
42 {
43 private final BufferedReader in;
44
45 private final StreamConsumer consumer;
46
47 private final PrintWriter out;
48
49 private volatile Exception exception = null;
50
51 private static final int SIZE = 1024;
52
53 /**
54 * @param in {@link InputStream}
55 * @param consumer {@link StreamConsumer}
56 */
57 public StreamPumper( InputStream in, StreamConsumer consumer )
58 {
59 this( new InputStreamReader( in ), null, consumer );
60 }
61
62 /**
63 * @param in {@link InputStream}
64 * @param consumer {@link StreamConsumer}
65 * @param charset {@link Charset}
66 */
67 public StreamPumper( InputStream in, StreamConsumer consumer, @Nullable Charset charset )
68 {
69 this( null == charset ? new InputStreamReader( in ) : new InputStreamReader( in, charset ), null, consumer );
70 }
71
72 /**
73 * @param in {@link Writer}
74 * @param writer {@link PrintWriter}
75 * @param consumer {@link StreamConsumer}
76 */
77 private StreamPumper( Reader in, PrintWriter writer, StreamConsumer consumer )
78 {
79 this.in = new BufferedReader( in, SIZE );
80 this.out = writer;
81 this.consumer = consumer;
82 }
83
84 /** run it. */
85 public void run()
86 {
87 try
88 {
89 for ( String line = in.readLine(); line != null; line = in.readLine() )
90 {
91 try
92 {
93 if ( exception == null )
94 {
95 consumeLine( line );
96 }
97 }
98 catch ( Exception t )
99 {
100 exception = t;
101 }
102
103 if ( out != null )
104 {
105 out.println( line );
106
107 out.flush();
108 }
109
110 }
111 }
112 catch ( IOException e )
113 {
114 exception = e;
115 }
116 finally
117 {
118 IOUtil.close( in );
119
120 synchronized ( this )
121 {
122 setDone();
123
124 this.notifyAll();
125 }
126 }
127 }
128
129 /**
130 * flush.
131 */
132 public void flush()
133 {
134 if ( out != null )
135 {
136 out.flush();
137 }
138 }
139
140 /**
141 * Close it.
142 */
143 public void close()
144 {
145 IOUtil.close( out );
146 }
147
148 /**
149 * @return {@link Exception}
150 */
151 public Exception getException()
152 {
153 return exception;
154 }
155
156 private void consumeLine( String line )
157 {
158 if ( consumer != null && !isDisabled() )
159 {
160 consumer.consumeLine( line );
161 }
162 }
163 }