Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SimpleApplicationConsumer |
|
| 1.625;1.625 | ||||
SimpleApplicationConsumer$1 |
|
| 1.625;1.625 | ||||
SimpleApplicationConsumer$StringBuilderOutputStream |
|
| 1.625;1.625 |
1 | package org.jtheque.utils.io; | |
2 | ||
3 | /* | |
4 | * This file is part of JTheque. | |
5 | * | |
6 | * JTheque is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License. | |
9 | * | |
10 | * JTheque is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | import java.io.IOException; | |
20 | import java.io.InputStreamReader; | |
21 | import java.io.InterruptedIOException; | |
22 | import java.io.OutputStream; | |
23 | import java.io.PrintStream; | |
24 | import java.io.Reader; | |
25 | import java.nio.CharBuffer; | |
26 | ||
27 | /** | |
28 | * This class enable to launch an application and to redirect resultStream and err flows to a StringBuilder. | |
29 | * | |
30 | * @author adiGuba | |
31 | * @author Baptiste Wicht | |
32 | */ | |
33 | public final class SimpleApplicationConsumer { | |
34 | 0 | private final StringBuilderOutputStream resultStream = new StringBuilderOutputStream(); |
35 | ||
36 | 0 | private final PrintStream out = new PrintStream(resultStream, true); |
37 | 0 | private final PrintStream err = new PrintStream(resultStream, true); |
38 | ||
39 | private static final int BUF_SIZE = 4196; | |
40 | ||
41 | private final ProcessBuilder builder; | |
42 | ||
43 | /** | |
44 | * Construct a new SimpleApplicationConsumer to launch a simple application with some arguments. | |
45 | * | |
46 | * @param args The application followed by the arguments to pass to the application. | |
47 | */ | |
48 | public SimpleApplicationConsumer(String... args) { | |
49 | 0 | super(); |
50 | ||
51 | 0 | builder = new ProcessBuilder(args); |
52 | 0 | } |
53 | ||
54 | /** | |
55 | * Consume all the streams of the process. All the streams are redirected to | |
56 | * a simple StringBuilder. | |
57 | * | |
58 | * @throws IOException I/O error | |
59 | */ | |
60 | public void consume() throws IOException { | |
61 | 0 | final Process process = builder.start(); |
62 | ||
63 | try { | |
64 | 0 | process.getOutputStream().close(); |
65 | ||
66 | 0 | dump(new InputStreamReader(process.getErrorStream()), err); |
67 | 0 | dump(new InputStreamReader(process.getInputStream()), out); |
68 | ||
69 | try { | |
70 | 0 | process.waitFor(); |
71 | 0 | } catch (InterruptedException e) { |
72 | 0 | IOException ioe = new InterruptedIOException(); |
73 | 0 | ioe.initCause(e); |
74 | 0 | throw ioe; |
75 | 0 | } |
76 | } finally { | |
77 | 0 | process.destroy(); |
78 | 0 | } |
79 | 0 | } |
80 | ||
81 | /** | |
82 | * Dump the in stream into the resultStream stream. | |
83 | * | |
84 | * @param in input stream. | |
85 | * @param out output stream. | |
86 | * @throws IOException I/O error | |
87 | */ | |
88 | private static void dump(Reader in, PrintStream out) throws IOException { | |
89 | try { | |
90 | try { | |
91 | 0 | Thread current = Thread.currentThread(); |
92 | 0 | CharBuffer cb = CharBuffer.allocate(BUF_SIZE); |
93 | ||
94 | ||
95 | 0 | cb.clear(); |
96 | ||
97 | 0 | int len = in.read(cb); |
98 | ||
99 | 0 | while (len > 0 && !current.isInterrupted()) { |
100 | 0 | cb.position(0).limit(len); |
101 | 0 | out.append(cb); |
102 | 0 | cb.clear(); |
103 | 0 | out.flush(); |
104 | ||
105 | 0 | if (current.isInterrupted()) { |
106 | 0 | break; |
107 | } | |
108 | ||
109 | 0 | len = in.read(cb); |
110 | } | |
111 | } finally { | |
112 | 0 | FileUtils.close(in); |
113 | 0 | } |
114 | } finally { | |
115 | 0 | FileUtils.close(out); |
116 | 0 | } |
117 | 0 | } |
118 | ||
119 | /** | |
120 | * Return the result of the consumed process. | |
121 | * | |
122 | * @return The String result of the consumed process. | |
123 | */ | |
124 | public String getResult() { | |
125 | 0 | return resultStream.getResult().toString(); |
126 | } | |
127 | ||
128 | /** | |
129 | * An output stream to write to a string builder. | |
130 | * | |
131 | * @author Baptiste Wicht | |
132 | */ | |
133 | 0 | private static final class StringBuilderOutputStream extends OutputStream { |
134 | 0 | private final StringBuilder result = new StringBuilder(250); |
135 | ||
136 | @Override | |
137 | public void write(int b) throws IOException { | |
138 | 0 | result.append(String.valueOf((char) b)); |
139 | 0 | } |
140 | ||
141 | @Override | |
142 | public void write(byte[] b, int off, int len) throws IOException { | |
143 | 0 | result.append(new String(b, off, len)); |
144 | 0 | } |
145 | ||
146 | @Override | |
147 | public void write(byte[] b) throws IOException{ | |
148 | 0 | write(b, 0, b.length); |
149 | 0 | } |
150 | ||
151 | /** | |
152 | * Return the result of the stream. | |
153 | * | |
154 | * @return A StringBuilder containing all the texts of the stream. | |
155 | */ | |
156 | public StringBuilder getResult() { | |
157 | 0 | return result; |
158 | } | |
159 | } | |
160 | } |