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 import java.util.concurrent.atomic.AtomicReference;
26
27 /**
28 * Read from an InputStream and write the output to an OutputStream.
29 *
30 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a>
31 */
32 class StreamFeeder
33 extends AbstractStreamHandler
34 {
35
36 private final AtomicReference<InputStream> input;
37
38 private final AtomicReference<OutputStream> output;
39
40 private volatile Throwable exception;
41
42 /**
43 * Create a new StreamFeeder
44 *
45 * @param input Stream to read from
46 * @param output Stream to write to
47 */
48 StreamFeeder( InputStream input, OutputStream output )
49 {
50 super();
51 this.input = new AtomicReference<InputStream>( input );
52 this.output = new AtomicReference<OutputStream>( output );
53 }
54
55 @Override
56 public void run()
57 {
58 try
59 {
60 feed();
61 }
62 catch ( Throwable e )
63 {
64 // Catch everything so the streams will be closed and flagged as done.
65 if ( this.exception != null )
66 {
67 this.exception = e;
68 }
69 }
70 finally
71 {
72 close();
73
74 synchronized ( this )
75 {
76 notifyAll();
77 }
78 }
79 }
80
81 public void close()
82 {
83 setDone();
84 final InputStream is = input.getAndSet( null );
85 if ( is != null )
86 {
87 try
88 {
89 is.close();
90 }
91 catch ( IOException ex )
92 {
93 if ( this.exception != null )
94 {
95 this.exception = ex;
96 }
97 }
98 }
99
100 final OutputStream os = output.getAndSet( null );
101 if ( os != null )
102 {
103 try
104 {
105 os.close();
106 }
107 catch ( IOException ex )
108 {
109 if ( this.exception != null )
110 {
111 this.exception = ex;
112 }
113 }
114 }
115 }
116
117 /**
118 * @since 3.2.0
119 */
120 public Throwable getException()
121 {
122 return this.exception;
123 }
124
125 @SuppressWarnings( "checkstyle:innerassignment" )
126 private void feed()
127 throws IOException
128 {
129 InputStream is = input.get();
130 OutputStream os = output.get();
131 boolean flush = false;
132
133 if ( is != null && os != null )
134 {
135 for ( int data; !isDone() && ( data = is.read() ) != -1; )
136 {
137 if ( !isDisabled() )
138 {
139 os.write( data );
140 flush = true;
141 }
142 }
143
144 if ( flush )
145 {
146 os.flush();
147 }
148 }
149 }
150
151 }