Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
StreamFeeder |
|
| 3.0;3 |
1 | package org.apache.maven.shared.utils.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 1396135 2012-10-09 17:45:37Z krosenvold $ | |
31 | */ | |
32 | class StreamFeeder | |
33 | extends AbstractStreamHandler | |
34 | { | |
35 | private InputStream input; | |
36 | ||
37 | private OutputStream output; | |
38 | ||
39 | ||
40 | /** | |
41 | * Create a new StreamFeeder | |
42 | * | |
43 | * @param input Stream to read from | |
44 | * @param output Stream to write to | |
45 | */ | |
46 | public StreamFeeder( InputStream input, OutputStream output ) | |
47 | 0 | { |
48 | 0 | this.input = input; |
49 | ||
50 | 0 | this.output = output; |
51 | 0 | } |
52 | ||
53 | // ---------------------------------------------------------------------- | |
54 | // Runnable implementation | |
55 | // ---------------------------------------------------------------------- | |
56 | ||
57 | public void run() | |
58 | { | |
59 | try | |
60 | { | |
61 | 0 | feed(); |
62 | } | |
63 | 0 | catch ( Throwable ex ) |
64 | { | |
65 | // Catched everything so the streams will be closed and flagged as done. | |
66 | } | |
67 | finally | |
68 | { | |
69 | 0 | close(); |
70 | ||
71 | 0 | synchronized ( this ) |
72 | { | |
73 | 0 | setDone(); |
74 | ||
75 | 0 | this.notifyAll(); |
76 | 0 | } |
77 | 0 | } |
78 | 0 | } |
79 | ||
80 | // ---------------------------------------------------------------------- | |
81 | // | |
82 | // ---------------------------------------------------------------------- | |
83 | ||
84 | public void close() | |
85 | { | |
86 | 0 | if ( input != null ) |
87 | { | |
88 | 0 | synchronized ( input ) |
89 | { | |
90 | try | |
91 | { | |
92 | 0 | input.close(); |
93 | } | |
94 | 0 | catch ( IOException ex ) |
95 | { | |
96 | // ignore | |
97 | 0 | } |
98 | ||
99 | 0 | input = null; |
100 | 0 | } |
101 | } | |
102 | ||
103 | 0 | if ( output != null ) |
104 | { | |
105 | 0 | synchronized ( output ) |
106 | { | |
107 | try | |
108 | { | |
109 | 0 | output.close(); |
110 | } | |
111 | 0 | catch ( IOException ex ) |
112 | { | |
113 | // ignore | |
114 | 0 | } |
115 | ||
116 | 0 | output = null; |
117 | 0 | } |
118 | } | |
119 | 0 | } |
120 | ||
121 | // ---------------------------------------------------------------------- | |
122 | // | |
123 | // ---------------------------------------------------------------------- | |
124 | ||
125 | private void feed() | |
126 | throws IOException | |
127 | { | |
128 | 0 | int data = input.read(); |
129 | ||
130 | 0 | while ( !isDone() && data != -1 ) |
131 | { | |
132 | 0 | synchronized ( output ) |
133 | { | |
134 | 0 | if ( !isDisabled() ) |
135 | { | |
136 | 0 | output.write( data ); |
137 | } | |
138 | ||
139 | 0 | data = input.read(); |
140 | 0 | } |
141 | } | |
142 | 0 | } |
143 | ||
144 | } |