File Source: KDialogProcess.java
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.addons.dcc.kde;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.util.ArrayList;
28
29 /**
30 * Hold a Process and stream readers for a KDialog Process
31 */
32 public class KDialogProcess {
33 /** Is kdialog in /bin? */
/*
P/P * Method: com.dmdirc.addons.dcc.kde.KDialogProcess__static_init
*
* Postconditions:
* init'ed(hasKDialog)
* init'ed(isBin)
*/
34 private final static boolean isBin = (new File("/bin/kdialog")).exists();
35
36 /** Does KDialog exist? */
37 private final static boolean hasKDialog = (new File("/usr/bin/kdialog")).exists() || isBin;
38
39 /** Stream for the stdout stream for this process */
40 private final StreamReader stdOutputStream;
41
42 /** Stream for the stderr stream for this process */
43 private final StreamReader stdErrorStream;
44
45 /** The actual process for this process */
46 private final Process process;
47
48 /**
49 * Execute kdialog with the Parameters in params
50 *
51 * @param params Parameters to pass to kdialog
52 */
/*
P/P * Method: void com.dmdirc.addons.dcc.kde.KDialogProcess(String[])
*
* Preconditions:
* params != null
*
* Presumptions:
* java.lang.Runtime:exec(...)@57 != null
* java.lang.Runtime:getRuntime(...)@57 != null
*
* Postconditions:
* this.process != null
* this.stdErrorStream == &new StreamReader(KDialogProcess#4)
* this.stdOutputStream == &new StreamReader(KDialogProcess#2)
* new ArrayList(KDialogProcess#3) num objects == 1
* new ArrayList(KDialogProcess#5) num objects == 1
* new StreamReader(KDialogProcess#2) num objects == 1
* new StreamReader(KDialogProcess#4) num objects == 1
* this.stdOutputStream.list == &new ArrayList(KDialogProcess#3)
* init'ed(this.stdOutputStream.prefix)
* init'ed(this.stdOutputStream.stream)
* ...
*/
53 public KDialogProcess(final String[] params) throws IOException {
54 final String[] exec = new String[params.length+1];
55 System.arraycopy(params, 0, exec, 1, params.length);
56 exec[0] = (isBin) ? "/bin/kdialog" : "/usr/bin/kdialog";
57 process = Runtime.getRuntime().exec(exec);
58 stdOutputStream = new StreamReader(process.getInputStream(), new ArrayList<String>());
59 stdErrorStream = new StreamReader(process.getErrorStream(), new ArrayList<String>());
60 stdOutputStream.start();
61 stdErrorStream.start();
62 }
63
64 /**
65 * Does this system have kdialog?
66 *
67 * @return True if kdialog was found, else false
68 */
69 public static boolean hasKDialog() {
/*
P/P * Method: bool hasKDialog()
*
* Postconditions:
* init'ed(return_value)
*/
70 return hasKDialog;
71 }
72
73 /**
74 * Get the process object for this KDialogProcess
75 *
76 * @return The process object for this KDialogProcess
77 */
78 public Process getProcess() {
/*
P/P * Method: Process getProcess()
*
* Postconditions:
* return_value == this.process
* init'ed(return_value)
*/
79 return process;
80 }
81
82 /**
83 * Get the StreamReader for this KDialogProcess's stdout stream
84 *
85 * @return The StreamReader for this KDialogProcess's stdout stream
86 */
87 public StreamReader getStdOutStream() {
/*
P/P * Method: StreamReader getStdOutStream()
*
* Postconditions:
* return_value == this.stdOutputStream
* init'ed(return_value)
*/
88 return stdOutputStream;
89 }
90
91 /**
92 * Get the StreamReader for this KDialogProcess's stderr stream
93 *
94 * @return The StreamReader for this KDialogProcess's stderr stream
95 */
96 public StreamReader getStdErrStream() {
/*
P/P * Method: StreamReader getStdErrStream()
*
* Postconditions:
* return_value == this.stdErrorStream
* init'ed(return_value)
*/
97 return stdErrorStream;
98 }
99
100 /**
101 * Wait for the process to finish.
102 */
103 public void waitFor() throws InterruptedException {
/*
P/P * Method: void waitFor()
*
* Preconditions:
* this.process != null
*/
104 process.waitFor();
105 }
106 }
SofCheck Inspector Build Version : 2.17854
| KDialogProcess.java |
2009-Jun-25 01:54:24 |
| KDialogProcess.class |
2009-Sep-02 17:04:15 |