uk.co.unclealex.process.gobblers.StreamGobbler.java Source code

Java tutorial

Introduction

Here is the source code for uk.co.unclealex.process.gobblers.StreamGobbler.java

Source

/**
 * Copyright 2012 Alex Jones
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 *
 */
package uk.co.unclealex.process.gobblers;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.Callable;

import uk.co.unclealex.process.ProcessCallback;

import com.google.common.io.Closeables;

/**
 * A stream gobbler is used to consume output from a {@link Process} and push it to a {@link ProcessCallback}.
 * @author alex
 *
 */
public abstract class StreamGobbler implements Callable<String>, Closeable {

    /**
     * The {@link InputStream} of the {@link Process} to consume.
     */
    private final InputStream inputStream;

    /**
     * The {@link ProcessCallback} to which output should be pushed.
     */
    private final ProcessCallback processCallback;

    /**
     * The name to set the current thread running this gobbler.
     */
    private final String threadName;

    /**
     * Instantiates a new stream gobbler.
     * 
     * @param inputStream
     *          the input stream
     * @param type
     *          the type
     * @param threadName
     *          the thread name
     * @param processCallback
     *          the process callback
     * @throws IOException
     *           Signals that an I/O exception has occurred.
     */
    protected StreamGobbler(InputStream inputStream, String type, String threadName,
            ProcessCallback processCallback) throws IOException {
        super();
        this.inputStream = inputStream;
        this.processCallback = processCallback;
        this.threadName = type + " of " + threadName;
    }

    /**
     * Consume the output of a stream and push it to the {@link ProcessCallback}.
     * 
     * @return The entire output of the stream.
     * @throws IOException
     *           Signals that an I/O exception has occurred.
     */
    @Override
    public String call() throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        Thread.currentThread().setName(getThreadName());
        BufferedReader reader = new BufferedReader(new InputStreamReader(getInputStream()));
        try {
            String line;
            ProcessCallback processCallback = getProcessCallback();
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line).append('\n');
                executeCallback(processCallback, line);
            }
        } finally {
            Closeables.closeQuietly(reader);
        }
        return stringBuilder.toString();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void close() throws IOException {
        Closeables.closeQuietly(getInputStream());
    }

    /**
     * Execute callback.
     * 
     * @param processCallback
     *          the process callback
     * @param line
     *          the line
     */
    protected abstract void executeCallback(ProcessCallback processCallback, String line);

    /**
     * Gets the {@link InputStream} of the {@link Process} to consume.
     * 
     * @return the {@link InputStream} of the {@link Process} to consume
     */
    public InputStream getInputStream() {
        return inputStream;
    }

    /**
     * Gets the {@link ProcessCallback} to which output should be pushed.
     * 
     * @return the {@link ProcessCallback} to which output should be pushed
     */
    public ProcessCallback getProcessCallback() {
        return processCallback;
    }

    /**
     * Gets the name to set the current thread running this gobbler.
     * 
     * @return the name to set the current thread running this gobbler
     */
    public String getThreadName() {
        return threadName;
    }
}