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