View Javadoc
1   package org.codehaus.plexus.util.cli;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  
23  /**
24   * Read from an InputStream and write the output to an OutputStream.
25   *
26   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
27   * @version $Id$
28   */
29  public class StreamFeeder
30      extends AbstractStreamHandler
31  {
32  
33      private InputStream input;
34  
35      private OutputStream output;
36  
37      private volatile Throwable exception = null;
38  
39      /**
40       * Create a new StreamFeeder
41       *
42       * @param input Stream to read from
43       * @param output Stream to write to
44       */
45      public StreamFeeder( InputStream input, OutputStream output )
46      {
47          super();
48          this.input = input;
49          this.output = output;
50      }
51  
52      @Override
53      public void run()
54      {
55          try
56          {
57              feed();
58          }
59          catch ( Throwable ex )
60          {
61              if ( exception == null )
62              {
63                  exception = ex;
64              }
65          }
66          finally
67          {
68              close();
69  
70              synchronized ( this )
71              {
72                  setDone();
73  
74                  this.notifyAll();
75              }
76          }
77      }
78  
79      public void close()
80      {
81          if ( input != null )
82          {
83              synchronized ( input )
84              {
85                  try
86                  {
87                      input.close();
88                  }
89                  catch ( IOException ex )
90                  {
91                      if ( exception == null )
92                      {
93                          exception = ex;
94                      }
95                  }
96  
97                  input = null;
98              }
99          }
100 
101         if ( output != null )
102         {
103             synchronized ( output )
104             {
105                 try
106                 {
107                     output.close();
108                 }
109                 catch ( IOException ex )
110                 {
111                     if ( exception == null )
112                     {
113                         exception = ex;
114                     }
115                 }
116 
117                 output = null;
118             }
119         }
120     }
121 
122     /**
123      * @since 3.1.0
124      */
125     public Throwable getException()
126     {
127         return exception;
128     }
129 
130     private void feed()
131         throws IOException
132     {
133         boolean flush = false;
134         int data = input.read();
135 
136         while ( !isDone() && data != -1 )
137         {
138             synchronized ( output )
139             {
140                 if ( !isDisabled() )
141                 {
142                     output.write( data );
143                     flush = true;
144                 }
145 
146                 data = input.read();
147             }
148         }
149 
150         if ( flush )
151         {
152             output.flush();
153         }
154     }
155 
156 }