1 package org.codehaus.plexus.util.cli;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22
23
24
25
26
27
28
29 public class StreamFeeder extends AbstractStreamHandler {
30
31 private InputStream input;
32
33 private OutputStream output;
34
35 private volatile Throwable exception = null;
36
37
38
39
40
41
42
43 public StreamFeeder(InputStream input, OutputStream output) {
44 super();
45 this.input = input;
46 this.output = output;
47 }
48
49 @Override
50 public void run() {
51 try {
52 feed();
53 } catch (Throwable ex) {
54 if (exception == null) {
55 exception = ex;
56 }
57 } finally {
58 close();
59
60 synchronized (this) {
61 setDone();
62
63 this.notifyAll();
64 }
65 }
66 }
67
68 public void close() {
69 if (input != null) {
70 synchronized (input) {
71 try {
72 input.close();
73 } catch (IOException ex) {
74 if (exception == null) {
75 exception = ex;
76 }
77 }
78
79 input = null;
80 }
81 }
82
83 if (output != null) {
84 synchronized (output) {
85 try {
86 output.close();
87 } catch (IOException ex) {
88 if (exception == null) {
89 exception = ex;
90 }
91 }
92
93 output = null;
94 }
95 }
96 }
97
98
99
100
101
102 public Throwable getException() {
103 return exception;
104 }
105
106 private void feed() throws IOException {
107 boolean flush = false;
108 int data = input.read();
109
110 while (!isDone() && data != -1) {
111 synchronized (output) {
112 if (!isDisabled()) {
113 output.write(data);
114 flush = true;
115 }
116
117 data = input.read();
118 }
119 }
120
121 if (flush) {
122 output.flush();
123 }
124 }
125 }