List of usage examples for java.io LineNumberReader LineNumberReader
public LineNumberReader(Reader in)
From source file:org.kalypso.model.hydrology.internal.postprocessing.LzsToGml.java
private double readLzgFile(final FileReader fileReader) throws NumberFormatException, IOException { final LineNumberReader reader = new LineNumberReader(fileReader); ChannelStatus status = ChannelStatus.SEARCH_HEADER; final String iniDate = m_dateFormat.format(m_initialDate); while (reader.ready()) { final String line = reader.readLine(); if (line == null) break; final String cleanLine = line.trim().replaceAll("\\s+", " "); //$NON-NLS-1$ //$NON-NLS-2$ switch (status) { case SEARCH_HEADER: if (cleanLine.endsWith("qgs") && cleanLine.startsWith(iniDate)) //$NON-NLS-1$ // 19960521 00 h 1 qgs // 1 0.000 status = ChannelStatus.READ_QGS; break; case READ_QGS: // Gesamtabfluss final String[] strings = cleanLine.split(" "); //$NON-NLS-1$ return Double.valueOf(strings[1]); }/*from w ww . j ava2s .c o m*/ } return Double.NaN; }
From source file:org.apache.james.server.core.MimeMessageWrapper.java
/** * Corrects JavaMail 1.1 version which always returns -1. Only corrected for * content less than 5000 bytes, to avoid memory hogging. *//*from w w w .j a v a2 s .co m*/ @Override public int getLineCount() throws MessagingException { InputStream in; try { in = getContentStream(); } catch (Exception e) { return -1; } if (in == null) { return -1; } // Wrap input stream in LineNumberReader // Not sure what encoding to use really... try (InputStream input = in; InputStreamReader isr = builderReader(input)) { // Read through all the data char[] block = new char[4096]; try (LineNumberReader counter = new LineNumberReader(isr)) { while (counter.read(block) > -1) { // Just keep reading } return counter.getLineNumber(); } } catch (IOException ioe) { return -1; } }
From source file:org.apache.james.core.MimeMessageWrapper.java
/** * Corrects JavaMail 1.1 version which always returns -1. Only corrected for * content less than 5000 bytes, to avoid memory hogging. */// www. j ava 2s .com @Override public int getLineCount() throws MessagingException { InputStream in; try { in = getContentStream(); } catch (Exception e) { return -1; } if (in == null) { return -1; } // Wrap input stream in LineNumberReader // Not sure what encoding to use really... InputStreamReader isr = null; LineNumberReader counter = null; try { if (getEncoding() != null) { isr = new InputStreamReader(in, getEncoding()); counter = new LineNumberReader(isr); } else { isr = new InputStreamReader(in); counter = new LineNumberReader(isr); } // Read through all the data char[] block = new char[4096]; while (counter.read(block) > -1) { // Just keep reading } return counter.getLineNumber(); } catch (IOException ioe) { return -1; } finally { IOUtils.closeQuietly(counter); IOUtils.closeQuietly(isr); IOUtils.closeQuietly(in); } }
From source file:edu.stanford.muse.index.Indexer.java
public static Set<String> readFileAndInternStrings(String file) { Set<String> result = new LinkedHashSet<String>(); try {// w w w . j ava 2 s . c om Reader r = null; if (file.toLowerCase().endsWith(".gz")) r = new InputStreamReader(new GZIPInputStream(new FileInputStream(file))); else r = new FileReader(file); LineNumberReader lnr = new LineNumberReader(r); while (true) { String word = lnr.readLine(); if (word == null) { lnr.close(); break; } word = word.trim(); if (word.startsWith("#") || word.length() == 0) continue; word = IndexUtils.canonicalizeMultiWordTerm(word, false); // TOFIX: not really sure if stemming shd be on word = InternTable.intern(word); result.add(word); } } catch (IOException e) { log.warn("Exception reading file " + file + ": " + e + Util.stackTrace(e)); } return result; }
From source file:org.agnitas.beans.impl.MailingImpl.java
@Override public DynamicTag findNextDynTag(String aTemplate, ApplicationContext con) throws Exception { int valueTagStartPos; int oldPos;/*from ww w . j av a 2 s . c o m*/ DynamicTag aDynTag = null; TagDetails aStartTag = null; TagDetails aEndTag = null; TagDetails aValueTag = null; aStartTag = getOneTag(aTemplate, "agnDYN", searchPos, con); if (aStartTag == null) return null; aStartTag.analyzeParameters(); aStartTag.findTagParameters(); searchPos = aStartTag.getEndPos(); aDynTag = (DynamicTag) con.getBean("DynamicTag"); aDynTag.setCompanyID(companyID); aDynTag.setMailingID(id); aDynTag.setComplex(aStartTag.isComplex()); aDynTag.setDynName(aStartTag.getName()); int group = 0; Map<String, String> params = aStartTag.getTagParameters(); if (params != null) { String gname = (String) params.get("group"); if (gname != null) { DynamicTagDao dao = (DynamicTagDao) con.getBean("DynamicTagDao"); group = dao.getIdForName(this.id, gname); } } aDynTag.setGroup(group); if (aStartTag.isComplex()) { oldPos = searchPos; do { aEndTag = getOneTag(aTemplate, "/agnDYN", searchPos, con); if (aEndTag == null) { LineNumberReader aReader = new LineNumberReader(new StringReader(aTemplate)); aReader.skip(searchPos); throw new Exception("NoEndTag$" + aReader.getLineNumber() + "$" + aStartTag.getName()); } searchPos = aEndTag.getEndPos(); aEndTag.analyzeParameters(); } while (aStartTag.getName().compareTo(aEndTag.getName()) != 0); String valueArea = aTemplate.substring(aStartTag.getEndPos(), aEndTag.getStartPos()); valueTagStartPos = 0; do { aValueTag = getOneTag(valueArea, "agnDVALUE", valueTagStartPos, con); if (aValueTag == null) { LineNumberReader aReader = new LineNumberReader(new StringReader(aTemplate)); aReader.skip(searchPos); throw new Exception("NoValueTag$" + aReader.getLineNumber() + "$" + aStartTag.getName()); } valueTagStartPos = aValueTag.getEndPos(); aValueTag.analyzeParameters(); } while (aStartTag.getName().compareTo(aValueTag.getName()) != 0); searchPos = oldPos; aDynTag.setStartTagStart(aStartTag.getStartPos()); aDynTag.setStartTagEnd(aStartTag.getEndPos()); aDynTag.setValueTagStart(aStartTag.getEndPos() + aValueTag.getStartPos()); aDynTag.setValueTagEnd(aStartTag.getEndPos() + aValueTag.getEndPos()); aDynTag.setEndTagStart(aEndTag.getStartPos()); aDynTag.setEndTagEnd(aEndTag.getEndPos()); } else { aDynTag.setStartTagStart(aStartTag.getStartPos()); aDynTag.setStartTagEnd(aStartTag.getEndPos()); } return aDynTag; }
From source file:org.beanfuse.struts2.action.EntityDrivenAction.java
/** * /* w w w . j a v a 2 s . c o m*/ * * @param upload * @param clazz * @return */ protected EntityImporter buildEntityImporter(String upload, Class clazz) { try { File[] files = (File[]) ActionContext.getContext().getParameters().get(upload); if (files == null || files.length < 1) { logger.error("cannot get {} file.", upload); } String fileName = get(upload + "FileName"); InputStream is = new FileInputStream(files[0]); if (fileName.endsWith(".xls")) { HSSFWorkbook wb = new HSSFWorkbook(is); if (wb.getNumberOfSheets() < 1 || wb.getSheetAt(0).getLastRowNum() == 0) { return null; } EntityImporter importer = (clazz == null) ? new DefaultEntityImporter() : new DefaultEntityImporter(clazz); importer.setReader(new ExcelItemReader(wb, 1)); put("importer", importer); return importer; } else { LineNumberReader reader = new LineNumberReader(new InputStreamReader(is)); if (null == reader.readLine()) return null; reader.reset(); EntityImporter importer = (clazz == null) ? new DefaultEntityImporter() : new DefaultEntityImporter(clazz); importer.setReader(new CSVReader(reader)); return importer; } } catch (Exception e) { logger.error("error", e); return null; } }
From source file:org.guzz.util.PropertyUtil.java
/** * Mysql?my.cnf???[key]keykey[key]value?PropertiesMap * //from ww w. ja v a 2 s.c o m * @param resource ???resource * @return Map ??vsprop?Properties[]?null */ public static Map loadGroupedProps(Resource resource) { Map resources = new HashMap(); LineNumberReader lnr = null; String line = null; String groupName = null; Properties props = null; try { lnr = new LineNumberReader(new InputStreamReader(resource.getInputStream())); while ((line = lnr.readLine()) != null) { line = line.trim(); int length = line.length(); if (length == 0) continue; if (line.charAt(0) == '#') continue; if (line.startsWith("rem ")) continue; if (line.charAt(0) == '[' && line.charAt(length - 1) == ']') {//[xxxx] //?? if (groupName != null) { Properties[] p = (Properties[]) resources.get(groupName); if (p == null) { resources.put(groupName, new Properties[] { props }); } else { resources.put(groupName, ArrayUtil.addToArray(p, props)); } } //? groupName = line.substring(1, length - 1).trim(); props = new Properties(); } else { // if (groupName == null) { log.warn("ignore ungrouped config property:" + line); } else { int pos = line.indexOf('='); if (pos < 1) { props.put(line, ""); log.warn("loading special config property:" + line); } else { String key = line.substring(0, pos).trim(); String value = line.substring(pos + 1, length).trim(); props.put(key, value); } } } } } catch (Exception e) { if (log.isDebugEnabled()) { log.debug("load resource failed. resouce:[" + resource + "], msg:" + e.getMessage()); } return null; } finally { CloseUtil.close(lnr); CloseUtil.close(resource); } //?? if (groupName != null) { Properties[] p = (Properties[]) resources.get(groupName); if (p == null) { resources.put(groupName, new Properties[] { props }); } else { resources.put(groupName, ArrayUtil.addToArray(p, props)); } } return resources; }
From source file:de.upb.timok.run.GenericSmacPipeline.java
private void splitTrainTestFile(String timedInputFile, String timedInputTrainFile, String timedInputTestFile, double trainPercentage, double testPercentage, double anomalyPercentage, boolean isRti) throws IOException { logger.info("TimedInputFile=" + timedInputFile); final File f = new File(timedInputFile); System.out.println(f);//from www . ja v a 2 s . c o m final LineNumberReader lnr = new LineNumberReader(new FileReader(timedInputFile)); lnr.skip(Long.MAX_VALUE); int samples = lnr.getLineNumber(); lnr.close(); final int trainingSamples = (int) (samples * trainPercentage); final int testSamples = (int) (samples * testPercentage); final int anomalies = (int) (anomalyPercentage * testSamples); final int writtenTrainingSamples = 0; final int writtenTestSamples = 0; int insertedAnomalies = 0; final BufferedReader br = Files.newBufferedReader(Paths.get(timedInputFile), StandardCharsets.UTF_8); String line = null; final BufferedWriter trainWriter = Files.newBufferedWriter(Paths.get(timedInputTrainFile), StandardCharsets.UTF_8); final BufferedWriter testWriter = Files.newBufferedWriter(Paths.get(timedInputTestFile), StandardCharsets.UTF_8); final Random r = new Random(MasterSeed.nextLong()); final Random mutation = new Random(MasterSeed.nextLong()); boolean force = false; int lineIndex = 0; int linesLeft; int anomaliesToInsert; if (isRti) { br.readLine(); samples--; } while ((line = br.readLine()) != null) { if (writtenTrainingSamples < trainingSamples && writtenTestSamples < testSamples) { // choose randomly according to train/test percentage if (r.nextDouble() > testPercentage) { // write to train writeSample(new TimedSequence(line, true, false).toTrebaString(), trainWriter); } else { // write to test insertedAnomalies = testAndWriteAnomaly(anomalies, insertedAnomalies, anomalyPercentage, line, testWriter, mutation, force); } } else if (writtenTrainingSamples >= trainingSamples) { insertedAnomalies = testAndWriteAnomaly(anomalies, insertedAnomalies, anomalyPercentage, line, testWriter, mutation, force); } else if (writtenTestSamples >= testSamples) { // only write trainSamples from now on writeSample(new TimedSequence(line, true, false).toTrebaString(), trainWriter); } lineIndex++; linesLeft = samples - lineIndex; anomaliesToInsert = anomalies - insertedAnomalies; if (linesLeft <= anomaliesToInsert) { force = true; } } br.close(); trainWriter.close(); testWriter.close(); }
From source file:gpl.pierrick.brihaye.aramorph.InMemoryDictionaryHandler.java
/** Loads a compatibility table into a <CODE>Set</CODE>. * @param set The set// ww w.ja v a 2 s. c om * @param name A human-readable name * @param is The stream * @throws RuntimeException If a problem occurs when reading the compatibility table */ private static void loadCompatibilityTable(Set set, String name, InputStream is) throws RuntimeException { System.out.print("Loading compatibility table : " + name + " "); try { LineNumberReader IN = new LineNumberReader(new InputStreamReader(is, "ISO8859_1")); String line = null; while ((line = IN.readLine()) != null) { if ((IN.getLineNumber() % 1000) == 1) System.out.print("."); if (!line.startsWith(";")) { //Ignore comments line = line.trim(); line = line.replaceAll("\\s+", " "); set.add(line); } } IN.close(); System.out.println(); System.out.println(set.size() + " entries"); } catch (IOException e) { throw new RuntimeException("Can not open : " + name); } }
From source file:com.raulexposito.alarife.sqlexecutor.SQLExecutor.java
/** * Reads the content of a script file and, for each command on it, generates a single command * @param file the script file with the commands * @return a list with the commands of the script * @throws java.io.IOException if the file cannot be readed *///from w ww.jav a2 s .co m protected static List<String> getSQLCommandsFromScriptFile(final InputStream file) throws IOException { // list of commands to be returned final List<String> SQLCommands = new ArrayList<String>(); // reader for the script file final LineNumberReader scriptFile = new LineNumberReader( new InputStreamReader(new BufferedInputStream(file), "ISO-8859-1")); // string builder where append the lines of the file final StringBuilder content = new StringBuilder(); // next line to be readed from the script file String line; while ((line = scriptFile.readLine()) != null) { log.trace(line); line = line.trim(); // deletes the comments if (line.indexOf(SQL_COMMENT) != NOT_EXISTS) { line = line.substring(0, line.indexOf(SQL_COMMENT)); } content.append(line); // is the end of the command? if (line.endsWith(SQL_END_OF_COMMAND)) { SQLCommands.add(content.toString()); content.setLength(0); } } return SQLCommands; }