All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.technophobia.substeps.runner.InputStreamConsumer Maven / Gradle / Ivy

The newest version!
/*
 *  Copyright Technophobia Ltd 2012
 *
 *   This file is part of Substeps.
 *
 *    Substeps is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    Substeps is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with Substeps.  If not, see .
 */
package com.technophobia.substeps.runner;

import org.apache.maven.plugin.logging.Log;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;

class InputStreamConsumer implements Runnable {

    private final Log logger;

    private final InputStream stderr;


    public InputStreamConsumer(final InputStream stderr, final Log logger) {

        this.logger = logger;
        this.stderr = stderr;
    }


    void closeQuietly(final Closeable closeable) {

        if (closeable != null) {

            try {
                closeable.close();
            } catch (final IOException e) {
                logger.error("IOException closing", e);
            }
        }
    }


    /**
     *
     */
    public void closeStreams() {
        closeQuietly(this.stderr);
    }


    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {

        String line = null;
        try {


            int c;
            StringBuilder buf = new StringBuilder();
            while ((c = this.stderr.read()) != -1) {

                String s = String.valueOf((char) c);

                if ((char) c == '\n') {
                    line = buf.toString();

                    buf = new StringBuilder();
                    logger.info("*\t" + line);
                } else {
                    buf.append(s);
                }
            }

        } catch (final IOException e) {
            logger.error("error handling output streams", e);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy