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

Last change on this file since 31581 was 31581, checked in by ak19, 7 years ago

Minor changes ahead of bigger commits

File size: 2.6 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Copyright (C) 1999 New Zealand Digital Library Project
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *########################################################################
24 */
25
26package org.greenstone.gatherer.util;
27
28import java.io.BufferedReader;
29import java.io.BufferedWriter;
30import java.io.File;
31import java.io.InputStream;
32import java.io.InputStreamReader;
33import java.io.IOException;
34import java.io.OutputStream;
35import java.io.OutputStreamWriter;
36
37// http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2
38// This class is used in FormatConversionDialog to properly read from the stdout and stderr
39// streams of a Process, Process.getInputStream() and Process.getErrorSream()
40public class InputStreamGobbler extends Thread
41{
42 InputStream is = null;
43 StringBuffer outputstr = new StringBuffer();
44 boolean split_newlines = false;
45
46
47 public InputStreamGobbler(InputStream is)
48 {
49 this.is = is;
50 split_newlines = false;
51 }
52
53 public InputStreamGobbler(InputStream is, boolean split_newlines)
54 {
55 this.is = is;
56 this.split_newlines = split_newlines;
57 }
58
59 public void run()
60 {
61 BufferedReader br = null;
62 try {
63 br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
64 String line=null;
65 while ( (line = br.readLine()) != null) {
66 //System.out.println("@@@ GOT LINE: " + line);
67 outputstr.append(line);
68 if(split_newlines) {
69 outputstr.append("\n");
70 }
71
72 }
73 } catch (IOException ioe) {
74 ioe.printStackTrace();
75 } finally {
76 SafeProcess.closeResource(br);
77 }
78 }
79
80 public String getOutput() {
81 return outputstr.toString(); // implicit toString() call anyway. //return outputstr;
82 }
83}
Note: See TracBrowser for help on using the repository browser.