use of java.io.BufferedReader in project platform_frameworks_base by android.
the class KernelCpuSpeedReader method readDelta.
/**
* The returned array is modified in subsequent calls to {@link #readDelta}.
* @return The time (in milliseconds) spent at different cpu speeds since the last call to
* {@link #readDelta}.
*/
public long[] readDelta() {
try (BufferedReader reader = new BufferedReader(new FileReader(mProcFile))) {
TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(' ');
String line;
int speedIndex = 0;
while (speedIndex < mLastSpeedTimes.length && (line = reader.readLine()) != null) {
splitter.setString(line);
Long.parseLong(splitter.next());
long time = Long.parseLong(splitter.next()) * mJiffyMillis;
if (time < mLastSpeedTimes[speedIndex]) {
// The stats reset when the cpu hotplugged. That means that the time
// we read is offset from 0, so the time is the delta.
mDeltaSpeedTimes[speedIndex] = time;
} else {
mDeltaSpeedTimes[speedIndex] = time - mLastSpeedTimes[speedIndex];
}
mLastSpeedTimes[speedIndex] = time;
speedIndex++;
}
} catch (IOException e) {
Slog.e(TAG, "Failed to read cpu-freq: " + e.getMessage());
Arrays.fill(mDeltaSpeedTimes, 0);
}
return mDeltaSpeedTimes;
}
use of java.io.BufferedReader in project platform_frameworks_base by android.
the class RedirectListener method run.
@Override
public void run() {
int count = 0;
synchronized (mLock) {
mListening = true;
mLock.notifyAll();
}
boolean terminate = false;
for (; ; ) {
count++;
try (Socket instance = mServerSocket.accept()) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(instance.getInputStream(), StandardCharsets.UTF_8))) {
boolean detected = false;
StringBuilder sb = new StringBuilder();
String s;
while ((s = in.readLine()) != null) {
sb.append(s).append('\n');
if (!detected && s.startsWith("GET")) {
String[] segments = s.split(" ");
if (segments.length == 3 && segments[2].startsWith("HTTP/") && segments[1].regionMatches(1, mPath, 0, mPath.length())) {
detected = true;
}
}
if (s.length() == 0) {
break;
}
}
Log.d(TAG, "Redirect receive: " + sb);
String response = null;
if (detected) {
response = status(OSUOperationStatus.UserInputComplete);
if (response == null) {
response = GoodBye;
terminate = true;
}
}
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(instance.getOutputStream(), StandardCharsets.UTF_8))) {
out.write(HTTPResponseHeader);
if (response != null) {
out.write(response);
}
}
if (terminate) {
break;
} else if (count > MaxRetry) {
status(OSUOperationStatus.UserInputAborted);
break;
}
}
} catch (IOException ioe) {
if (mAborted) {
return;
} else if (count > MaxRetry) {
status(OSUOperationStatus.UserInputAborted);
break;
}
}
}
}
use of java.io.BufferedReader in project platform_frameworks_base by android.
the class NetworkManagementService method readRouteList.
private ArrayList<String> readRouteList(String filename) {
FileInputStream fstream = null;
ArrayList<String> list = new ArrayList<>();
try {
fstream = new FileInputStream(filename);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String s;
while (((s = br.readLine()) != null) && (s.length() != 0)) {
list.add(s);
}
} catch (IOException ex) {
// return current list, possibly empty
} finally {
if (fstream != null) {
try {
fstream.close();
} catch (IOException ex) {
}
}
}
return list;
}
use of java.io.BufferedReader in project fastjson by alibaba.
the class TestKlutz method setUp.
@Override
protected void setUp() throws Exception {
InputStreamReader isr = new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("epub.json"));
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp;
while ((temp = reader.readLine()) != null) {
sb.append(temp);
}
String s = sb.toString();
this.book = JSON.parseObject(s, EpubViewBook.class);
}
use of java.io.BufferedReader in project jstorm by alibaba.
the class ShellUtils method runCommand.
/** Run a command */
private void runCommand() throws IOException {
ProcessBuilder builder = new ProcessBuilder(getExecString());
Timer timeOutTimer = null;
ShellTimeoutTimerTask timeoutTimerTask = null;
timedOut = new AtomicBoolean(false);
completed = new AtomicBoolean(false);
if (environment != null) {
builder.environment().putAll(this.environment);
}
if (dir != null) {
builder.directory(this.dir);
}
builder.redirectErrorStream(redirectErrorStream);
process = builder.start();
if (timeOutInterval > 0) {
timeOutTimer = new Timer("Shell command timeout");
timeoutTimerTask = new ShellTimeoutTimerTask(this);
// One time scheduling.
timeOutTimer.schedule(timeoutTimerTask, timeOutInterval);
}
final BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
BufferedReader inReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
final StringBuffer errMsg = new StringBuffer();
// read error and input streams as this would free up the buffers
// free the error stream buffer
Thread errThread = new Thread() {
@Override
public void run() {
try {
String line = errReader.readLine();
while ((line != null) && !isInterrupted()) {
errMsg.append(line);
errMsg.append(System.getProperty("line.separator"));
line = errReader.readLine();
}
} catch (IOException ioe) {
LOG.warn("Error reading the error stream", ioe);
}
}
};
try {
errThread.start();
} catch (IllegalStateException ise) {
}
try {
// parse the output
parseExecResult(inReader);
// clear the input stream buffer
String line = inReader.readLine();
while (line != null) {
line = inReader.readLine();
}
// wait for the process to finish and check the exit code
exitCode = process.waitFor();
// make sure that the error thread exits
joinThread(errThread);
completed.set(true);
// taken care in finally block
if (exitCode != 0) {
throw new ExitCodeException(exitCode, errMsg.toString());
}
} catch (InterruptedException ie) {
throw new IOException(ie.toString());
} finally {
if (timeOutTimer != null) {
timeOutTimer.cancel();
}
// close the input stream
try {
// JDK 7 tries to automatically drain the input streams for us
// when the process exits, but since close is not synchronized,
// it creates a race if we close the stream first and the same
// fd is recycled. the stream draining thread will attempt to
// drain that fd!! it may block, OOM, or cause bizarre behavior
// see: https://bugs.openjdk.java.net/browse/JDK-8024521
// issue is fixed in build 7u60
InputStream stdout = process.getInputStream();
synchronized (stdout) {
inReader.close();
}
} catch (IOException ioe) {
LOG.warn("Error while closing the input stream", ioe);
}
if (!completed.get()) {
errThread.interrupt();
joinThread(errThread);
}
try {
InputStream stderr = process.getErrorStream();
synchronized (stderr) {
errReader.close();
}
} catch (IOException ioe) {
LOG.warn("Error while closing the error stream", ioe);
}
process.destroy();
lastTime = System.currentTimeMillis();
}
}
Aggregations