source: main/trunk/gli/src/org/greenstone/gatherer/util/InputStreamGobbler.java@ 29017

Last change on this file since 29017 was 29017, checked in by ak19, 10 years ago

Moved the StreamGobbler classes and their closeResource help method out of FormatConversionDialog.java as these are more generally useful

File size: 1.4 KB
Line 
1package org.greenstone.gatherer.util;
2
3import java.io.BufferedReader;
4import java.io.BufferedWriter;
5import java.io.File;
6import java.io.InputStream;
7import java.io.InputStreamReader;
8import java.io.IOException;
9import java.io.OutputStream;
10import 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
14public 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}
Note: See TracBrowser for help on using the repository browser.