List of usage examples for java.io RandomAccessFile readLine
public final String readLine() throws IOException
From source file:gate.util.reporting.DocTimeReporter.java
/** * Provides the functionality to separate out pipeline specific benchmark * entries in separate temporary benchmark files in a temporary folder in the * current working directory.//w w w . j ava 2 s . co m * * @param benchmarkFile * An object of type File representing the input benchmark file. * @param report * A file handle to the report file to be written. * @throws BenchmarkReportFileAccessException * if any error occurs while accessing the input benchmark file or * while splitting it. * @throws BenchmarkReportExecutionException * if the given input benchmark file is modified while generating * the report. */ private void splitBenchmarkFile(File benchmarkFile, File report) throws BenchmarkReportFileAccessException, BenchmarkReportInputFileFormatException { File dir = temporaryDirectory; // Folder already exists; then delete all files in the temporary folder if (dir.isDirectory()) { File files[] = dir.listFiles(); for (int count = 0; count < files.length; count++) { if (!files[count].delete()) { throw new BenchmarkReportFileAccessException( "Could not delete files in the folder \"" + temporaryDirectory + "\""); } } } else if (!dir.mkdir()) { throw new BenchmarkReportFileAccessException( "Could not create temporary folder \"" + temporaryDirectory + "\""); } // delete report2 from the filesystem if (getPrintMedia().equalsIgnoreCase(MEDIA_TEXT)) { deleteFile(new File(report.getAbsolutePath() + ".txt")); } else if (getPrintMedia().equalsIgnoreCase(MEDIA_HTML)) { deleteFile(new File(report.getAbsolutePath() + ".html")); } RandomAccessFile in = null; BufferedWriter out = null; try { String logEntry = ""; long fromPos = 0; // File benchmarkFileName; if (getLogicalStart() != null) { fromPos = tail(benchmarkFile, FILE_CHUNK_SIZE); } in = new RandomAccessFile(benchmarkFile, "r"); if (getLogicalStart() != null) { in.seek(fromPos); } ArrayList<String> startTokens = new ArrayList<String>(); String lastStart = ""; Pattern pattern = Pattern.compile("(\\d+) (\\d+) (.*) (.*) \\{(.*)\\}"); Matcher matcher = null; File benchmarkFileName = null; while ((logEntry = in.readLine()) != null) { matcher = pattern.matcher(logEntry); String startToken = ""; if (logEntry.matches(".*START.*")) { String[] splittedStartEntry = logEntry.split("\\s"); if (splittedStartEntry.length > 2) { startToken = splittedStartEntry[2]; } else { throw new BenchmarkReportInputFileFormatException(getBenchmarkFile() + " is invalid."); } if (startToken.endsWith("Start")) { continue; } if (!startTokens.contains(startToken)) { // create a new file for the new pipeline startTokens.add(startToken); benchmarkFileName = new File(temporaryDirectory, startToken + "_benchmark.txt"); if (!benchmarkFileName.createNewFile()) { throw new BenchmarkReportFileAccessException("Could not create \"" + startToken + "_benchmark.txt" + "\" in directory named \"" + temporaryDirectory + "\""); } out = new BufferedWriter(new FileWriter(benchmarkFileName)); out.write(logEntry); out.newLine(); } } // if a valid benchmark entry then write it to the pipeline specific // file if (matcher != null && matcher.matches() && (validateLogEntry(matcher.group(3), startTokens) || logEntry.matches(".*documentLoaded.*"))) { startToken = matcher.group(3).split("\\.")[0]; if (!(lastStart.equals(startToken))) { if (out != null) { out.close(); } benchmarkFileName = new File(temporaryDirectory, startToken + "_benchmark.txt"); out = new BufferedWriter(new FileWriter(benchmarkFileName, true)); } if (out != null) { out.write(logEntry); out.newLine(); } lastStart = startToken; } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } }
From source file:it.drwolf.ridire.session.CrawlerManager.java
private long getURICount(Job job, String whichCount, User currentUser) throws IOException, HeritrixException, DocumentException, XPathExpressionException, SAXException { // this.updateJobsList(currentUser); Pattern pURICount = Pattern.compile( "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)", Pattern.MULTILINE); String jobName = job.getName(); Job j = this.getPersistedJob(jobName); if (j == null) { return 0L; }/*from w ww. j a va2 s. c o m*/ if (job.getChildJobName() != null && job.getChildJobName().length() > 0) { jobName = job.getChildJobName(); } String dir = this.entityManager.find(Parameter.class, Parameter.JOBS_DIR.getKey()).getValue(); long uriCountFromCrawlReport = 0L; long queuedURICount = 0L; long discoveredURICount = 0L; HttpMethod method = null; String jobStatus = this.getJobStatus(jobName); // jobName = jobName.replaceAll(" ", "\\\\ "); try { while (true) { if (jobStatus.equals(CrawlStatus.RUNNING.toString())) { RandomAccessFile progressStatistics = null; try { progressStatistics = new RandomAccessFile(this.jobsDir + CrawlerManager.FILE_SEPARATOR + jobName + CrawlerManager.FILE_SEPARATOR + "logs" + CrawlerManager.FILE_SEPARATOR + "progress-statistics.log", "r"); if (progressStatistics != null) { progressStatistics.seek(Math.max(0, progressStatistics.length() - 3000)); String line = progressStatistics.readLine(); StringBuffer buffer = new StringBuffer(); while (line != null) { buffer.append(line + "\n"); line = progressStatistics.readLine(); } String progressStatisticsContent = buffer.toString(); Matcher m = pURICount.matcher(progressStatisticsContent); int start = 0; long queuedURICountTemp = 0L; long discoveredURICountTemp = 0L; long uriCountFromCrawlReportTemp = 0L; while (m.find(start)) { start = m.end(); queuedURICountTemp = Long.parseLong(m.group(2)); discoveredURICountTemp = Long.parseLong(m.group(1)); uriCountFromCrawlReportTemp = Long.parseLong(m.group(3)); } queuedURICount += queuedURICountTemp; discoveredURICount = discoveredURICountTemp; uriCountFromCrawlReport = uriCountFromCrawlReportTemp; } } catch (FileNotFoundException e) { // TODO: handle exception } finally { if (progressStatistics != null) { progressStatistics.close(); } } break; } else if (whichCount.equalsIgnoreCase("finishedURICount")) { File reportFile = new File( dir + CrawlerManager.FILE_SEPARATOR + jobName + CrawlerManager.FILE_SEPARATOR + "reports" + CrawlerManager.FILE_SEPARATOR + "crawl-report.txt"); if (reportFile.exists() && reportFile.canRead()) { String content = FileUtils.readFileToString(reportFile); Matcher m = CrawlerManager.pFinishedURICount.matcher(content); if (m.find()) { String bytes = m.group(1); uriCountFromCrawlReport += Long.parseLong(bytes); } } Matcher m = CrawlerManager.childJobPattern.matcher(jobName); if (m.matches()) { Integer count = Integer.parseInt(m.group(1)); if (count > 1) { count--; jobName = jobName.substring(0, jobName.indexOf("__")) + "__" + count; } else if (count == 1) { jobName = jobName.substring(0, jobName.indexOf("__")); } else { break; } } else { break; } } else { return 0L; } } } finally { if (method != null) { method.releaseConnection(); } } if (whichCount.equals("discoveredUriCount")) { return discoveredURICount; } if (whichCount.equals("queuedUriCount")) { return queuedURICount; } return uriCountFromCrawlReport; }
From source file:com.mozilla.SUTAgentAndroid.service.DoCommand.java
public long GetMemoryUsage() { String load = ""; try {//from w w w . ja v a2 s. c om RandomAccessFile reader = new RandomAccessFile("/proc/meminfo", "r"); load = reader.readLine(); // Read in the MemTotal load = reader.readLine(); // Read in the MemFree } catch (IOException ex) { return (0); } String[] toks = load.split(" "); int i = 1; for (i = 1; i < toks.length; i++) { String val = toks[i].trim(); if (!val.equals("")) { break; } } if (i <= toks.length) { long lMem = Long.parseLong(toks[i].trim()); return (lMem * 1024); } return (0); }