Example usage for org.apache.commons.io LineIterator closeQuietly

List of usage examples for org.apache.commons.io LineIterator closeQuietly

Introduction

In this page you can find the example usage for org.apache.commons.io LineIterator closeQuietly.

Prototype

public static void closeQuietly(LineIterator iterator) 

Source Link

Document

Closes the iterator, handling null and ignoring exceptions.

Usage

From source file:org.orbisgis.view.toc.actions.cui.legend.components.ColorScheme.java

/**
 * Fills the inner collections using the dedicated resource file.
 *///w  ww . j ava 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.
 *///  ww  w .  ja v a  2 s  .com
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.paxle.gui.impl.servlets.ParserTestServlet.java

@Override
protected void requestCleanup(HttpServletRequest request, HttpServletResponse response, Context context) {
    super.requestCleanup(request, response, context);

    // cleanup the command
    try {/*from   w ww. ja v  a  2  s.co  m*/
        final ICommand cmd = (ICommand) request.getAttribute(REQ_ATTR_CMD);
        if (cmd != null) {
            // cleanup the crawler-doc temp-file
            final ICrawlerDocument cDoc = cmd.getCrawlerDocument();
            if (cDoc != null) {
                final File cDocContentFile = cDoc.getContent();
                if (cDocContentFile != null) {
                    this.tfm.releaseTempFile(cDocContentFile);
                }
            }

            // cleanup the parser-doc temp-file
            final IParserDocument pDoc = cmd.getParserDocument();
            if (pDoc != null) {
                final File pDocContentFile = pDoc.getTextFile();
                if (pDocContentFile != null) {
                    this.tfm.releaseTempFile(pDocContentFile);
                }
            }
        }
    } catch (Throwable e) {
        this.logger.error(e);
    }

    // cleanup line iterators
    @SuppressWarnings("unchecked")
    final List<LineIterator> iterators = (List<LineIterator>) request.getAttribute(REQ_ATTR_LINEITERS);
    if (iterators != null) {
        for (LineIterator iter : iterators) {
            LineIterator.closeQuietly(iter);
        }
    }
}

From source file:org.pentaho.metadata.query.model.util.CsvDataReader.java

public List<List<String>> loadData() {
    String line = null;//from w ww.  ja v  a2s.  c  om
    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.sindice.siren.demo.bnb.BNBDemo.java

public void index() throws IOException {
    final SimpleIndexer indexer = new SimpleIndexer(indexDir);
    try {/*from   ww  w. j a  v a 2s .  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/*from www  .  j  a  va  2 s.  c  om*/
 * @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  ww w  . j  a  v  a 2 s  .  c  om*/
        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.plugins.codesize.LineCounter.java

private int countFile(File file) {

    int lines = 0;
    LineIterator lineIterator = null;// www .  ja  v  a2 s . com

    try {
        lineIterator = FileUtils.lineIterator(file, defaultCharset.name());

        for (; lineIterator.hasNext(); lineIterator.next()) {
            lines++;
        }
    } catch (IOException e) {
        LOG.error("Could not open file", e);
    } finally {
        LineIterator.closeQuietly(lineIterator);
    }
    LOG.debug(file.getName() + ": " + lines);
    return lines;
}

From source file:org.sonar.plugins.xaml.XamlLineCouterSensor.java

private void addMeasures(SensorContext sensorContext, File file, org.sonar.api.resources.File xmlFile) {

    LineIterator iterator = null;//from w  w w  .j a v a  2 s .c  o  m
    int numLines = 0;
    int numEmptyLines = 0;

    try {
        iterator = FileUtils.lineIterator(file);

        while (iterator.hasNext()) {
            String line = iterator.nextLine();
            numLines++;
            if (StringUtils.isEmpty(line)) {
                numEmptyLines++;
            }
        }
    } catch (IOException e) {
        LOG.warn(e.getMessage());
    } finally {
        LineIterator.closeQuietly(iterator);
    }

    try {

        Log.debug("Count comment in " + file.getPath());

        int numCommentLines = new XamlLineCountParser().countLinesOfComment(FileUtils.openInputStream(file));
        sensorContext.saveMeasure(xmlFile, CoreMetrics.LINES, (double) numLines);
        sensorContext.saveMeasure(xmlFile, CoreMetrics.COMMENT_LINES, (double) numCommentLines);
        sensorContext.saveMeasure(xmlFile, CoreMetrics.NCLOC,
                (double) numLines - numEmptyLines - numCommentLines);
    } catch (Exception e) {
        LOG.debug("Fail to count lines in " + file.getPath(), e);
    }

    LOG.debug("LineCountSensor: " + xmlFile.getKey() + ":" + numLines + "," + numEmptyLines + "," + 0);
}

From source file:org.sonar.plugins.xml.LineCountSensor.java

private void addMeasures(SensorContext sensorContext, File file, org.sonar.api.resources.File xmlFile) {

    LineIterator iterator = null;//  w  w w. ja v a 2s. c o  m
    int numLines = 0;
    int numEmptyLines = 0;

    try {
        iterator = FileUtils.lineIterator(file);

        while (iterator.hasNext()) {
            String line = iterator.nextLine();
            numLines++;
            if (StringUtils.isEmpty(line)) {
                numEmptyLines++;
            }
        }
    } catch (IOException e) {
        LOG.warn(e.getMessage());
    } finally {
        LineIterator.closeQuietly(iterator);
    }

    try {

        Log.debug("Count comment in " + file.getPath());

        int numCommentLines = new LineCountParser().countLinesOfComment(FileUtils.openInputStream(file));
        sensorContext.saveMeasure(xmlFile, CoreMetrics.LINES, (double) numLines);
        sensorContext.saveMeasure(xmlFile, CoreMetrics.COMMENT_LINES, (double) numCommentLines);
        sensorContext.saveMeasure(xmlFile, CoreMetrics.NCLOC,
                (double) numLines - numEmptyLines - numCommentLines);
    } catch (Exception e) {
        LOG.debug("Fail to count lines in " + file.getPath(), e);
    }

    LOG.debug("LineCountSensor: " + xmlFile.getKey() + ":" + numLines + "," + numEmptyLines + "," + 0);
}