Debian Codegraph @

common/src/main/java/com/android/utils/GrabProcessOutput.java

raw
1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.utils;
18
19 import com.android.annotations.NonNull;
20 import com.android.annotations.Nullable;
21 import com.google.common.io.Closeables;
22
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27
28 public class GrabProcessOutput {
29
30 public enum Wait {
31 /**
32 * Doesn't wait for the exec to complete.
33 * This still monitors the output but does not wait for the process to finish.
34 * In this mode the process return code is unknown and always 0.
35 */
36 ASYNC,
37 /**
38 * This waits for the process to finish.
39 * In this mode, {@link GrabProcessOutput#grabProcessOutput} returns the
40 * error code from the process.
41 * <p>
42 * In some rare cases and depending on the OS, the process might not have
43 * finished dumping data into stdout/stderr.
44 * </p>
45 * Use this when you don't particularly care for the output but instead
46 * care for the return code of the executed process.
47 */
48 WAIT_FOR_PROCESS,
49 /**
50 * <p>
51 * This waits for the process to finish <em>and</em> for the stdout/stderr
52 * threads to complete.
53 * In this mode, {@link GrabProcessOutput#grabProcessOutput} returns the
54 * error code from the process.
55 * </p>
56 * Use this one when capturing all the output from the process is important.
57 */
58 WAIT_FOR_READERS,
59 }
60
61 public interface IProcessOutput {
62 /**
63 * Processes an stdout message line.
64 * @param line The stdout message line. Null when the reader reached the end of stdout.
65 */
66 void out(@Nullable String line);
67 /**
68 * Processes an stderr message line.
69 * @param line The stderr message line. Null when the reader reached the end of stderr.
70 */
71 void err(@Nullable String line);
72 }
73
74 /**
75 * Get the stderr/stdout outputs of a process and return when the process is done.
76 * Both <b>must</b> be read or the process will block on windows.
77 *
78 * @param process The process to get the output from.
79 * @param output Optional object to capture stdout/stderr.
80 * Note that on Windows capturing the output is not optional. If output is null
81 * the stdout/stderr will be captured and discarded.
82 * @param waitMode Whether to wait for the process and/or the readers to finish.
83 * @return the process return code.
84 * @throws InterruptedException if {@link Process#waitFor()} was interrupted.
85 */
86 public static int grabProcessOutput(
87 @NonNull final Process process,
88 Wait waitMode,
89 @Nullable final IProcessOutput output) throws InterruptedException {
90 // read the lines as they come. if null is returned, it's
91 // because the process finished
92 Thread threadErr = new Thread("stderr") {
93 @Override
94 public void run() {
95 // create a buffer to read the stderr output
96 InputStream is = process.getErrorStream();
97 InputStreamReader isr = new InputStreamReader(is);
98 BufferedReader errReader = new BufferedReader(isr);
99
100 try {
101 while (true) {
102 String line = errReader.readLine();
103 if (output != null) {
104 output.err(line);
105 }
106 if (line == null) {
107 break;
108 }
109 }
110 } catch (IOException e) {
111 // do nothing.
112 } finally {
113 try {
114 Closeables.close(is, true /* swallowIOException */);
115 } catch (IOException e) {
116 // cannot happen
117 }
118 try {
119 Closeables.close(isr, true /* swallowIOException */);
120 } catch (IOException e) {
121 // cannot happen
122 }
123 try {
124 Closeables.close(errReader, true /* swallowIOException */);
125 } catch (IOException e) {
126 // cannot happen
127 }
128 }
129 }
130 };
131
132 Thread threadOut = new Thread("stdout") {
133 @Override
134 public void run() {
135 InputStream is = process.getInputStream();
136 InputStreamReader isr = new InputStreamReader(is);
137 BufferedReader outReader = new BufferedReader(isr);
138
139 try {
140 while (true) {
141 String line = outReader.readLine();
142 if (output != null) {
143 output.out(line);
144 }
145 if (line == null) {
146 break;
147 }
148 }
149 } catch (IOException e) {
150 // do nothing.
151 } finally {
152 try {
153 Closeables.close(is, true /* swallowIOException */);
154 } catch (IOException e) {
155 // cannot happen
156 }
157 try {
158 Closeables.close(isr, true /* swallowIOException */);
159 } catch (IOException e) {
160 // cannot happen
161 }
162 try {
163 Closeables.close(outReader, true /* swallowIOException */);
164 } catch (IOException e) {
165 // cannot happen
166 }
167 }
168 }
169 };
170
171 threadErr.start();
172 threadOut.start();
173
174 if (waitMode == Wait.ASYNC) {
175 return 0;
176 }
177
178 // it looks like on windows process#waitFor() can return
179 // before the thread have filled the arrays, so we wait for both threads and the
180 // process itself.
181 if (waitMode == Wait.WAIT_FOR_READERS) {
182 try {
183 threadErr.join();
184 } catch (InterruptedException e) {
185 }
186 try {
187 threadOut.join();
188 } catch (InterruptedException e) {
189 }
190 }
191
192 // get the return code from the process
193 return process.waitFor();
194 }
195 }