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