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

org.ow2.util.protocol.impl.StreamReader Maven / Gradle / Ivy

/**
 * Copyright 2010-2012 Bull S.A.S.
 *
 * Licensed 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 org.ow2.util.protocol.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.ow2.util.file.FileUtils;

/**
 * @author Loris Bouzonnet
 */
public class StreamReader implements Runnable {

    /**
     * Logger.
     */
    private static Logger     logger =
                                             Logger.getLogger(StreamReader.class.getName());

    private final InputStream inputStream;

    private final String      type;

    private final boolean     error;

    private final OutputStream outputStream;

    /**
     * Reads the stream with the logger.
     * @param inputStream
     * @param type
     * @param error true if the stream is related to an error
     */
    public StreamReader(final InputStream inputStream, final String type,
            final boolean error) {
        this.inputStream = inputStream;
        this.error = error;
        this.type = type;
        this.outputStream = null;
    }

    /**
     * Reads the stream into an other stream.
     * @param inputStream
     * @param outputStream
     */
    public StreamReader(final InputStream inputStream, final OutputStream outputStream) {
        this.inputStream = inputStream;
        this.outputStream = outputStream;
        this.error = false;
        this.type = null;
    }

    public void run() {

        if (this.outputStream == null) {

            // Log -> prefer reader
            // TODO: Which charset?
            final InputStreamReader isr = new InputStreamReader(this.inputStream);
            final BufferedReader br = new BufferedReader(isr);

            String line = null;
            try {
                while ((line = br.readLine()) != null) {

                    if (this.error) {
                        logger.severe(this.type + "> " + line);
                    } else {
                        logger.info(this.type + "> " + line);
                    }
                }
            } catch (final IOException e) {
                logger.log(Level.SEVERE, "Error when reading a new line", e);
            } finally {
                FileUtils.close(br, this.inputStream);
            }
        } else {

            // Redirect -> keep bytes
            int readBytes;
            final byte[] bytes = new byte[512];
            try {
                while ((readBytes = this.inputStream.read(bytes)) > 0) {
                    this.outputStream.write(bytes, 0, readBytes);
                }
            } catch (final IOException e) {
                logger.log(Level.SEVERE, "Error when reading/writing some bytes", e);
            }
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy