Example usage for org.apache.commons.io IOUtils lineIterator

List of usage examples for org.apache.commons.io IOUtils lineIterator

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils lineIterator.

Prototype

public static LineIterator lineIterator(InputStream input, String encoding) throws IOException 

Source Link

Document

Return an Iterator for the lines in an InputStream, using the character encoding specified (or default encoding if null).

Usage

From source file:org.apache.olingo.client.core.communication.response.batch.ODataBatchResponseManager.java

public ODataBatchResponseManager(final ODataBatchResponse res, final List<ODataBatchResponseItem> expectedItems,
        final boolean continueOnError) {

    this.continueOnError = continueOnError;

    try {//from   www . j  a  v  a 2s.co  m
        this.expectedItemsIterator = expectedItems.iterator();
        this.batchLineIterator = new ODataBatchLineIteratorImpl(
                IOUtils.lineIterator(res.getRawResponse(), Constants.UTF8));

        // search for boundary
        batchBoundary = ODataBatchUtilities.getBoundaryFromHeader(res.getHeader(HttpHeader.CONTENT_TYPE));
        LOG.debug("Retrieved batch response bondary '{}'", batchBoundary);
    } catch (IOException e) {
        LOG.error("Error parsing batch response", e);
        throw new IllegalStateException(e);
    }
}

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;//from www .  j  a  va2  s .c om
    try {
        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;/*from  www  .  jav  a 2s  .c  o m*/
    try {
        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.archive.crawler.migrate.MigrateH1to3Tool.java

protected Map<String, String> getMigrateMap() throws IOException {
    Map<String, String> map = new HashMap<String, String>();
    InputStream inStream = getClass().getResourceAsStream("/org/archive/crawler/migrate/H1toH3.map");
    LineIterator iter = IOUtils.lineIterator(inStream, "UTF-8");
    while (iter.hasNext()) {
        String[] fields = iter.nextLine().split("\\|");
        map.put(fields[1], fields[0]);//from www .  j a v  a  2  s .co  m
    }
    inStream.close();
    return map;
}

From source file:org.carewebframework.maven.plugin.core.ConfigTemplate.java

/**
 * Creates a config file template from a classpath resource.
 * /*from w  w  w.j ava  2  s. co m*/
 * @param filename Path to config file template.
 * @throws MojoExecutionException Error loading the template.
 */
ConfigTemplate(String filename) throws MojoExecutionException {
    this.filename = filename;
    InputStream in = getClass().getResourceAsStream("/" + filename);

    if (in == null) {
        throw new MojoExecutionException("Cannot find config file template: " + filename);
    }

    try {
        LineIterator lines = IOUtils.lineIterator(in, "UTF-8");

        while (lines.hasNext()) {
            String line = lines.next();

            if (line.startsWith("*")) {
                String[] pcs = line.split("\\*", 3);
                entries.put(pcs[1], new ConfigEntry(pcs[2], buffer.size()));
                buffer.add("");
            } else {
                buffer.add(line);
            }
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Unexpected error while creating configuration file.", e);
    } finally {
        IOUtils.closeQuietly(in);
    }

    if (entries.isEmpty()) {
        throw new MojoExecutionException("Failed to locate insertion point in configuration file.");
    }
}

From source file:org.carewebframework.maven.plugin.theme.CSSTransform.java

@Override
public void process(IResource resource, OutputStream outputStream) throws Exception {
    CSSResource css = (CSSResource) resource;
    File mapper = css.getMapper();

    if (mapper == null) {
        super.process(resource, outputStream);
        return;//w ww.j  av  a2s .c  o  m
    }
    InputStream in = new FileInputStream(mapper);
    LineIterator lines = IOUtils.lineIterator(in, "UTF-8");
    String line = "";

    while (lines.hasNext()) {
        line += lines.nextLine();

        if (line.endsWith("\\")) {
            line = StringUtils.left(line, line.length() - 1);
        } else {
            addMapEntry(line);
            line = "";
        }
    }

    addMapEntry(line);
    IOUtils.closeQuietly(in);
    super.process(resource, outputStream);
}

From source file:org.cleverbus.admin.web.msg.MessageLogParser.java

/**
 * Gets lines which corresponds with specified correlation ID from the specified log file.
 *
 * @param logFile the log file//from   w w w.  ja  v  a  2s .c  o  m
 * @param correlationId the correlation ID
 * @return log lines
 * @throws IOException when error occurred during file reading
 */
private List<String> getLogLines(File logFile, String correlationId) throws IOException {
    List<String> logLines = new ArrayList<String>();

    Log.debug("Go through the following log file: " + logFile);

    int year = Calendar.getInstance().get(Calendar.YEAR);
    String[] possibleYears = new String[] { String.valueOf(year - 1), String.valueOf(year) };

    InputStream stream = null;
    try {
        if (logFile.getName().endsWith(GZIP_FILE_EXTENSION)) {
            stream = new GZIPInputStream(new BufferedInputStream(new FileInputStream(logFile)));
        } else {
            stream = new BufferedInputStream(new FileInputStream(logFile));
        }

        LineIterator it = IOUtils.lineIterator(stream, Charset.defaultCharset());

        String requestId = null;
        boolean lastCorrectLine = false; // if previous log line belongs to requestId
        while (it.hasNext()) {
            String line = it.nextLine();

            if (requestId == null) {
                if (StringUtils.contains(line, correlationId)) {
                    logLines.add(formatLogLine(line));

                    // finds requestID
                    requestId = getRequestId(line);

                    if (requestId != null) {
                        Log.debug("correlationId (" + correlationId + ") => requestId (" + requestId + ")");
                    }
                }
            } else {
                // adds lines with requestID and lines that belongs to previous log record (e.g. XML request)
                //  it's better to check also correlationID because it's not one request ID
                //  for all repeated scheduled jobs for processing of partly failed messages

                // 2013-05-23 20:22:36,754 [MACHINE_IS_UNDEFINED, ajp-bio-8009-exec-19, /esb/ws/account/v1, ...
                // <checkCustomerCreditRequest xmlns="http://cleverbus.org/ws/AccountService-v1">
                //    <firstName>csd</firstName>
                //    <lastName>acs</lastName>
                //    <birthNumber>111111/1111</birthNumber>
                // </checkCustomerCreditRequest>

                if (StringUtils.contains(line, requestId) || (StringUtils.contains(line, correlationId))
                        || (lastCorrectLine && !StringUtils.startsWithAny(line, possibleYears))) {
                    logLines.add(formatLogLine(line));
                    lastCorrectLine = true;
                } else {
                    lastCorrectLine = false;
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(stream);
    }

    return logLines;
}

From source file:org.cloudbyexample.dc.agent.command.ImageClientImpl.java

private String convertInputStreamToString(InputStream response) {
    StringWriter logwriter = new StringWriter();

    try {//from   w w w  .  java2  s.  c om
        LineIterator itr = IOUtils.lineIterator(response, "UTF-8");

        while (itr.hasNext()) {
            String line = itr.next();
            logwriter.write(line + (itr.hasNext() ? "\n" : ""));

            logger.info(line);
        }

        return logwriter.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(response);
    }
}

From source file:org.cloudml.connectors.DockerConnector.java

public void BuildImageFromDockerFile(String path) {
    File baseDir = new File(path);

    InputStream response = dockerClient.buildImageCmd(baseDir).exec();

    StringWriter logwriter = new StringWriter();

    try {//from  ww w.ja  va  2 s .c om
        LineIterator itr = IOUtils.lineIterator(response, "UTF-8");
        while (itr.hasNext()) {
            String line = itr.next();
            logwriter.write(line);
            journal.log(Level.INFO, line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(response);
    }
}

From source file:org.codice.ddf.test.common.KarafContainerFailureLogger.java

private void printFileContent(Path rootPath, Path fileRelativePath) {
    File file = rootPath.resolve(fileRelativePath).toFile();

    try (FileInputStream fileInputStream = new FileInputStream(file)) {
        LOGGER.error("===== Printing {} content =====", fileRelativePath.toString());
        IOUtils.lineIterator(fileInputStream, "UTF-8").forEachRemaining(OUT::println);
    } catch (IOException e) {
        LOGGER.error("Couldn't find {}", file.getAbsolutePath());
    }/*from  w w w.j  a va  2 s .  c  o  m*/
}