1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package org.objectledge.external;
30
31 import java.io.BufferedInputStream;
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.IOException;
35 import java.io.UnsupportedEncodingException;
36
37 import org.objectledge.utils.StringUtils;
38
39 /***
40 * Represents the result of script execution.
41 *
42 * @author <a href="rafal@caltha.pl">RafaĆ Krzewski</a>
43 * @version $Id: ExecutionResult.java,v 1.1 2006/03/24 14:27:37 rafal Exp $
44 */
45 public class ExecutionResult
46 {
47 private static final int BUFFER_SIZE = 65536;
48 private final int exitValue;
49 private final byte[] output;
50 private final byte[] error;
51
52 /***
53 * Creates a new ExecutionResult instance.
54 *
55 * @param exitValue the exit value of the process.
56 * @param captured output stream the process, or null if not available.
57 * @param captured error stream the process, or null if not available.
58 */
59 public ExecutionResult(int exitValue, byte[] output, byte[] error)
60 {
61 this.exitValue = exitValue;
62 this.output = output;
63 this.error = error;
64 }
65
66 /***
67 * Creates a new ExecutionResult instance.
68 *
69 * @param exitValue process exit value.
70 */
71 public ExecutionResult(int exitValue)
72 {
73 this.exitValue = exitValue;
74 this.output = null;
75 this.error = null;
76 }
77
78 /***
79 * Returns the process exit value.
80 *
81 * @return the process exit value.
82 */
83 public int getExitValue()
84 {
85 return exitValue;
86 }
87
88 /***
89 * Returns the data captured from process output stream as bytes.
90 *
91 * @return the data captured from process output stream as bytes, or null if not available.
92 */
93 public byte[] getOutputBytes()
94 {
95 return output;
96 }
97
98 /***
99 * Returns the data captured from process output stream as a string.
100 *
101 * @param charset output stream encoding.
102 * @return the data captured from process output stream as a string, or null if not available.
103 * @throws UnsupportedEncodingException if the encoding is not supported by the VM.
104 */
105 public String getOutput(String charset) throws UnsupportedEncodingException
106 {
107 if(output == null)
108 {
109 return null;
110 }
111 return new String(output, charset);
112 }
113
114 /***
115 * Returns the data captured from process output stream as an UTF-8 string.
116 *
117 * @return the data captured from process output stream as an UTF-8 string, or null if not available.
118 */
119 public String getOutput()
120 {
121 return StringUtils.fromUTF8(output);
122 }
123
124 /***
125 * Returns the data captured from process error stream as bytes.
126 *
127 * @return the data captured from process error stream as bytes, or null if not available.
128 */
129 public byte[] getErrorBytes()
130 {
131 return error;
132 }
133
134 /***
135 * Returns the data captured from process error stream as a string.
136 *
137 * @param charset output stream encoding.
138 * @return the data captured from process error stream as a string, or null if not available.
139 * @throws UnsupportedEncodingException if the encoding is not supported by the VM.
140 */
141 public String getError(String charset) throws UnsupportedEncodingException
142 {
143 if(error == null)
144 {
145 return null;
146 }
147 return new String(error, charset);
148 }
149
150 /***
151 * Returns the data captured from process error stream as an UTF-8 string.
152 *
153 * @return the data captured from process error stream as an UTF-8 string, or null if not available.
154 */
155 public String getError()
156 {
157 return StringUtils.fromUTF8(error);
158 }
159 }