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.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  
26  /**
27   * Read from an InputStream and write the output to an OutputStream.
28   *
29   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
30   * @version $Id: StreamFeeder.java 1396135 2012-10-09 17:45:37Z krosenvold $
31   */
32  class StreamFeeder
33      extends AbstractStreamHandler
34  {
35      private InputStream input;
36  
37      private OutputStream output;
38  
39  
40      /**
41       * Create a new StreamFeeder
42       *
43       * @param input  Stream to read from
44       * @param output Stream to write to
45       */
46      public StreamFeeder( InputStream input, OutputStream output )
47      {
48          this.input = input;
49  
50          this.output = output;
51      }
52  
53      // ----------------------------------------------------------------------
54      // Runnable implementation
55      // ----------------------------------------------------------------------
56  
57      public void run()
58      {
59          try
60          {
61              feed();
62          }
63          catch ( Throwable ex )
64          {
65              // Catched everything so the streams will be closed and flagged as done.
66          }
67          finally
68          {
69              close();
70  
71              synchronized ( this )
72              {
73                  setDone();
74  
75                  this.notifyAll();
76              }
77          }
78      }
79  
80      // ----------------------------------------------------------------------
81      //
82      // ----------------------------------------------------------------------
83  
84      public void close()
85      {
86          if ( input != null )
87          {
88              synchronized ( input )
89              {
90                  try
91                  {
92                      input.close();
93                  }
94                  catch ( IOException ex )
95                  {
96                      // ignore
97                  }
98  
99                  input = null;
100             }
101         }
102 
103         if ( output != null )
104         {
105             synchronized ( output )
106             {
107                 try
108                 {
109                     output.close();
110                 }
111                 catch ( IOException ex )
112                 {
113                     // ignore
114                 }
115 
116                 output = null;
117             }
118         }
119     }
120 
121     // ----------------------------------------------------------------------
122     //
123     // ----------------------------------------------------------------------
124 
125     private void feed()
126         throws IOException
127     {
128         int data = input.read();
129 
130         while ( !isDone() && data != -1 )
131         {
132             synchronized ( output )
133             {
134                 if ( !isDisabled() )
135                 {
136                     output.write( data );
137                 }
138 
139                 data = input.read();
140             }
141         }
142     }
143 
144 }