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

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

Introduction

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

Prototype

public void close() 

Source Link

Document

Closes the underlying Reader quietly.

Usage

From source file:nl.opengeogroep.safetymaps.server.admin.stripes.LayerActionBean.java

@Before(stages = LifecycleStage.BindingAndValidation)
private void findMapfiles() {
    try {/*  ww  w.j  a v a2s  .c  o m*/
        JSONArray a = new JSONArray();
        File search = Cfg.getPath("static_mapserver_searchdirs");

        if (search != null) {
            for (File f : FileUtils.listFiles(search, new String[] { "map" }, true)) {
                JSONObject m = new JSONObject();
                m.put("path", f.getPath().substring(search.getPath().length() + 1));
                a.put(m);

                // Naive mapfile parser. Perhaps replace by JavaScript client-side
                // GetCap parsing
                JSONArray l = new JSONArray();
                m.put("layers", l);
                LineIterator it = FileUtils.lineIterator(f, "US-ASCII");
                try {
                    while (it.hasNext()) {
                        String line = it.nextLine().trim();
                        if (line.equals("LAYER")) {
                            String n = it.nextLine().trim();
                            n = n.substring(6, n.length() - 1);
                            l.put(n);
                        }
                    }
                } finally {
                    it.close();
                }
            }
        }
        mapFilesJson = a.toString(4);
    } catch (Exception e) {
    }
}

From source file:nl.ru.cmbi.vase.tools.util.ToolBox.java

public static StringBuilder getStringBuilderFromFile(final String fileName) throws IOException {
    // int STRINGBUILDER_SIZE = 403228383; // big!
    File file = new File(fileName);
    LineIterator it = FileUtils.lineIterator(file);
    StringBuilder sb = new StringBuilder(); // STRINGBUILDER_SIZE
    while (it.hasNext()) {
        sb.append(it.nextLine());//from  ww w  . j ava  2 s  . co  m
        sb.append("\n");
    }
    it.close();
    return sb;
}

From source file:org.apache.jackrabbit.oak.upgrade.blob.LengthCachingDataStore.java

private static Map<String, Long> loadMappingData(File mappingFile) throws FileNotFoundException {
    Map<String, Long> mapping = new HashMap<String, Long>();
    log.info("Reading mapping data from {}", mappingFile.getAbsolutePath());
    LineIterator itr = new LineIterator(Files.newReader(mappingFile, Charsets.UTF_8));
    try {/*from ww  w  . j a  v  a 2  s .c om*/
        while (itr.hasNext()) {
            String line = itr.nextLine();
            int indexOfBar = line.indexOf(SEPARATOR);
            checkState(indexOfBar > 0, "Malformed entry found [%s]", line);
            String length = line.substring(0, indexOfBar);
            String id = line.substring(indexOfBar + 1);
            mapping.put(id.trim(), Long.valueOf(length));
        }
        log.info("Total {} mapping entries found", mapping.size());
    } finally {
        itr.close();
    }
    return mapping;
}

From source file:org.apache.marmotta.loader.rio.GeonamesParser.java

/**
 * Parses the data from the supplied InputStream, using the supplied baseURI
 * to resolve any relative URI references.
 *
 * @param in      The InputStream from which to read the data.
 * @param baseURI The URI associated with the data in the InputStream.
 * @throws java.io.IOException                 If an I/O error occurred while data was read from the InputStream.
 * @throws org.openrdf.rio.RDFParseException   If the parser has found an unrecoverable parse error.
 * @throws org.openrdf.rio.RDFHandlerException If the configured statement handler has encountered an
 *                                             unrecoverable error.
 *//*www  .  j av a2  s. c  o m*/
@Override
public void parse(InputStream in, String baseURI) throws IOException, RDFParseException, RDFHandlerException {
    LineIterator it = IOUtils.lineIterator(in, RDFFormat.RDFXML.getCharset());
    try {
        while (it.hasNext()) {
            lineNumber++;

            String line = it.nextLine();
            if (lineNumber % 2 == 0) {
                // only odd line numbers contain triples
                StringReader buffer = new StringReader(line);
                lineParser.parse(buffer, baseURI);
            }
        }
    } finally {
        it.close();
    }
}

From source file:org.apache.marmotta.loader.rio.GeonamesParser.java

/**
 * Parses the data from the supplied Reader, using the supplied baseURI to
 * resolve any relative URI references.// w w w.  j  a v  a2s .  co m
 *
 * @param reader  The Reader from which to read the data.
 * @param baseURI The URI associated with the data in the InputStream.
 * @throws java.io.IOException                 If an I/O error occurred while data was read from the InputStream.
 * @throws org.openrdf.rio.RDFParseException   If the parser has found an unrecoverable parse error.
 * @throws org.openrdf.rio.RDFHandlerException If the configured statement handler has encountered an
 *                                             unrecoverable error.
 */
