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.Reader;
27 import java.nio.charset.Charset;
28 import javax.annotation.Nullable;
29
30
31
32
33
34
35
36 public class StreamPumper
37 extends AbstractStreamHandler
38 {
39 private final BufferedReader in;
40
41 private final StreamConsumer consumer;
42
43 private volatile Exception exception = null;
44
45 private static final int SIZE = 1024;
46
47
48
49
50
51 public StreamPumper( InputStream in, StreamConsumer consumer )
52 {
53 this( new InputStreamReader( in ), consumer );
54 }
55
56
57
58
59
60
61 public StreamPumper( InputStream in, StreamConsumer consumer, @Nullable Charset charset )
62 {
63 this( null == charset ? new InputStreamReader( in ) : new InputStreamReader( in, charset ), consumer );
64 }
65
66
67
68
69
70 private StreamPumper( Reader in, StreamConsumer consumer )
71 {
72 super();
73 this.in = new BufferedReader( in, SIZE );
74 this.consumer = consumer;
75 }
76
77
78 public void run()
79 {
80 try
81 {
82 for ( String line = in.readLine(); line != null; line = in.readLine() )
83 {
84 try
85 {
86 if ( exception == null )
87 {
88 consumeLine( line );
89 }
90 }
91 catch ( Exception t )
92 {
93 exception = t;
94 }
95 }
96 }
97 catch ( IOException e )
98 {
99 exception = e;
100 }
101 finally
102 {
103 try
104 {
105 in.close();
106 }
107 catch ( final IOException e2 )
108 {
109 if ( this.exception == null )
110 {
111 this.exception = e2;
112 }
113 }
114
115 synchronized ( this )
116 {
117 setDone();
118
119 this.notifyAll();
120 }
121 }
122 }
123
124
125
126
127
128
129 @Deprecated
130 public void flush()
131 {
132
133 }
134
135
136
137
138
139
140 @Deprecated
141 public void close()
142 {
143
144 }
145
146
147
148
149 public Exception getException()
150 {
151 return exception;
152 }
153
154 private void consumeLine( String line ) throws IOException
155 {
156 if ( consumer != null && !isDisabled() )
157 {
158 consumer.consumeLine( line );
159 }
160 }
161 }