source: main/trunk/gli/src/org/greenstone/gatherer/util/OutputStreamGobbler.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.6 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 write to the inputstream of a Process
14public class OutputStreamGobbler extends Thread
15{
16 OutputStream os = null;
17 String inputstr = "";
18
19 public OutputStreamGobbler(OutputStream os, String inputstr)
20 {
21 this.os = os;
22 this.inputstr = inputstr;
23 }
24
25 public void run()
26 {
27 BufferedWriter osw = null;
28 try {
29 osw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
30 //System.out.println("@@@ SENDING LINE: " + inputstr);
31 osw.write(inputstr, 0, inputstr.length());
32 osw.newLine();//osw.write("\n");
33 osw.flush();
34
35 // Don't explicitly send EOF when using StreamGobblers as below,
36 // as the EOF char is echoed to output.
37 // Flushing the write handle and/or closing the resource seems
38 // to already send EOF silently.
39
40 /*if(Utility.isWindows()) {
41 osw.write("\032"); // octal for Ctrl-Z, EOF on Windows
42 } else { // EOF on Linux/Mac is Ctrl-D
43 osw.write("\004"); // octal for Ctrl-D, see http://www.unix-manuals.com/refs/misc/ascii-table.html
44 }
45 osw.flush();
46 */
47 } catch (IOException ioe) {
48 ioe.printStackTrace();
49 } finally {
50 Utility.closeResource(osw);
51 }
52 }
53}
Note: See TracBrowser for help on using the repository browser.