View Javadoc

1   /*
2    * Copyright 2007 scala-tools.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing,
11   * software distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions
14   * and limitations under the License.
15   */
16  package org.scala_tools.maven;
17  
18  import java.io.InputStream;
19  import java.io.OutputStream;
20  
21  import org.codehaus.plexus.util.IOUtil;
22  
23  public class StreamPiper extends Thread {
24  
25      private InputStream in_;
26      private OutputStream out_;
27  
28      public StreamPiper(InputStream in, OutputStream out) {
29          in_ = in;
30          out_ = out;
31      }
32  
33      @Override
34      public void run() {
35          try {
36              byte[] buffer = new byte[512];
37              int bytes_read;
38              while (true) {
39                  bytes_read = in_.read(buffer);
40                  if (bytes_read != -1) {
41                      out_.write(buffer, 0, bytes_read);
42                      out_.flush();
43                  }
44  //                System.err.print("sleep");
45                  if (bytes_read < 1) {
46                      yield();
47                      sleep(500l);
48                  }
49              }
50          } catch (InterruptedException exc) {
51              System.err.print("stop by interrupt");
52              return;
53          } catch (Exception exc) {
54              System.err.print("!!!! exc !!!");
55              exc.printStackTrace();
56              throw new RuntimeException("wrap: " + exc.getMessage(), exc);
57          } finally {
58              IOUtil.close(in_);
59              IOUtil.close(out_);
60          }
61      }
62  }