View Javadoc
1   package org.codehaus.plexus.util.cli;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.util.Objects;
23  
24  /**
25   * Polls an {@link InputStream} for available data and writes it to an {@link OutputStream}. Unlike
26   * {@link StreamFeeder} it never blocks in a read, so it can be stopped at any time even when the input is a stream
27   * such as {@code System.in} that has no data and no end.
28   * <p>
29   * Ported from {@code org.apache.maven.shared.utils.cli.StreamPollFeeder} of Apache Maven Shared Utils
30   * (<a href="https://issues.apache.org/jira/browse/MSHARED-1072">MSHARED-1072</a>). The input stream is never closed
31   * by this class.
32   * </p>
33   */
34  class StreamPollFeeder extends StreamFeeder {
35  
36      private static final int BUF_LEN = 80;
37  
38      private static final long POLL_INTERVAL_MILLIS = 100;
39  
40      private final InputStream pollInput;
41  
42      private final OutputStream pollOutput;
43  
44      private final Object lock = new Object();
45  
46      private volatile Throwable pollException;
47  
48      private volatile boolean stop;
49  
50      /**
51       * Create a new StreamPollFeeder.
52       *
53       * @param input stream to read from
54       * @param output stream to write to
55       */
56      StreamPollFeeder(InputStream input, OutputStream output) {
57          super(input, output);
58          this.pollInput = Objects.requireNonNull(input);
59          this.pollOutput = Objects.requireNonNull(output);
60          setDaemon(true);
61      }
62  
63      @Override
64      public void run() {
65          byte[] buf = new byte[BUF_LEN];
66  
67          try {
68              while (!stop) {
69                  if (pollInput.available() > 0) {
70                      int i = pollInput.read(buf);
71                      if (i > 0) {
72                          if (!isDisabled()) {
73                              pollOutput.write(buf, 0, i);
74                              pollOutput.flush();
75                          }
76                      } else {
77                          stop = true;
78                      }
79                  } else {
80                      synchronized (lock) {
81                          if (!stop) {
82                              lock.wait(POLL_INTERVAL_MILLIS);
83                          }
84                      }
85                  }
86              }
87          } catch (IOException e) {
88              pollException = e;
89          } catch (InterruptedException e) {
90              Thread.currentThread().interrupt();
91          } finally {
92              closeOutput();
93              synchronized (this) {
94                  setDone();
95                  notifyAll();
96              }
97          }
98      }
99  
100     private void closeOutput() {
101         try {
102             pollOutput.close();
103         } catch (IOException e) {
104             if (pollException == null) {
105                 pollException = e;
106             }
107         }
108     }
109 
110     /**
111      * Stops polling and waits for the feeder thread to finish.
112      */
113     @Override
114     public void waitUntilDone() throws InterruptedException {
115         stopPolling();
116         join();
117     }
118 
119     @Override
120     public void disable() {
121         super.disable();
122         stopPolling();
123     }
124 
125     /**
126      * The output stream is closed when the thread ends, and the input stream is left open, so there is nothing to do.
127      */
128     @Override
129     public void close() {
130         // nothing to close here
131     }
132 
133     @Override
134     public Throwable getException() {
135         return pollException;
136     }
137 
138     private void stopPolling() {
139         synchronized (lock) {
140             stop = true;
141             lock.notifyAll();
142         }
143     }
144 }