If you think the Android project Pimp_my_Z1 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Performance Control - An Android CPU Control application Copyright (C) 2012
* James Roberts//www.java2s.com
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/package com.androguide.honamicontrol.helpers.CMDProcessor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
importstatic java.lang.System.nanoTime;
publicclass ChildProcess {
private String TAG = getClass().getSimpleName();
privatestaticfinalint PIPE_SIZE = 1024;
privateclass ChildReader extends Thread {
InputStream mStream;
StringBuffer mBuffer;
ChildReader(InputStream is, StringBuffer buf) {
mStream = is;
mBuffer = buf;
}
publicvoid run() {
byte[] buf = newbyte[PIPE_SIZE];
try {
int len;
while ((len = mStream.read(buf)) != -1) {
String s = new String(buf, 0, len);
mBuffer.append(s);
}
} catch (IOException e) {
// Ignore
}
try {
mStream.close();
} catch (IOException e) {
// Ignore
}
}
}
privateclass ChildWriter extends Thread {
OutputStream mStream;
String mBuffer;
ChildWriter(OutputStream os, String buf) {
mStream = os;
mBuffer = buf;
}
publicvoid run() {
int off = 0;
byte[] buf = mBuffer.getBytes();
try {
while (off < buf.length) {
int len = Math.min(PIPE_SIZE, buf.length - off);
mStream.write(buf, off, len);
off += len;
}
} catch (IOException e) {
// Ignore
}
try {
mStream.close();
} catch (IOException e) {
// Ignore
}
}
}
privatelong mStartTime;
private Process mChildProc;
private ChildWriter mChildStdinWriter;
private ChildReader mChildStdoutReader;
private ChildReader mChildStderrReader;
private StringBuffer mChildStdout;
private StringBuffer mChildStderr;
privateint mExitValue;
privatelong mEndTime;
public ChildProcess(String[] cmdarray, String childStdin) {
mStartTime = nanoTime();
try {
mChildProc = Runtime.getRuntime().exec(cmdarray);
if (childStdin != null) {
mChildStdinWriter = new ChildWriter(mChildProc.getOutputStream(), childStdin);
mChildStdinWriter.start();
}
mChildStdout = new StringBuffer();
mChildStdoutReader = new ChildReader(mChildProc.getInputStream(), mChildStdout);
mChildStdoutReader.start();
mChildStderr = new StringBuffer();
mChildStderrReader = new ChildReader(mChildProc.getErrorStream(), mChildStderr);
mChildStderrReader.start();
} catch (IOException e) {
// XXX: log
}
}
publicboolean isFinished() {
boolean finished = true;
if (mChildProc != null) {
try {
mChildProc.exitValue();
} catch (IllegalStateException e) {
finished = false;
}
}
return finished;
}
publicint waitFinished() {
while (mChildProc != null) {
try {
mExitValue = mChildProc.waitFor();
mEndTime = nanoTime();
mChildProc = null;
mChildStderrReader.join();
mChildStderrReader = null;
mChildStdoutReader.join();
mChildStdoutReader = null;
if (mChildStdinWriter != null) {
mChildStdinWriter.join();
mChildStdinWriter = null;
}
} catch (InterruptedException e) {
// Ignore
}
}
return mExitValue;
}
public CommandResult getResult() {
if (!isFinished()) {
thrownew IllegalThreadStateException("Child process running");
}
returnnew CommandResult(
mStartTime,
mExitValue,
mChildStdout.toString(),
mChildStderr.toString(),
mEndTime);
}
}