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 * @version $Id: StreamFeeder.java 1703274 2015-09-15 19:20:31Z tibordigana $
32 */
33 class StreamFeeder
34 extends AbstractStreamHandler
35 {
36 private final AtomicReference<InputStream> input;
37 private final AtomicReference<OutputStream> output;
38
39 /**
40 * Create a new StreamFeeder
41 *
42 * @param input Stream to read from
43 * @param output Stream to write to
44 */
45 public StreamFeeder( InputStream input, OutputStream output )
46 {
47 this.input = new AtomicReference<InputStream>( input );
48 this.output = new AtomicReference<OutputStream>( output );
49 }
50
51 // ----------------------------------------------------------------------
52 // Runnable implementation
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 }
66 finally
67 {
68 close();
69
70 synchronized ( this )
71 {
72 notifyAll();
73 }
74 }
75 }
76
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 // ignore
94 }
95 }
96
97 final OutputStream os = output.getAndSet( null );
98 if ( os != null )
99 {
100 try
101 {
102 os.close();
103 }
104 catch ( IOException ex )
105 {
106 // ignore
107 }
108 }
109 }
110
111 // ----------------------------------------------------------------------
112 //
113 // ----------------------------------------------------------------------
114
115 @SuppressWarnings( "checkstyle:innerassignment" )
116 private void feed()
117 throws IOException
118 {
119 InputStream is = input.get();
120 OutputStream os = output.get();
121 if ( is != null && os != null )
122 {
123 for ( int data; !isDone() && ( data = is.read() ) != -1; )
124 {
125 if ( !isDisabled() )
126 {
127 os.write( data );
128 }
129 }
130 }
131 }
132
133 }