View Javadoc
1   /*
2    * Copyright (C) 2014 Cristian Sulea ( http://cristian.sulea.net )
3    * 
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    * 
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Lesser General Public License for more details.
13   * 
14   * You should have received a copy of the GNU Lesser General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  
18  package jatoo.exec;
19  
20  import java.io.BufferedReader;
21  import java.io.BufferedWriter;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.InputStreamReader;
25  import java.io.OutputStream;
26  import java.io.OutputStreamWriter;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  /**
32   * A class that will exhaust a provided {@link InputStream} but dumping the
33   * contents to a specified stream..
34   * 
35   * @author <a href="http://cristian.sulea.net" rel="author">Cristian Sulea</a>
36   * @version 1.6, July 25, 2014
37   */
38  public class InputStreamExhausterWithDumpStream implements Runnable {
39  
40    private static final Log LOGGER = LogFactory.getLog(InputStreamExhausterWithDumpStream.class);
41  
42    private final InputStream processInputStream;
43    private final OutputStream dumpOutputStream;
44    private final boolean closeDumpOutputStream;
45  
46    public InputStreamExhausterWithDumpStream(final InputStream processInputStream, final OutputStream dumpOutputStream, final boolean closeDumpOutputStream) {
47      this.processInputStream = processInputStream;
48      this.dumpOutputStream = dumpOutputStream;
49      this.closeDumpOutputStream = closeDumpOutputStream;
50    }
51  
52    public final void exhaust() {
53  
54      BufferedReader processInputStreamReader = null;
55      BufferedWriter dumpOutputStreamWriter = null;
56  
57      try {
58  
59        processInputStreamReader = new BufferedReader(new InputStreamReader(processInputStream));
60        dumpOutputStreamWriter = new BufferedWriter(new OutputStreamWriter(dumpOutputStream));
61  
62        String line = null;
63        while ((line = processInputStreamReader.readLine()) != null) {
64  
65          dumpOutputStreamWriter.write(line);
66          dumpOutputStreamWriter.newLine();
67  
68          dumpOutputStreamWriter.flush();
69        }
70      }
71  
72      catch (IOException e) {
73        LOGGER.error("error exhausting the stream", e);
74      }
75  
76      finally {
77        if (closeDumpOutputStream && dumpOutputStreamWriter != null) {
78          try {
79            dumpOutputStreamWriter.close();
80          } catch (IOException e) {
81            LOGGER.error("error closing the dump stream", e);
82          }
83        }
84      }
85    }
86  
87    @Override
88    public final void run() {
89      exhaust();
90    }
91  
92  }