View Javadoc
1   package org.apache.maven.shared.release.exec;
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  public class RawStreamPumper
27      extends Thread 
28  {
29      private InputStream in;
30  
31      private OutputStream out;
32  
33      boolean done;
34      
35      boolean poll;
36      
37      byte buffer[] = new byte[256];
38  
39      public RawStreamPumper( InputStream in , OutputStream out, boolean poll )
40      {
41          this.in = in;
42          this.out = out;
43          this.poll = poll;
44      }
45  
46      public RawStreamPumper( InputStream in , OutputStream out )
47      {
48          this.in = in;
49          this.out = out;
50          this.poll = false;
51      }
52  
53      public void setDone()
54      {
55          done = true;
56      }
57  
58      public void closeInput() 
59          throws IOException
60      {
61          in.close();
62      }
63  
64      public void closeOutput()
65          throws IOException
66      {
67          out.close();
68      }
69  
70      public void run()
71      {
72          try
73          {
74              if ( poll )
75              {
76                  while ( !done )
77                  {
78                      if ( in.available() > 0 )
79                      {
80                          int i = in.read( buffer );
81                          if ( i != -1 )
82                          {
83                              out.write( buffer, 0, i );
84                              out.flush();
85                          }
86                          else
87                          {
88                              done = true;
89                          }
90                      }
91                      else
92                      {
93                          Thread.sleep( 1 );
94                      }
95                  }
96              }
97              else
98              {
99                  int i = in.read( buffer );
100                 while ( i != -1 && !done )
101                 {
102                     if ( i != -1 )
103                     {
104                         out.write( buffer, 0, i );
105                         out.flush();
106                     }
107                     else
108                     {
109                         done = true;
110                     }
111                     i = in.read( buffer );
112                 }
113             }
114         }
115         catch ( Throwable e )
116         {
117             // Catched everything
118         }
119         finally
120         {
121             done = true;
122         }
123     }
124 }