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 public void run()
74 {
75 try
76 {
77 if ( poll )
78 {
79 while ( !done )
80 {
81 if ( in.available() > 0 )
82 {
83 int i = in.read( buffer );
84 if ( i != -1 )
85 {
86 out.write( buffer, 0, i );
87 out.flush();
88 }
89 else
90 {
91 done = true;
92 }
93 }
94 else
95 {
96 Thread.sleep( 1 );
97 }
98 }
99 }
100 else
101 {
102 int i = in.read( buffer );
103 while ( i != -1 && !done )
104 {
105 if ( i != -1 )
106 {
107 out.write( buffer, 0, i );
108 out.flush();
109 }
110 else
111 {
112 done = true;
113 }
114 i = in.read( buffer );
115 }
116 }
117 }
118 catch ( Throwable e )
119 {
120 // Catched everything
121 }
122 finally
123 {
124 done = true;
125 }
126 }
127 }