1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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 * Class to pump the error stream during Process's runtime. Copied from the Ant built-in task.
32 *
33 * @author <a href="mailto:fvancea@maxiq.com">Florin Vancea </a>
34 * @author <a href="mailto:pj@thoughtworks.com">Paul Julius </a>
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 * @param in {@link InputStream}
47 * @param consumer {@link StreamConsumer}
48 */
49 public StreamPumper(InputStream in, StreamConsumer consumer) {
50 this(new InputStreamReader(in), consumer);
51 }
52
53 /**
54 * @param in {@link InputStream}
55 * @param consumer {@link StreamConsumer}
56 * @param charset {@link Charset}
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 * @param in {@link Reader}
64 * @param consumer {@link StreamConsumer}
65 */
66 private StreamPumper(Reader in, StreamConsumer consumer) {
67 super();
68 this.in = new BufferedReader(in, SIZE);
69 this.consumer = consumer;
70 }
71
72 /** run it. */
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 * flush.
105 *
106 * @deprecated As of 3.2.0, removed without replacement.
107 */
108 @Deprecated
109 public void flush() {
110 // Nothing to flush.
111 }
112
113 /**
114 * Close it.
115 *
116 * @deprecated As of 3.2.0, removed without replacement.
117 */
118 @Deprecated
119 public void close() {
120 // Nothing to close.
121 }
122
123 /**
124 * @return {@link Exception}
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 }