Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
StreamFeeder |
|
| 2.2;2,2 |
1 | package org.codehaus.plexus.util.cli; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | import java.io.IOException; | |
23 | import java.io.InputStream; | |
24 | import java.io.OutputStream; | |
25 | ||
26 | /** | |
27 | * Read from an InputStream and write the output to an OutputStream. | |
28 | * | |
29 | * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a> | |
30 | * @version $Id: StreamFeeder.java 591654 2007-11-03 17:14:56Z dennisl $ | |
31 | */ | |
32 | public class StreamFeeder | |
33 | extends Thread | |
34 | { | |
35 | private InputStream input; | |
36 | ||
37 | private OutputStream output; | |
38 | ||
39 | private boolean done; | |
40 | ||
41 | /** | |
42 | * Create a new StreamFeeder | |
43 | * | |
44 | * @param input Stream to read from | |
45 | * @param output Stream to write to | |
46 | */ | |
47 | public StreamFeeder( InputStream input, OutputStream output ) | |
48 | 0 | { |
49 | 0 | this.input = input; |
50 | ||
51 | 0 | this.output = output; |
52 | 0 | } |
53 | ||
54 | // ---------------------------------------------------------------------- | |
55 | // Runnable implementation | |
56 | // ---------------------------------------------------------------------- | |
57 | ||
58 | public void run() | |
59 | { | |
60 | try | |
61 | { | |
62 | 0 | feed(); |
63 | 0 | } |
64 | 0 | catch ( Throwable ex ) |
65 | { | |
66 | // Catched everything so the streams will be closed and flagged as done. | |
67 | 0 | } |
68 | finally | |
69 | { | |
70 | 0 | close(); |
71 | ||
72 | 0 | done = true; |
73 | ||
74 | 0 | synchronized ( this ) |
75 | { | |
76 | 0 | this.notifyAll(); |
77 | 0 | } |
78 | 0 | } |
79 | 0 | } |
80 | ||
81 | // ---------------------------------------------------------------------- | |
82 | // | |
83 | // ---------------------------------------------------------------------- | |
84 | ||
85 | public void close() | |
86 | { | |
87 | 0 | if ( input != null ) |
88 | { | |
89 | 0 | synchronized ( input ) |
90 | { | |
91 | try | |
92 | { | |
93 | 0 | input.close(); |
94 | } | |
95 | 0 | catch ( IOException ex ) |
96 | { | |
97 | // ignore | |
98 | 0 | } |
99 | ||
100 | 0 | input = null; |
101 | 0 | } |
102 | } | |
103 | ||
104 | 0 | if ( output != null ) |
105 | { | |
106 | 0 | synchronized ( output ) |
107 | { | |
108 | try | |
109 | { | |
110 | 0 | output.close(); |
111 | } | |
112 | 0 | catch ( IOException ex ) |
113 | { | |
114 | // ignore | |
115 | 0 | } |
116 | ||
117 | 0 | output = null; |
118 | 0 | } |
119 | } | |
120 | 0 | } |
121 | ||
122 | public boolean isDone() | |
123 | { | |
124 | 0 | return done; |
125 | } | |
126 | ||
127 | // ---------------------------------------------------------------------- | |
128 | // | |
129 | // ---------------------------------------------------------------------- | |
130 | ||
131 | private void feed() | |
132 | throws IOException | |
133 | { | |
134 | 0 | int data = input.read(); |
135 | ||
136 | 0 | while ( !done && data != -1 ) |
137 | { | |
138 | 0 | synchronized ( output ) |
139 | { | |
140 | 0 | output.write( data ); |
141 | ||
142 | 0 | data = input.read(); |
143 | 0 | } |
144 | } | |
145 | 0 | } |
146 | } |