View Javadoc

1   package org.apache.maven.it.util.cli;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2004, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.io.OutputStream;
30  
31  /**
32   * Read from an InputStream and write the output to an OutputStream.
33   *
34   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
35   * @version $Id: StreamFeeder.java 467764 2006-10-25 21:04:07Z jvanzyl $
36   */
37  public class StreamFeeder
38      extends Thread
39  {
40      private InputStream input;
41  
42      private OutputStream output;
43  
44      private boolean done;
45  
46      /**
47       * Create a new StreamFeeder
48       *
49       * @param input  Stream to read from
50       * @param output Stream to write to
51       */
52      public StreamFeeder( InputStream input, OutputStream output )
53      {
54          this.input = input;
55  
56          this.output = output;
57      }
58  
59      // ----------------------------------------------------------------------
60      // Runnable implementation
61      // ----------------------------------------------------------------------
62  
63      public void run()
64      {
65          try
66          {
67              feed();
68          }
69          catch ( Throwable ex )
70          {
71              // Catched everything so the streams will be closed and flagged as done.
72          }
73          finally
74          {
75              close();
76  
77              done = true;
78  
79              synchronized ( this )
80              {
81                  this.notifyAll();
82              }
83          }
84      }
85  
86      // ----------------------------------------------------------------------
87      //
88      // ----------------------------------------------------------------------
89  
90      public void close()
91      {
92          if ( input != null )
93          {
94              synchronized ( input )
95              {
96                  try
97                  {
98                      input.close();
99                  }
100                 catch ( IOException ex )
101                 {
102                     // ignore
103                 }
104 
105                 input = null;
106             }
107         }
108 
109         if ( output != null )
110         {
111             synchronized ( output )
112             {
113                 try
114                 {
115                     output.close();
116                 }
117                 catch ( IOException ex )
118                 {
119                     // ignore
120                 }
121 
122                 output = null;
123             }
124         }
125     }
126 
127     public boolean isDone()
128     {
129         return done;
130     }
131 
132     // ----------------------------------------------------------------------
133     //
134     // ----------------------------------------------------------------------
135 
136     private void feed()
137         throws IOException
138     {
139         int data = input.read();
140 
141         while ( !done && data != -1 )
142         {
143             synchronized ( output )
144             {
145                 output.write( data );
146 
147                 data = input.read();
148             }
149         }
150     }
151 }