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