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