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 private volatile Exception exception = null;
47
48 private static final int SIZE = 1024;
49
50 public StreamPumper( InputStream in, StreamConsumer consumer )
51 {
52 this( in, null, consumer );
53 }
54
55 private StreamPumper( InputStream in, PrintWriter writer, StreamConsumer consumer )
56 {
57 this.in = new BufferedReader( new InputStreamReader( in ), SIZE );
58 this.out = writer;
59 this.consumer = consumer;
60 }
61
62 public void run()
63 {
64 try
65 {
66 for ( String line = in.readLine(); line != null; line = in.readLine() )
67 {
68 try
69 {
70 if ( exception == null )
71 {
72 consumeLine( line );
73 }
74 }
75 catch ( Exception t )
76 {
77 exception = t;
78 }
79
80 if ( out != null )
81 {
82 out.println( line );
83
84 out.flush();
85 }
86
87 }
88 }
89 catch ( IOException e )
90 {
91 exception = e;
92 }
93 finally
94 {
95 IOUtil.close( in );
96
97 synchronized ( this )
98 {
99 setDone();
100
101 this.notifyAll();
102 }
103 }
104 }
105
106 public void flush()
107 {
108 if ( out != null )
109 {
110 out.flush();
111 }
112 }
113
114 public void close()
115 {
116 IOUtil.close( out );
117 }
118
119 public Exception getException()
120 {
121 return exception;
122 }
123
124 private void consumeLine( String line )
125 {
126 if ( consumer != null && !isDisabled() )
127 {
128 consumer.consumeLine( line );
129 }
130 }
131 }