List of usage examples for org.apache.commons.io LineIterator closeQuietly
public static void closeQuietly(LineIterator iterator)
From source file:fr.gael.dhus.server.http.webapp.search.controller.SearchController.java
/** * Provides the openSearch description file via /search/description API. * * @param res response//from ww w . jav a2s .c o m * @throws IOException if file description cannot be accessed */ @PreAuthorize("hasRole('ROLE_SEARCH')") @RequestMapping(value = "/description") public void search(HttpServletResponse res) throws IOException { String url = configurationManager.getServerConfiguration().getExternalUrl(); if (url != null && url.endsWith("/")) { url = url.substring(0, url.length() - 1); } String long_name = configurationManager.getNameConfiguration().getLongName(); String short_name = configurationManager.getNameConfiguration().getShortName(); String contact_mail = configurationManager.getSupportConfiguration().getMail(); InputStream is = ClassLoader.getSystemResourceAsStream(DESCRIPTION_FILE); if (is == null) { throw new IOException("Cannot find \"" + DESCRIPTION_FILE + "\" OpenSearch description file."); } LineIterator li = IOUtils.lineIterator(is, "UTF-8"); try (ServletOutputStream os = res.getOutputStream()) { while (li.hasNext()) { String line = li.next(); // Last line? -> the iterator eats LF if (li.hasNext()) { line = line + "\n"; } line = line.replace("[dhus_server]", url); if (long_name != null) { line = line.replace("[dhus_long_name]", long_name); } if (short_name != null) { line = line.replace("[dhus_short_name]", short_name); } if (contact_mail != null) { line = line.replace("[dhus_contact_mail]", contact_mail); } os.write(line.getBytes()); } } finally { IOUtils.closeQuietly(is); LineIterator.closeQuietly(li); } }
From source file:com.ipcglobal.fredimport.process.Reference.java
/** * Creates the ref us states./* ww w . jav a2s . c o m*/ * * @param path the path * @throws Exception the exception */ private void createRefUSStates(String path) throws Exception { refStateAbbrevsByNames = new HashMap<String, String>(); refValidStateAbbrevs = new HashMap<String, Object>(); LineIterator it = FileUtils.lineIterator(new File(path + FILENAME_US_STATE_NAMES_ABBREVS), "UTF-8"); try { while (it.hasNext()) { // Format: <stateName>|<stateAbbrev> String line = it.nextLine(); String[] fields = line.split("[|]"); fields[0] = fields[0].trim(); fields[1] = fields[1].trim(); refStateAbbrevsByNames.put(fields[0], fields[1]); refValidStateAbbrevs.put(fields[1], null); } } finally { LineIterator.closeQuietly(it); } }
From source file:$.LogEventParsingIterator.java
/** * Returns a line iterator to process next/current file, opening the next file, if necessary. *//www. j a v a 2 s .c o m * @return line iterator for the current/next file; or null if no files left */ private LineIterator getLineIterator() throws IOException { if (lineIterator != null && lineIterator.hasNext()) { return lineIterator; } // discard current line iterator LineIterator.closeQuietly(lineIterator); lineIterator = null; if (fileEventsFound == 0) { Log.debug("No events in the last file, closing prematurely"); close(); // optimize: last file had no events, no point continuing } else if (!logFiles.isEmpty()) { File file = logFiles.poll(); Log.debug("Opening {}", file); lineIterator = FileUtils.lineIterator(file); fileEventsFound = 0; // restart per-file counter } return lineIterator; }
From source file:net.sf.logsaw.dialect.websphere.WebsphereDialect.java
@Override public void parse(ILogResource log, InputStream input, ILogEntryCollector collector) throws CoreException { Assert.isNotNull(log, "log"); //$NON-NLS-1$ Assert.isNotNull(input, "input"); //$NON-NLS-1$ Assert.isNotNull(collector, "collector"); //$NON-NLS-1$ Assert.isTrue(isConfigured(), "Dialect should be configured by now"); //$NON-NLS-1$ try {/*from ww w . j a va 2s . co m*/ LogEntry currentEntry = null; IHasEncoding enc = (IHasEncoding) log.getAdapter(IHasEncoding.class); IHasLocale loc = (IHasLocale) log.getAdapter(IHasLocale.class); // WebSphere Dialect doesn't need to care about the timezone, because it is encoded in the log messages DateFormat df = getDateFormat(loc.getLocale()); LineIterator iter = IOUtils.lineIterator(input, enc.getEncoding()); int lineNo = 0; try { while (iter.hasNext()) { // Error handling lineNo++; List<IStatus> statuses = null; boolean fatal = false; // determines whether to interrupt parsing String line = iter.nextLine(); Matcher m = getInternalPattern().matcher(line); if (m.find()) { // The next line matches, so flush the previous entry and continue if (currentEntry != null) { collector.collect(currentEntry); currentEntry = null; } currentEntry = new LogEntry(); for (int i = 0; i < m.groupCount(); i++) { try { extractField(currentEntry, i + 1, m.group(i + 1), df); } catch (CoreException e) { // Mark for interruption fatal = fatal || e.getStatus().matches(IStatus.ERROR); // Messages will be displayed later if (statuses == null) { statuses = new ArrayList<IStatus>(); } if (e.getStatus().isMultiStatus()) { Collections.addAll(statuses, e.getStatus().getChildren()); } else { statuses.add(e.getStatus()); } } } // We encountered errors or warnings if (statuses != null && !statuses.isEmpty()) { currentEntry = null; // Stop propagation IStatus status = new MultiStatus(WebsphereDialectPlugin.PLUGIN_ID, 0, statuses.toArray(new IStatus[statuses.size()]), NLS.bind(Messages.WebsphereDialect_error_failedToParseLine, lineNo), null); if (fatal) { // Interrupt parsing in case of error throw new CoreException(status); } else { collector.addMessage(status); } } } else if (currentEntry != null) { // Append to message String msg = currentEntry.get(getFieldProvider().getMessageField()); StringWriter strWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(strWriter); printWriter.print(msg); printWriter.println(); printWriter.print(line); currentEntry.put(getFieldProvider().getMessageField(), strWriter.toString()); } if (collector.isCanceled()) { // Cancel parsing break; } } if (currentEntry != null) { // Collect left over entry collector.collect(currentEntry); } } finally { LineIterator.closeQuietly(iter); } } catch (Exception e) { throw new CoreException(new Status(IStatus.ERROR, WebsphereDialectPlugin.PLUGIN_ID, NLS.bind(Messages.WebsphereDialect_error_failedToParseFile, new Object[] { log.getName(), e.getLocalizedMessage() }), e)); } }
From source file:fr.ericlab.mabed.structure.Corpus.java
public void loadCorpus(boolean parallelized) { output = "";/*from ww w . ja v a 2s . c om*/ if (configuration.prepareCorpus) { prepareCorpus(); } String[] fileArray = new File("input/").list(); nbTimeSlices = 0; NumberFormat formatter = new DecimalFormat("00000000"); ArrayList<Integer> list = new ArrayList<>(); for (String filename : fileArray) { if (filename.endsWith(".text")) { try { list.add(formatter.parse(filename.substring(0, 8)).intValue()); } catch (ParseException ex) { Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex); } nbTimeSlices++; } } int a = Collections.min(list), b = Collections.max(list); distribution = new int[nbTimeSlices]; messageCount = 0; LineIterator it = null; try { it = FileUtils.lineIterator(new File("input/" + formatter.format(a) + ".time"), "UTF-8"); if (it.hasNext()) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); Date parsedDate = dateFormat.parse(it.nextLine()); startTimestamp = new java.sql.Timestamp(parsedDate.getTime()); } it = FileUtils.lineIterator(new File("input/" + formatter.format(b) + ".time"), "UTF-8"); String timestamp = ""; while (it.hasNext()) { timestamp = it.nextLine(); } SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); Date parsedDate = dateFormat.parse(timestamp); endTimestamp = new java.sql.Timestamp(parsedDate.getTime()); } catch (IOException | ParseException ex) { Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex); } finally { LineIterator.closeQuietly(it); } try { // Global index FileInputStream fisMatrix = new FileInputStream("input/indexes/frequencyMatrix.dat"); ObjectInputStream oisMatrix = new ObjectInputStream(fisMatrix); frequencyMatrix = (short[][]) oisMatrix.readObject(); FileInputStream fisVocabulary = new FileInputStream("input/indexes/vocabulary.dat"); ObjectInputStream oisVocabulary = new ObjectInputStream(fisVocabulary); vocabulary = (ArrayList<String>) oisVocabulary.readObject(); // Mention index FileInputStream fisMentionMatrix = new FileInputStream("input/indexes/mentionFrequencyMatrix.dat"); ObjectInputStream oisMentionMatrix = new ObjectInputStream(fisMentionMatrix); mentionFrequencyMatrix = (short[][]) oisMentionMatrix.readObject(); FileInputStream fisMentionVocabulary = new FileInputStream("input/indexes/mentionVocabulary.dat"); ObjectInputStream oisMentionVocabulary = new ObjectInputStream(fisMentionVocabulary); mentionVocabulary = (ArrayList<String>) oisMentionVocabulary.readObject(); // Message count String messageCountStr = FileUtils.readFileToString(new File("input/indexes/messageCount.txt")); messageCount = Integer.parseInt(messageCountStr); // Message count distribution FileInputStream fisDistribution = new FileInputStream("input/indexes/messageCountDistribution.dat"); ObjectInputStream oisDistribution = new ObjectInputStream(fisDistribution); distribution = (int[]) oisDistribution.readObject(); } catch (FileNotFoundException ex) { Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException | ClassNotFoundException ex) { Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex); } DecimalFormat df = new DecimalFormat("#,###"); System.out.println(Util.getDate() + " Loaded corpus:"); output += Util.getDate() + " Loaded corpus:\n"; info = " - time-slices: " + df.format(nbTimeSlices) + " time-slices of " + configuration.timeSliceLength + " minutes each\n"; info += " - first message: " + startTimestamp + "\n"; double datasetLength = (nbTimeSlices * configuration.timeSliceLength) / 60 / 24; info += " - last message: " + endTimestamp + " (" + datasetLength + " days)\n"; info += " - number of messages: " + df.format(messageCount); output += info; System.out.println(info); }
From source file:itemsetmining.itemset.ItemsetTree.java
/** * Build the itemset-tree based on an input file containing transactions * * @param input/*from w w w . j a v a 2s .c o m*/ * an input file * @return */ public void buildTree(final File inputFile) throws IOException { // record start time startTimestamp = System.currentTimeMillis(); // reset memory usage statistics MemoryLogger.getInstance().reset(); // create an empty root for the tree root = new ItemsetTreeNode(null, 0); // Scan the database to read the transactions int count = 0; final LineIterator it = FileUtils.lineIterator(inputFile, "UTF-8"); while (it.hasNext()) { final String line = it.nextLine(); // if the line is a comment, is empty or is a // kind of metadata if (line.isEmpty() == true || line.charAt(0) == '#' || line.charAt(0) == '%' || line.charAt(0) == '@') { continue; } // add transaction to the tree addTransaction(line); count++; } // close the input file LineIterator.closeQuietly(it); // set the number of transactions noTransactions = count; // check the memory usage MemoryLogger.getInstance().checkMemory(); endTimestamp = System.currentTimeMillis(); }
From source file:com.ipcglobal.fredimport.process.Reference.java
/** * Creates the ref countries./*from ww w . j a v a 2s.com*/ * * @param path the path * @throws Exception the exception */ private void createRefCountries(String path) throws Exception { refCountries = new HashMap<String, String>(); LineIterator it = FileUtils.lineIterator(new File(path + FILENAME_COUNTRIES), "UTF-8"); try { while (it.hasNext()) { // Format: <commonCountryName>|akaCountryName1|akaCountryName2|... // For example: United States|the U.S.|the United States // All three will match as a valid country, and "United States" will always be used as the common country name String[] countries = it.nextLine().split("[|]"); String commonCountryName = countries[0].trim(); refCountries.put(commonCountryName, commonCountryName); for (int i = 1; i < countries.length; i++) refCountries.put(countries[i].trim(), commonCountryName); } } finally { LineIterator.closeQuietly(it); } }
From source file:com.jpetrak.gate.scala.ScalaScriptPR.java
public void tryCompileScript() { String scalaProgramSource;/*w w w. j ava2 s .c o m*/ String className; if (classloader != null) { Gate.getClassLoader().forgetClassLoader(classloader); } classloader = Gate.getClassLoader().getDisposableClassLoader( //"C"+java.util.UUID.randomUUID().toString().replaceAll("-", ""), scalaProgramUrl.toExternalForm() + System.currentTimeMillis(), this.getClass().getClassLoader(), true); try { className = "ScalaScriptClass" + getNextId(); StringBuilder sb = new StringBuilder(); scalaProgramLines = new ArrayList<String>(); scalaProgramLines.add(fileProlog); scalaProgramLines.add(classProlog.replaceAll("THECLASSNAME", className)); LineIterator it = FileUtils.lineIterator(scalaProgramFile, "UTF-8"); try { while (it.hasNext()) { String line = it.nextLine(); scalaProgramLines.add(line); } } finally { LineIterator.closeQuietly(it); } scalaProgramLines.add(scalaCompiler.getClassEpilog().replaceAll("THECLASSNAME", className)); for (String line : scalaProgramLines) { sb.append(line); sb.append("\n"); } scalaProgramSource = sb.toString(); //System.out.println("Program Source: " + scalaProgramSource); } catch (IOException ex) { System.err.println("Problem reading program from " + scalaProgramUrl); ex.printStackTrace(System.err); return; } try { //System.out.println("Trying to compile ..."); scalaProgramClass = scalaCompiler.compile(className, scalaProgramSource, classloader); //scalaProgramClass = (ScalaScript) Gate.getClassLoader(). // loadClass("scalascripting." + className).newInstance(); scalaProgramClass.globalsForPr = globalsForPr; scalaProgramClass.lockForPr = new Object(); if (registeredEditorVR != null) { registeredEditorVR.setCompilationOk(); } scalaProgramClass.resource1 = resource1; scalaProgramClass.resource2 = resource2; scalaProgramClass.resource3 = resource3; isCompileError = false; scalaProgramClass.resetInitAll(); } catch (Exception ex) { System.err.println("Problem compiling ScalaScript Class"); printScalaProgram(System.err); ex.printStackTrace(System.err); if (classloader != null) { Gate.getClassLoader().forgetClassLoader(classloader); classloader = null; } isCompileError = true; scalaProgramClass = null; if (registeredEditorVR != null) { registeredEditorVR.setCompilationError(); } return; } }
From source file:edu.ku.brc.util.HelpIndexer.java
protected void processFile(final File file, final Vector<String> lines) { // System.out.println("processing file: " + file.getName()); LineIterator it;// ww w . j ava 2s . com try { it = FileUtils.lineIterator(file, "UTF-8"); } catch (IOException ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(HelpIndexer.class, ex); System.out.println("error processing file: " + file.getName()); return; } String target = getTarget(file); String title = getFileTitle(file); boolean removeTitleEntry = false; if (title != null) { String tline = "<indexitem text=\"" + title; if (target != null) { tline += "\" target=\"" + target; } tline += "\">"; lines.add(tline); removeTitleEntry = true; } if (target != null) { try { while (it.hasNext()) { String line = it.nextLine(); //System.out.println(line); if (isIndexLine(line)) { System.out.println("indexing " + file.getName() + ": " + line); String indexEntry = processIndexLine(line, target); if (indexEntry != null) { lines.add(" " + indexEntry); removeTitleEntry = false; } } } } finally { LineIterator.closeQuietly(it); } } if (title != null && !removeTitleEntry) { lines.add("</indexitem>"); } if (removeTitleEntry) { lines.remove(lines.size() - 1); } }
From source file:$.LogEventParsingIterator.java
@Override public void close() { LineIterator.closeQuietly(lineIterator); lineIterator = null; logFiles.clear(); Log.debug("Closed"); }