@Override public void load(Module br) { Section section = br.findSection(Section.KERNEL_WAKELOCKS); if (section == null) { br.printErr(3, TAG + "Section not found: " + Section.KERNEL_WAKELOCKS + " (aborting plugin)"); return; } String line = section.getLine(0); String columns[] = line.split("\t"); boolean ok = true; if (columns.length < COLUMNS.length) { ok = false; } for (int i = 0; i < COLUMNS.length; i++) { if (!COLUMNS[i].equals(columns[i])) { ok = false; } } if (!ok) { br.printErr( 3, TAG + "Data format changed in section " + Section.KERNEL_WAKELOCKS + " (aborting plugin)"); return; } int cnt = section.getLineCount(); for (int i = 1; i < cnt; i++) { line = section.getLine(i); columns = line.split("\t"); if (columns.length < COLUMNS.length) { continue; } KernelWakelock lock = new KernelWakelock(); lock.name = columns[0]; if (lock.name.startsWith("\"") && lock.name.endsWith("\"")) { lock.name = lock.name.substring(1, lock.name.length() - 1); } lock.count = Long.parseLong(columns[1]); lock.expire_count = Long.parseLong(columns[2]); lock.wake_count = Long.parseLong(columns[3]); lock.active_since = Long.parseLong(columns[4]); lock.total_time = Long.parseLong(columns[5]); lock.sleep_time = Long.parseLong(columns[6]); lock.max_time = Long.parseLong(columns[7]); lock.last_change = Long.parseLong(columns[8]); mLocks.add(lock); } mLoaded = true; }
/** * Return a SaveFile object which can be used to save user created content. This will return * always the same instance. */ public SaveFile getSaveFile() { if (mSaveFile != null) return mSaveFile; // Don't try again if (mSaveFileFailed) return null; try { String fn = mDoc.getFileName() + ".dat"; mSaveFile = new SaveFile(fn); } catch (Throwable t) { printErr(2, "Cannot create save file: " + t); mSaveFileFailed = true; } return mSaveFile; }
public void setCSVOutput(Module br, String csv) { if (csv == null) return; String fn = br.getRelRawDir() + csv + ".csv"; try { mCsvF = new FileOutputStream(br.getBaseDir() + fn); mCsvOut = new PrintStream(mCsvF); new Hint(this).add("A CSV format version is saved as: ").add(new Link(fn, fn)); } catch (IOException e) { br.printErr(4, "Failed creating CSV file `" + fn + "': " + e); mCsvF = null; mCsvOut = null; } }
private int readFile(Section sl, String fileName, InputStream is, int limit) { int ret = READ_ALL; try { if (is == null) { // Check file size (only if not reading from stream) File f = new File(fileName); long size = f.length(); is = new FileInputStream(f); if (size > limit) { // Need to seek to "end - limit" Util.skip(is, size - limit); Util.skipToEol(is); printErr( 1, "File '" + fileName + "' is too long, loading only last " + (limit / Util.MB) + " megabyte(s)..."); ret = READ_PARTS; } } LineReader br = new LineReader(is); String line = null; while (null != (line = br.readLine())) { sl.addLine(line); } br.close(); is.close(); return ret; } catch (IOException e) { printErr(1, "Error reading file '" + fileName + "' (it will be ignored): " + e); return READ_FAILED; } }
private void copyRes(String fni, String fno) throws IOException { InputStream is = getClass().getResourceAsStream(fni); if (is == null) { printErr(2, "Cannot find resource: " + fni); return; } File f = new File(mDoc.getOutDir() + fno); f.getParentFile().mkdirs(); FileOutputStream fo = new FileOutputStream(f); byte buff[] = new byte[1024]; while (true) { int read = is.read(buff); if (read <= 0) break; fo.write(buff, 0, read); } fo.close(); is.close(); }
/** * Return a new connection to the SQL database. This will return always the same instance, so if * you close it, you're doomed. If connection failed, null will be returned (which can happen if * the jdbc libraries are not found) * * @return A connection to the database or null. */ public Connection getSQLConnection() { if (mSQLConnection != null) return mSQLConnection; // Don't try again if (mSQLFailed) return null; try { Class.forName("org.sqlite.JDBC"); String fnBase = "raw/report.db"; String fn = mDoc.getOutDir() + fnBase; File f = new File(fn); f.delete(); // We must create a new database every time mSQLConnection = DriverManager.getConnection("jdbc:sqlite:" + fn); if (mSQLConnection != null) { mSQLConnection.setAutoCommit(false); addHeaderLine("Note: SQLite report database created as " + fn); } } catch (Throwable t) { printErr(2, "Cannot make DB connection: " + t); mSQLFailed = true; } return mSQLConnection; }