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 write to the inputstream of a Process
|
---|
14 | public 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 | }
|
---|