1 | |
package org.apache.maven.shared.utils.cli; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.BufferedReader; |
23 | |
import java.io.IOException; |
24 | |
import java.io.InputStream; |
25 | |
import java.io.InputStreamReader; |
26 | |
import java.io.PrintWriter; |
27 | |
import org.apache.maven.shared.utils.io.IOUtil; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class StreamPumper |
38 | |
extends AbstractStreamHandler |
39 | |
{ |
40 | |
private final BufferedReader in; |
41 | |
|
42 | |
private final StreamConsumer consumer; |
43 | |
|
44 | |
private final PrintWriter out; |
45 | |
|
46 | 0 | private volatile Exception exception = null; |
47 | |
|
48 | |
private static final int SIZE = 1024; |
49 | |
|
50 | |
public StreamPumper( InputStream in, StreamConsumer consumer ) |
51 | |
{ |
52 | 0 | this( in, null, consumer ); |
53 | 0 | } |
54 | |
|
55 | |
private StreamPumper( InputStream in, PrintWriter writer, StreamConsumer consumer ) |
56 | 0 | { |
57 | 0 | this.in = new BufferedReader( new InputStreamReader( in ), SIZE ); |
58 | 0 | this.out = writer; |
59 | 0 | this.consumer = consumer; |
60 | 0 | } |
61 | |
|
62 | |
public void run() |
63 | |
{ |
64 | |
try |
65 | |
{ |
66 | 0 | for ( String line = in.readLine(); line != null; line = in.readLine() ) |
67 | |
{ |
68 | |
try |
69 | |
{ |
70 | 0 | if ( exception == null ) |
71 | |
{ |
72 | 0 | consumeLine( line ); |
73 | |
} |
74 | |
} |
75 | 0 | catch ( Exception t ) |
76 | |
{ |
77 | 0 | exception = t; |
78 | 0 | } |
79 | |
|
80 | 0 | if ( out != null ) |
81 | |
{ |
82 | 0 | out.println( line ); |
83 | |
|
84 | 0 | out.flush(); |
85 | |
} |
86 | |
|
87 | |
} |
88 | |
} |
89 | 0 | catch ( IOException e ) |
90 | |
{ |
91 | 0 | exception = e; |
92 | |
} |
93 | |
finally |
94 | |
{ |
95 | 0 | IOUtil.close( in ); |
96 | |
|
97 | 0 | synchronized ( this ) |
98 | |
{ |
99 | 0 | setDone(); |
100 | |
|
101 | 0 | this.notifyAll(); |
102 | 0 | } |
103 | 0 | } |
104 | 0 | } |
105 | |
|
106 | |
public void flush() |
107 | |
{ |
108 | 0 | if ( out != null ) |
109 | |
{ |
110 | 0 | out.flush(); |
111 | |
} |
112 | 0 | } |
113 | |
|
114 | |
public void close() |
115 | |
{ |
116 | 0 | IOUtil.close( out ); |
117 | 0 | } |
118 | |
|
119 | |
public Exception getException() |
120 | |
{ |
121 | 0 | return exception; |
122 | |
} |
123 | |
|
124 | |
private void consumeLine( String line ) |
125 | |
{ |
126 | 0 | if ( consumer != null && !isDisabled() ) |
127 | |
{ |
128 | 0 | consumer.consumeLine( line ); |
129 | |
} |
130 | 0 | } |
131 | |
} |