@Override
public void parse(Reader reader, String baseURI) throws IOException, RDFParseException, RDFHandlerException {
    LineIterator it = IOUtils.lineIterator(reader);
    try {
        while (it.hasNext()) {
            lineNumber++;

            String line = it.nextLine();
            if (lineNumber % 2 == 1) {
                // only odd line numbers contain triples
                StringReader buffer = new StringReader(line);
                lineParser.parse(buffer, baseURI);
            }
        }
    } finally {
        it.close();
    }
}

From source file:org.apache.marmotta.platform.core.services.prefix.PrefixCC.java

@Override
public String getNamespace(final String prefix) {
    HttpGet get = new HttpGet(URI + prefix + ".file.txt");
    HttpRequestUtil.setUserAgentString(get, USER_AGENT);
    get.setHeader(ACCEPT, "text/plain");
    try {//  ww  w  .  j  av  a  2  s .  c o  m
        return httpClientService.execute(get, new ResponseHandler<String>() {

            @Override
            public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
                if (200 == response.getStatusLine().getStatusCode()) {
                    HttpEntity entity = response.getEntity();

                    final LineIterator it = IOUtils.lineIterator(entity.getContent(), Charset.defaultCharset());
                    try {
                        while (it.hasNext()) {
                            final String l = it.next();
                            if (l.startsWith(prefix + "\t")) {
                                return l.substring(prefix.length() + 1);
                            }
                        }
                    } finally {
                        it.close();
                    }
                }
                log.error("Error: prefix '" + prefix + "' not found at prefix.cc");
                return null;
            }
        });
    } catch (Exception e) {
        log.error("Error retrieving prefix '" + prefix + "' from prefix.cc: " + e.getMessage());
        return null;
    }
}

From source file:org.apache.marmotta.platform.core.services.prefix.PrefixCC.java

@Override
public String getPrefix(final String namespace) {
    try {/*  w w w. java 2  s. com*/
        HttpGet get = new HttpGet(URI + "reverse?format=txt&uri=" + URLEncoder.encode(namespace, "utf-8"));
        HttpRequestUtil.setUserAgentString(get, USER_AGENT);
        get.setHeader(ACCEPT, "text/plain");

        return httpClientService.execute(get, new ResponseHandler<String>() {

            @Override
            public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
                if (200 == response.getStatusLine().getStatusCode()) {
                    HttpEntity entity = response.getEntity();

                    final LineIterator it = IOUtils.lineIterator(entity.getContent(), Charset.defaultCharset());
                    try {
                        while (it.hasNext()) {
                            final String l = it.next();
                            if (l.endsWith("\t" + namespace)) {
                                return l.substring(0, l.indexOf("\t"));
                            }
                        }
                    } finally {
                        it.close();
                    }
                }
                log.error("Error: reverse namespace lookup for '" + namespace + "' not found at prefix.cc");
                return null;
            }
        });
    } catch (Exception e) {
        log.error("Error trying to retrieve prefic.cc reverse lookup for namespace '" + namespace + "': "
                + e.getMessage());
        return null;
    }
}

From source file:org.apache.tez.history.ATSImportTool.java

private void logErrorMessage(ClientResponse response) throws IOException {
    LOG.error("Response status={}", response.getClientResponseStatus().toString());
    LineIterator it = null;
    try {//  ww w  .j a  v a 2  s.  c  o  m
        it = IOUtils.lineIterator(response.getEntityInputStream(), UTF8);
        while (it.hasNext()) {
            String line = it.nextLine();
            LOG.error(line);
        }
    } finally {
        if (it != null) {
            it.close();
        }
    }
}

From source file:org.apache.tez.history.ATSImportTool_V2.java

private String logErrorMessage(ClientResponse response) throws IOException {
    StringBuilder sb = new StringBuilder();
    LOG.error("Response status={}", response.getClientResponseStatus().toString());
    LineIterator it = null;
    try {/*from  w ww  . j a v  a2  s .c o  m*/
        it = IOUtils.lineIterator(response.getEntityInputStream(), UTF8);
        while (it.hasNext()) {
            String line = it.nextLine();
            LOG.error(line);
        }
    } finally {
        if (it != null) {
            it.close();
        }
    }
    return sb.toString();
}

From source file:org.canova.api.records.reader.impl.LineRecordReader.java

@Override
public void close() throws IOException {
    if (iter != null) {
        if (iter instanceof LineIterator) {
            LineIterator iter2 = (LineIterator) iter;
            iter2.close();
        }/*from  ww w. ja  va 2  s  .  c  om*/
    }
}