View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.release.exec;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  
25  /**
26   * <p>RawStreamPumper class.</p>
27   */
28  public class RawStreamPumper extends Thread {
29      private final InputStream in;
30  
31      private final OutputStream out;
32  
33      boolean done;
34  
35      boolean poll;
36  
37      byte[] buffer = new byte[256];
38  
39      /**
40       * <p>Constructor for RawStreamPumper.</p>
41       *
42       * @param in   a {@link java.io.InputStream} object
43       * @param out  a {@link java.io.OutputStream} object
44       * @param poll a boolean
45       */
46      public RawStreamPumper(InputStream in, OutputStream out, boolean poll) {
47          this.in = in;
48          this.out = out;
49          this.poll = poll;
50      }
51  
52      /**
53       * <p>Constructor for RawStreamPumper.</p>
54       *
55       * @param in  a {@link java.io.InputStream} object
56       * @param out a {@link java.io.OutputStream} object
57       */
58      public RawStreamPumper(InputStream in, OutputStream out) {
59          this.in = in;
60          this.out = out;
61          this.poll = false;
62      }
63  
64      /**
65       * <p>Setter for the field <code>done</code>.</p>
66       */
67      public void setDone() {
68          done = true;
69      }
70  
71      /**
72       * <p>closeInput.</p>
73       *
74       * @throws java.io.IOException if any.
75       */
76      public void closeInput() throws IOException {
77          in.close();
78      }
79  
80      /**
81       * <p>closeOutput.</p>
82       *
83       * @throws java.io.IOException if any.
84       */
85      public void closeOutput() throws IOException {
86          out.close();
87      }
88  
89      @Override
90      public void run() {
91          try {
92              if (poll) {
93                  while (!done) {
94                      if (in.available() > 0) {
95                          int i = in.read(buffer);
96                          if (i != -1) {
97                              out.write(buffer, 0, i);
98                              out.flush();
99                          } else {
100                             done = true;
101                         }
102                     } else {
103                         Thread.sleep(1);
104                     }
105                 }
106             } else {
107                 int i = in.read(buffer);
108                 while (i != -1 && !done) {
109                     if (i != -1) {
110                         out.write(buffer, 0, i);
111                         out.flush();
112                     } else {
113                         done = true;
114                     }
115                     i = in.read(buffer);
116                 }
117             }
118         } catch (Throwable e) {
119             // Caught everything
120         } finally {
121             done = true;
122         }
123     }
124 }