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   *
28   */
29  public class StreamFeeder extends AbstractStreamHandler {
30  
31      private InputStream input;
32  
33      private OutputStream output;
34  
35      private volatile Throwable exception = null;
36  
37      /**
38       * Create a new StreamFeeder
39       *
40       * @param input Stream to read from
41       * @param output Stream to write to
42       */
43      public StreamFeeder(InputStream input, OutputStream output) {
44          super();
45          this.input = input;
46          this.output = output;
47      }
48  
49      @Override
50      public void run() {
51          try {
52              feed();
53          } catch (Throwable ex) {
54              if (exception == null) {
55                  exception = ex;
56              }
57          } finally {
58              close();
59  
60              synchronized (this) {
61                  setDone();
62  
63                  this.notifyAll();
64              }
65          }
66      }
67  
68      public void close() {
69          if (input != null) {
70              synchronized (input) {
71                  try {
72                      input.close();
73                  } catch (IOException ex) {
74                      if (exception == null) {
75                          exception = ex;
76                      }
77                  }
78  
79                  input = null;
80              }
81          }
82  
83          if (output != null) {
84              synchronized (output) {
85                  try {
86                      output.close();
87                  } catch (IOException ex) {
88                      if (exception == null) {
89                          exception = ex;
90                      }
91                  }
92  
93                  output = null;
94              }
95          }
96      }
97  
98      /**
99       * @since 3.1.0
100      * @return the Exception
101      */
102     public Throwable getException() {
103         return exception;
104     }
105 
106     private void feed() throws IOException {
107         boolean flush = false;
108         int data = input.read();
109 
110         while (!isDone() && data != -1) {
111             synchronized (output) {
112                 if (!isDisabled()) {
113                     output.write(data);
114                     flush = true;
115                 }
116 
117                 data = input.read();
118             }
119         }
120 
121         if (flush) {
122             output.flush();
123         }
124     }
125 }