[29017] | 1 | package org.greenstone.gatherer.util;
|
---|
| 2 |
|
---|
| 3 | import java.io.BufferedReader;
|
---|
| 4 | import java.io.BufferedWriter;
|
---|
| 5 | import java.io.File;
|
---|
| 6 | import java.io.InputStream;
|
---|
| 7 | import java.io.InputStreamReader;
|
---|
| 8 | import java.io.IOException;
|
---|
| 9 | import java.io.OutputStream;
|
---|
| 10 | import java.io.OutputStreamWriter;
|
---|
| 11 |
|
---|
| 12 | // http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2
|
---|
| 13 | // This class is used in FormatConversionDialog to properly read from the output and error streams of a Process
|
---|
| 14 | public class InputStreamGobbler extends Thread
|
---|
| 15 | {
|
---|
| 16 | InputStream is = null;
|
---|
| 17 | StringBuffer outputstr = new StringBuffer();
|
---|
| 18 | boolean split_newlines = false;
|
---|
| 19 |
|
---|
| 20 |
|
---|
| 21 | public InputStreamGobbler(InputStream is)
|
---|
| 22 | {
|
---|
| 23 | this.is = is;
|
---|
| 24 | split_newlines = false;
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | public InputStreamGobbler(InputStream is, boolean split_newlines)
|
---|
| 28 | {
|
---|
| 29 | this.is = is;
|
---|
| 30 | this.split_newlines = split_newlines;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | public void run()
|
---|
| 34 | {
|
---|
| 35 | BufferedReader br = null;
|
---|
| 36 | try {
|
---|
| 37 | br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
---|
| 38 | String line=null;
|
---|
| 39 | while ( (line = br.readLine()) != null) {
|
---|
| 40 | //System.out.println("@@@ GOT LINE: " + line);
|
---|
| 41 | outputstr.append(line);
|
---|
| 42 | if(split_newlines) {
|
---|
| 43 | outputstr.append("\n");
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | }
|
---|
| 47 | } catch (IOException ioe) {
|
---|
| 48 | ioe.printStackTrace();
|
---|
| 49 | } finally {
|
---|
| 50 | Utility.closeResource(br);
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public String getOutput() {
|
---|
| 55 | return outputstr.toString(); // implicit toString() call anyway. //return outputstr;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|