List of usage examples for org.apache.commons.io LineIterator close
public void close()
Reader
quietly. From source file:org.mskcc.cbio.portal.util.IGVLinking.java
private static String getFileContents(File file) throws Exception { StringBuilder sb = new StringBuilder(); LineIterator it = null; try {//from w w w . j av a 2s . c o m it = FileUtils.lineIterator(file, "UTF-8"); while (it.hasNext()) { sb.append(it.nextLine()); } } finally { if (it != null) it.close(); } return sb.toString(); }
From source file:org.openmrs.module.emrmonitor.metric.ConfigurableMetricProducer.java
/** * If multiple lines of output are returned and each is in the format of key=value, then the key will be considered part of the metric, and the value the value * Otherwise, the full contents of output will be the value of a single metric *///from w w w . ja v a 2s . co m protected void handleShellScript(Map<String, String> metrics, String namespace, File f) throws IOException { Process process = Runtime.getRuntime().exec(f.getAbsolutePath()); StringBuilder singleValueMetric = new StringBuilder(); Map<String, String> keyValueMetrics = new LinkedHashMap<String, String>(); LineIterator successIterator = null; try { successIterator = IOUtils.lineIterator(process.getInputStream(), "UTF-8"); while (successIterator.hasNext()) { String line = successIterator.nextLine(); String[] split = StringUtils.splitByWholeSeparatorPreserveAllTokens(line, "=", 2); if (split.length == 2) { keyValueMetrics.put(namespace + "." + split[0], split[1]); } else { singleValueMetric.append(line).append(System.getProperty("line.separator")); } } if (singleValueMetric.length() > 0) { metrics.put(namespace, singleValueMetric.toString()); } else { metrics.putAll(keyValueMetrics); } } finally { successIterator.close(); } StringBuilder error = new StringBuilder(); LineIterator errorIterator = null; try { errorIterator = IOUtils.lineIterator(process.getErrorStream(), "UTF-8"); while (errorIterator.hasNext()) { String line = errorIterator.nextLine(); error.append(System.getProperty("line.separator")).append(line); } } finally { errorIterator.close(); } if (error.length() > 0) { throw new RuntimeException( "An error occurred while executing shell script " + f.getName() + ": " + error); } }
From source file:org.openmrs.module.pihmalawi.sql.MysqlRunner.java
/** * Executes a Sql Script/*from ww w .j a va 2s .c o m*/ */ public static MysqlResult executeSql(String sql, Map<String, Object> parameterValues) { log.info("Executing SQL..."); File toExecute = null; try { // Writing SQL to temporary file for execution toExecute = File.createTempFile("mysqlrunner", ".sql"); StringBuilder sqlToWrite = new StringBuilder(); if (parameterValues != null) { for (String paramName : parameterValues.keySet()) { Object paramValue = parameterValues.get(paramName); sqlToWrite.append("set @").append(paramName); sqlToWrite.append("=").append(getParameterAssignmentString(paramValue)).append(";"); sqlToWrite.append(System.getProperty("line.separator")); } } sqlToWrite.append(sql); FileUtils.writeStringToFile(toExecute, sqlToWrite.toString()); log.debug("Wrote SQL file for execution: " + toExecute.getAbsolutePath()); log.debug("Contents:\n" + sqlToWrite); // Constructing command line elements to execute List<String> commands = new ArrayList<String>(); commands.add("mysql"); commands.add("-u" + Context.getRuntimeProperties().getProperty("connection.username")); commands.add("-p" + Context.getRuntimeProperties().getProperty("connection.password")); commands.add("-esource " + toExecute.getAbsolutePath()); commands.add(DatabaseUpdater.getConnection().getCatalog()); // Database Name log.debug("Constructed command to execute: \n" + OpenmrsUtil.join(commands, " ")); Process process = Runtime.getRuntime().exec(commands.toArray(new String[] {})); MysqlResult result = new MysqlResult(); LineIterator successIterator = null; try { successIterator = IOUtils.lineIterator(process.getInputStream(), "UTF-8"); while (successIterator.hasNext()) { String line = successIterator.nextLine(); String[] elements = StringUtils.splitPreserveAllTokens(line, '\t'); if (result.getColumns().isEmpty()) { result.setColumns(Arrays.asList(elements)); } else { Map<String, String> row = new LinkedHashMap<String, String>(); for (int i = 0; i < result.getColumns().size(); i++) { String value = elements[i].trim(); if ("NULL".equals(value)) { value = null; } row.put(result.getColumns().get(i), value); } result.getData().add(row); } } } finally { successIterator.close(); } LineIterator errorIterator = null; try { errorIterator = IOUtils.lineIterator(process.getErrorStream(), "UTF-8"); while (errorIterator.hasNext()) { String line = errorIterator.nextLine(); if (!line.toLowerCase().startsWith("warning")) { result.getErrors().add(line); } } } finally { errorIterator.close(); } return result; } catch (Exception e) { throw new RuntimeException("An error occurred while executing a SQL file", e); } finally { FileUtils.deleteQuietly(toExecute); } }
From source file:org.opensextant.examples.RegexTest.java
/** * The main method.// w ww . ja v a 2 s. c o m * * @param args * the arguments */ public static void main(String[] args) { // file containing the tab separated test data String testFileName = args[0]; // the file containing the regex definintions String patternFileName = args[1]; // file into which we will write the results String resultFileName = args[2]; File testFile = new File(testFileName); URL patternFile = null; BufferedWriter resWriter = null; // setup the output File resFile = new File(resultFileName); try { resWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(resFile), "UTF-8")); } catch (UnsupportedEncodingException e) { LOGGER.error("Couldnt write to " + resFile.getName() + ":" + e.getMessage(), e); return; } catch (FileNotFoundException e) { LOGGER.error("Couldnt write to " + resFile.getName() + ":" + e.getMessage(), e); return; } // write the results header try { resWriter.write( "Entity Type\tPos\\Neg\tTest Input\tScore\tComplete\tCount\tTypes Found\tRules Matched\tAnnotations Found"); resWriter.newLine(); } catch (IOException e) { LOGGER.error("Couldnt write to " + resFile.getName() + ":" + e.getMessage(), e); } // get the pattern file as a URL try { patternFile = new File(patternFileName).toURI().toURL(); } catch (MalformedURLException e) { LOGGER.error("Couldn't use pattern file " + patternFileName + ":" + e.getMessage(), e); } // initialize the regex matcher RegexMatcher reger = new RegexMatcher(patternFile); LOGGER.info("Loaded " + reger.getRules().size() + " rules " + " for types " + reger.getTypes()); LOGGER.info("Writing results to " + resFile.getAbsolutePath()); // loop over the lines of the test file LineIterator iter = null; try { iter = FileUtils.lineIterator(testFile, "UTF-8"); } catch (IOException e) { LOGGER.error("Couldnt read from " + testFile.getName() + ":" + e.getMessage(), e); } int lineCount = 0; while (iter.hasNext()) { // get next line String line = iter.next(); // skip comments and blank lines if (line == null || line.startsWith("#") || line.trim().length() == 0) { continue; } lineCount++; // get the fields of the line String[] pieces = line.split("[\t\\s]+", 3); String entityType = pieces[0]; String posOrNeg = pieces[1]; String testText = pieces[2]; // send the test text to regex matcher List<RegexAnnotation> annos = reger.match(testText); // examine the results and return a line to be sent to the results // file String results = score(entityType, posOrNeg, testText, annos); annos.clear(); try { // write the original line and the results to the results file resWriter.write(line + "\t" + results); resWriter.newLine(); } catch (IOException e) { LOGGER.error("Couldn't write to " + resFile.getName() + ":" + e.getMessage(), e); } } iter.close(); LOGGER.info("Tagged and scored " + lineCount + " test lines"); // cleanup try { resWriter.flush(); resWriter.close(); } catch (IOException e) { LOGGER.error("Couldn't close " + resFile.getName() + ":" + e.getMessage(), e); } }
From source file:org.opentestsystem.delivery.testreg.upload.TextFileUtils.java
public void processTextFile(final InputStream textFile, final String formatType, final LineMapper lineMapper) throws Exception { final String CHARSET = "UTF-8"; final TextFileProcessor<InputStream, String> textFileProcessor = new TextFileProcessor<InputStream, String>() { @Override//from www .j a va 2s . com public String process(final InputStream input) throws Exception { LineIterator lineIterator = null; try { lineIterator = IOUtils.lineIterator(textFile, CHARSET); int lineNumber = 1; // First line starts at 1 while (lineIterator.hasNext()) { String line = lineIterator.next(); if (lineNumber == 1) { //worry about BOM chars (/ufeff) line = line.replaceAll("[\uFEFF]", ""); } if (!lineMapper.mapLine(line, lineNumber++, formatType)) { break; } } } catch (RuntimeException | IOException ex) { if (lineIterator != null) { lineIterator.close(); } IOUtils.closeQuietly(textFile); throw ex; } finally { if (lineIterator != null) { lineIterator.close(); } IOUtils.closeQuietly(textFile); } return null; } }; textFileProcessor.process(textFile); }
From source file:org.proninyaroslav.libretorrent.core.IPFilterParser.java
public static boolean parseDATFilterFile(String path, ip_filter filter) { if (path == null || filter == null) { return false; }/*from w w w . j a va 2s .c om*/ File file = new File(path); if (!file.exists()) { return false; } LineIterator it = null; try { it = FileUtils.lineIterator(file, "UTF-8"); } catch (IOException e) { Log.e(TAG, Log.getStackTraceString(e)); } if (it == null) { return false; } long lineNum = 0; long badLineNum = 0; try { while (it.hasNext()) { ++lineNum; String line = it.nextLine(); line = line.trim(); if (line.isEmpty()) { continue; } /* Ignoring commented lines */ if (line.startsWith("#") || line.startsWith("//")) { continue; } /* Line should be split by commas */ String[] parts = line.split(","); long elementNum = parts.length; /* IP Range should be split by a dash */ String[] ips = parts[0].split("-"); if (ips.length != 2) { Log.w(TAG, "parseDATFilterFile: line " + lineNum + " is malformed."); Log.w(TAG, "Line was " + line); ++badLineNum; continue; } String startIp = cleanupIPAddress(ips[0]); if (startIp == null || startIp.isEmpty()) { Log.w(TAG, "parseDATFilterFile: line " + lineNum + " is malformed."); Log.w(TAG, "Start IP of the range is malformated: " + ips[0]); ++badLineNum; continue; } error_code error = new error_code(); address startAddr = address.from_string(startIp, error); if (error.value() > 0) { Log.w(TAG, "parseDATFilterFile: line " + lineNum + " is malformed."); Log.w(TAG, "Start IP of the range is malformated:" + ips[0]); ++badLineNum; continue; } String endIp = cleanupIPAddress(ips[1]); if (endIp == null || endIp.isEmpty()) { Log.w(TAG, "parseDATFilterFile: line " + lineNum + " is malformed."); Log.w(TAG, "End IP of the range is malformated: " + ips[1]); ++badLineNum; continue; } address endAddr = address.from_string(endIp, error); if (error.value() > 0) { Log.w(TAG, "parseDATFilterFile: line " + lineNum + " is malformed."); Log.w(TAG, "End IP of the range is malformated:" + ips[1]); ++badLineNum; continue; } if (startAddr.is_v4() != endAddr.is_v4()) { Log.w(TAG, "parseDATFilterFile: line " + lineNum + " is malformed."); Log.w(TAG, "One IP is IPv4 and the other is IPv6!"); ++badLineNum; continue; } /* Check if there is an access value (apparently not mandatory) */ int accessNum = 0; if (elementNum > 1) { /* There is possibly one */ accessNum = Integer.parseInt(parts[1].trim()); } /* Ignoring this rule because access value is too high */ if (accessNum > 127) { continue; } try { filter.add_rule(startAddr, endAddr, ip_filter.access_flags.blocked.swigValue()); } catch (Exception e) { Log.w(TAG, "parseDATFilterFile: line " + lineNum + " is malformed."); Log.w(TAG, "Line was " + line); ++badLineNum; } } } finally { it.close(); } return badLineNum < lineNum; }
From source file:org.proninyaroslav.libretorrent.core.IPFilterParser.java
public static boolean parseP2PFilterFile(String path, ip_filter filter) { if (path == null || filter == null) { return false; }// w w w.j a v a 2 s . co m File file = new File(path); if (!file.exists()) { return false; } LineIterator it = null; try { it = FileUtils.lineIterator(file, "UTF-8"); } catch (IOException e) { Log.e(TAG, Log.getStackTraceString(e)); } if (it == null) { return false; } long lineNum = 0; long badLineNum = 0; try { while (it.hasNext()) { ++lineNum; String line = it.nextLine(); line = line.trim(); if (line.isEmpty()) { continue; } /* Ignoring commented lines */ if (line.startsWith("#") || line.startsWith("//")) { continue; } /* Line should be split by ':' */ String[] parts = line.split(":"); if (parts.length < 2) { Log.w(TAG, "parseP2PFilterFile: line " + lineNum + " is malformed."); ++badLineNum; continue; } /* IP Range should be split by a dash */ String[] ips = parts[1].split("-"); if (ips.length != 2) { Log.w(TAG, "parseP2PFilterFile: line " + lineNum + " is malformed."); Log.w(TAG, "Line was " + line); ++badLineNum; continue; } String startIp = cleanupIPAddress(ips[0]); if (startIp == null || startIp.isEmpty()) { Log.w(TAG, "parseP2PFilterFile: line " + lineNum + " is malformed."); Log.w(TAG, "Start IP of the range is malformated: " + ips[0]); ++badLineNum; continue; } error_code error = new error_code(); address startAddr = address.from_string(startIp, error); if (error.value() > 0) { Log.w(TAG, "parseP2PFilterFile: line " + lineNum + " is malformed."); Log.w(TAG, "Start IP of the range is malformated:" + ips[0]); ++badLineNum; continue; } String endIp = cleanupIPAddress(ips[1]); if (endIp == null || endIp.isEmpty()) { Log.w(TAG, "parseP2PFilterFile: line " + lineNum + " is malformed."); Log.w(TAG, "End IP of the range is malformated: " + ips[1]); ++badLineNum; continue; } address endAddr = address.from_string(endIp, error); if (error.value() > 0) { Log.w(TAG, "parseP2PFilterFile: line " + lineNum + " is malformed."); Log.w(TAG, "End IP of the range is malformated:" + ips[1]); ++badLineNum; continue; } if (startAddr.is_v4() != endAddr.is_v4()) { Log.w(TAG, "parseP2PFilterFile: line " + lineNum + " is malformed."); Log.w(TAG, "One IP is IPv4 and the other is IPv6!"); ++badLineNum; continue; } try { filter.add_rule(startAddr, endAddr, ip_filter.access_flags.blocked.swigValue()); } catch (Exception e) { Log.w(TAG, "parseP2PFilterFile: line " + lineNum + " is malformed."); Log.w(TAG, "Line was " + line); ++badLineNum; } } } finally { it.close(); } return badLineNum < lineNum; }
From source file:org.wikimedia.analytics.varnishkafka.Cli.java
private Integer writeJsonOutput() { int n = 0;/*from w ww . j a va 2s.c o m*/ JsonFactory jfactory = new JsonFactory(); /*** write to file ***/ try { JsonGenerator jGenerator; SnappyOutputStream snappyOutputStream = null; File outputFile = new File(cwd.getPath(), "test." + getFormat()); OutputStream out = new FileOutputStream(outputFile); BufferedOutputStream bos = new BufferedOutputStream(out); if (compress) { snappyOutputStream = new SnappyOutputStream(bos); jGenerator = jfactory.createJsonGenerator(snappyOutputStream, JsonEncoding.UTF8); } else { jGenerator = jfactory.createJsonGenerator(bos, JsonEncoding.UTF8); } log.info("Output file path: " + outputFile.toString()); LineIterator it = FileUtils.lineIterator(inputFile, "UTF-8"); try { setStart(System.nanoTime()); while (it.hasNext()) { n++; String line = it.nextLine(); String[] fields = line.split("\\t"); jGenerator.writeStartObject(); jGenerator.writeNumberField("kafka_offset", Long.parseLong(fields[0])); jGenerator.writeStringField("host", fields[1]); jGenerator.writeNumberField("seq_num", Long.parseLong(fields[2])); jGenerator.writeStringField("timestamp", fields[3]); jGenerator.writeNumberField("response", Float.parseFloat(fields[4])); jGenerator.writeStringField("ip", fields[5]); jGenerator.writeStringField("http_status", fields[6]); jGenerator.writeNumberField("bytes_sent", parseBytesSent(fields[7])); jGenerator.writeStringField("request_method", fields[8]); jGenerator.writeStringField("uri", fields[9]); jGenerator.writeStringField("proxy_host", fields[10]); jGenerator.writeStringField("mime_type", fields[11]); jGenerator.writeStringField("referer", fields[12]); jGenerator.writeStringField("x_forwarded_for", fields[13]); jGenerator.writeStringField("user_agent", fields[14]); jGenerator.writeStringField("accept_language", fields[15]); jGenerator.writeStringField("x_analytics", fields[16]); jGenerator.writeEndObject(); } setEnd(System.nanoTime()); } finally { it.close(); jGenerator.flush(); jGenerator.close(); if (compress) { snappyOutputStream.close(); } else { out.close(); bos.close(); } } } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return n; }
From source file:se.nawroth.asciidoc.browser.AsciidocBrowserApplication.java
private void showFile(final File file, final boolean addIt) { locationTextField.setText(file.getAbsolutePath()); refreshReplacements();//from w w w. ja v a 2s .c o m try { StringBuilder sb = new StringBuilder(10 * 1024); sb.append("<html><head><title>" + file.getName() + "</title><style>body {font-size: 1em;}pre {margin: 0;}</style></head><body>"); String parent = file.getParent(); LineIterator lines = FileUtils.lineIterator(file, "UTF-8"); while (lines.hasNext()) { String line = StringEscapeUtils.escapeHtml4(lines.next()); sb.append("<pre>"); if (line.startsWith(LINK_LINE_START)) { String href = getFileLocation(parent, line); sb.append("<a href=\"").append(href).append("\">").append(line).append("</a>"); } else { sb.append(line); } sb.append("</pre>"); } sb.append("</body></html>"); lines.close(); sourceEditorPane.setText(sb.toString()); sourceEditorPane.setCaretPosition(0); if (addIt) { int listSize = pageList.size(); if (listSize > 0) { int pageIndex = pageList.indexOf(currentFile); if (pageIndex < listSize - 1) { for (int i = listSize - 1; i > pageIndex; i--) { pageList.remove(i); } } } pageList.add(file); } currentFile = file; updateButtons(); if (paths.containsKey(file)) { currentSelectionPath = new TreePath(paths.get(file)); documentTree.setSelectionPath(currentSelectionPath); } } catch (IOException e) { showError("Error in file handling: " + e); } }
From source file:voldemort.utils.app.VoldemortApp.java
protected List<HostNamePair> getHostNamesPairsFromFile(File file) { List<HostNamePair> hostNamePairs = new ArrayList<HostNamePair>(); // This was recently changed from using Properties to performing the // file parsing manually. This is due to the fact that we want to // maintain the ordering in the file. Line N in the host names file // should correspond to node ID N-1. Previously they were in hashed // order, so certain tools that auto-configure a cluster based on // this same hosts file were creating invalid configurations between // the cluster.xml and server.properties (node.id) files. LineIterator li = null; int lineNumber = 0; try {/* w ww .j a v a 2 s .co m*/ li = FileUtils.lineIterator(file); while (li.hasNext()) { String rawLine = String.valueOf(li.next()).trim(); lineNumber++; // Strip comments int hashIndex = rawLine.indexOf("#"); if (hashIndex != -1) rawLine = rawLine.substring(0, hashIndex).trim(); // Whitespace if (rawLine.length() == 0) continue; String[] line = StringUtils.split(rawLine, " \t=:"); if (line.length < 1 || line.length > 2) { System.err.println("Invalid entry (line " + lineNumber + ") in " + file.getAbsolutePath() + ": " + rawLine); System.exit(2); } String externalHostName = line[0]; String internalHostName = line.length > 1 ? line[1] : externalHostName; hostNamePairs.add(new HostNamePair(externalHostName, internalHostName)); } } catch (IOException e) { throw new RuntimeException(e); } finally { if (li != null) li.close(); } return hostNamePairs; }