List of usage examples for org.apache.commons.io LineIterator hasNext
public boolean hasNext()
Reader
has more lines. From source file:org.orbisgis.view.toc.actions.cui.legend.components.ColorScheme.java
/** * Fills the inner collections using the dedicated resource file. *///from w w w .j a va 2s .co m private static void load() { rangeColorSchemeNames = new ArrayList<>(); discreteColorSchemeNames = new ArrayList<>(); nameToColorsMap = new HashMap<>(); InputStream stream = ColorScheme.class.getResourceAsStream("ColorScheme.txt"); if (stream != null) { InputStreamReader br = new InputStreamReader(stream); LineIterator lineIterator = IOUtils.lineIterator(br); try { while (lineIterator.hasNext()) { String line = lineIterator.nextLine(); add(line); } } finally { LineIterator.closeQuietly(lineIterator); } } }
From source file:org.orbisgis.view.toc.actions.cui.legends.components.ColorScheme.java
/** * Fills the inner collections using the dedicated resource file. *///from w w w .ja v a 2 s. c om private static void load() { rangeColorSchemeNames = new ArrayList<String>(); discreteColorSchemeNames = new ArrayList<String>(); nameToColorsMap = new HashMap<String, List<Color>>(); InputStream stream = ColorScheme.class.getResourceAsStream("ColorScheme.txt"); InputStreamReader br = new InputStreamReader(stream); LineIterator lineIterator = IOUtils.lineIterator(br); try { while (lineIterator.hasNext()) { String line = lineIterator.next(); add(line); } } finally { LineIterator.closeQuietly(lineIterator); } }
From source file:org.pentaho.metadata.query.model.util.CsvDataReader.java
public List<List<String>> loadData() { String line = null;// w w w .j a va 2 s. c o m List<List<String>> dataSample = new ArrayList<List<String>>(rowLimit); List<String> rowData = null; InputStreamReader reader = null; CSVTokenizer csvt = null; LineIterator lineIterator = null; try { InputStream inputStream = KettleVFS.getInputStream(fileLocation); reader = new InputStreamReader(inputStream); lineIterator = new LineIterator(reader); int row = 0; int count; while (row < rowLimit && lineIterator.hasNext()) { line = lineIterator.nextLine(); ++row; csvt = new CSVTokenizer(line, delimiter, enclosure, false); rowData = new ArrayList<String>(); count = 0; while (csvt.hasMoreTokens()) { // get next token and store it in the list rowData.add(csvt.nextToken()); count++; } if (columnCount < count) { columnCount = count; } if (headerPresent && row == 1) { header = rowData; } else { dataSample.add(rowData); } } } catch (KettleFileException e) { logger.error(Messages.getString("CsvDataReader.ERROR_0001_Failed"), e); //$NON-NLS-1$ } finally { LineIterator.closeQuietly(lineIterator); IOUtils.closeQuietly(reader); } data = dataSample; return dataSample; }
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 v a 2 s. c o 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 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; }/*from ww w . ja v a2 s . c o 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.schedulesdirect.api.LineupTest.java
static private void initJsonData() throws Exception { try {//from w ww .j a v a 2 s . c o m if (!SampleData.updateFor(SampleType.LINEUPS, false)) LOG.info("Using current sample data because it's less than a week old."); } catch (Exception e) { if (!SampleData.exists(SampleType.LINEUPS)) throw new IOException("No sample data available!", e); else LOG.warn("Failed to download fresh sample data; using existing data instead!"); } LineIterator itr = FileUtils.lineIterator(SampleData.locate(SampleType.LINEUPS), "UTF-8"); while (itr.hasNext()) SAMPLE_DATA.add(itr.nextLine()); }
From source file:org.sindice.siren.demo.bnb.BNBDemo.java
public void index() throws IOException { final SimpleIndexer indexer = new SimpleIndexer(indexDir); try {//from w w w .ja va2 s . c o m int counter = 0; final LineIterator it = FileUtils.lineIterator(BNB_PATH); while (it.hasNext()) { final String id = Integer.toString(counter++); final String content = (String) it.next(); logger.info("Indexing document {}", id); indexer.addDocument(id, content); } LineIterator.closeQuietly(it); logger.info("Commiting all pending documents"); indexer.commit(); } finally { logger.info("Closing index"); indexer.close(); } }
From source file:org.sipfoundry.sipxconfig.admin.X509CertificateUtils.java
/** * Creates a X509 certificate based on the provided file path. Strips any preamble information * from the original certificate (if any) * * @param path// w w w .j a v a2 s. com * @return X509 Certificate or null if cannot create */ public static X509Certificate getX509Certificate(String path) { X509Certificate cert = null; LineIterator it = null; InputStream is = null; try { File sslCertFile = new File(path); it = FileUtils.lineIterator(sslCertFile); StringBuffer rep = new StringBuffer(); boolean shouldWrite = false; while (it.hasNext()) { String line = it.nextLine(); if (line.equals(BEGIN_LINE)) { shouldWrite = true; } if (shouldWrite) { rep.append(line); rep.append(NEW_LINE); } } is = new ByteArrayInputStream(rep.toString().getBytes()); CertificateFactory cf = CertificateFactory.getInstance(X509_TYPE); cert = (X509Certificate) cf.generateCertificate(is); } catch (Exception ex) { cert = null; } finally { LineIterator.closeQuietly(it); IOUtils.closeQuietly(is); } return cert; }
From source file:org.soaplab.clients.InputUtils.java
static byte[][] list2Files(String value) throws IOException { ArrayList<byte[]> result = new ArrayList<byte[]>(); File listFile = new File(value); String fullPath = FilenameUtils.getFullPath(listFile.getAbsolutePath()); LineIterator it = FileUtils.lineIterator(listFile); try {//from w w w. ja v a 2s. c o m while (it.hasNext()) { String line = it.nextLine().trim(); if (StringUtils.isEmpty(line)) continue; // ignore blank lines if (line.startsWith("#")) continue; // ignore comments File aFile = new File(line); try { result.add(FileUtils.readFileToByteArray(aFile)); } catch (IOException e) { // try to add path of the list file if (aFile.isAbsolute()) { throw new IOException("Error when reading [" + line + "]: " + e.getMessage()); } else { File bFile = new File(fullPath, line); result.add(FileUtils.readFileToByteArray(bFile)); } } } } finally { LineIterator.closeQuietly(it); } return result.toArray(new byte[][] {}); }
From source file:org.sonar.dev.TrimMojo.java
private void trimDirectory() throws MojoExecutionException { File[] files = scanFiles();//from ww w . j a va2s . c o m for (File file : files) { StringBuilder sb = new StringBuilder(); try { LineIterator lines = FileUtils.lineIterator(file, sourceEncoding); while (lines.hasNext()) { String line = lines.nextLine(); if (line != null && !"".equals(line.trim())) { sb.append(line.trim()); sb.append(IOUtils.LINE_SEPARATOR); } } FileUtils.writeStringToFile(file, sb.toString(), sourceEncoding); } catch (IOException e) { throw new MojoExecutionException("Can not trim the file " + file, e); } } getLog().info("Trimmed files: " + files.length); }