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 |
|
---|
26 | package org.greenstone.gatherer.util;
|
---|
27 |
|
---|
28 | import java.io.BufferedReader;
|
---|
29 | import java.io.BufferedWriter;
|
---|
30 | import java.io.File;
|
---|
31 | import java.io.InputStream;
|
---|
32 | import java.io.InputStreamReader;
|
---|
33 | import java.io.IOException;
|
---|
34 | import java.io.OutputStream;
|
---|
35 | import 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 write to the inputstream of a Process
|
---|
39 | // Process.getOutputStream()
|
---|
40 | public class OutputStreamGobbler extends Thread
|
---|
41 | {
|
---|
42 | OutputStream os = null;
|
---|
43 | String inputstr = "";
|
---|
44 |
|
---|
45 | public OutputStreamGobbler(OutputStream os, String inputstr)
|
---|
46 | {
|
---|
47 | this.os = os;
|
---|
48 | this.inputstr = inputstr;
|
---|
49 | }
|
---|
50 |
|
---|
51 | public void run()
|
---|
52 | {
|
---|
53 |
|
---|
54 | if (inputstr == null) {
|
---|
55 | return;
|
---|
56 | }
|
---|
57 |
|
---|
58 | BufferedWriter osw = null;
|
---|
59 | try {
|
---|
60 | osw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
|
---|
61 | //System.out.println("@@@ SENDING LINE: " + inputstr);
|
---|
62 | osw.write(inputstr, 0, inputstr.length());
|
---|
63 | osw.newLine();//osw.write("\n");
|
---|
64 | osw.flush();
|
---|
65 |
|
---|
66 | // Don't explicitly send EOF when using StreamGobblers as below,
|
---|
67 | // as the EOF char is echoed to output.
|
---|
68 | // Flushing the write handle and/or closing the resource seems
|
---|
69 | // to already send EOF silently.
|
---|
70 |
|
---|
71 | /*if(Utility.isWindows()) {
|
---|
72 | osw.write("\032"); // octal for Ctrl-Z, EOF on Windows
|
---|
73 | } else { // EOF on Linux/Mac is Ctrl-D
|
---|
74 | osw.write("\004"); // octal for Ctrl-D, see http://www.unix-manuals.com/refs/misc/ascii-table.html
|
---|
75 | }
|
---|
76 | osw.flush();
|
---|
77 | */
|
---|
78 | } catch (IOException ioe) {
|
---|
79 | ioe.printStackTrace();
|
---|
80 | } finally {
|
---|
81 | SafeProcess.closeResource(osw);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|