001package org.apache.maven.wagon.providers.ssh;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.wagon.Streams;
023
024import java.io.BufferedReader;
025import java.io.IOException;
026
027/**
028 * CommandExecutorStreamProcessor 
029 *
030 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
031 *
032 */
033public class CommandExecutorStreamProcessor
034{
035    private CommandExecutorStreamProcessor()
036    {
037        // shoo!
038    }
039
040    public static Streams processStreams( BufferedReader stderrReader, BufferedReader stdoutReader )
041        throws IOException
042    {
043        Streams streams = new Streams();
044
045        while ( true )
046        {
047            String line = stderrReader.readLine();
048
049            if ( line == null )
050            {
051                break;
052            }
053
054            // TODO: I think we need to deal with exit codes instead, but IIRC there are some cases of errors that
055            // don't have exit codes ignore this error. TODO: output a warning
056            if ( !line.startsWith( "Could not chdir to home directory" )
057                 && !line.endsWith( "ttyname: Operation not supported" ) )
058            {
059                streams.setErr( streams.getErr() + line + "\n" );
060            }
061        }
062
063        while ( true )
064        {
065            String line = stdoutReader.readLine();
066
067            if ( line == null )
068            {
069                break;
070            }
071
072            streams.setOut( streams.getOut() + line + "\n" );
073        }
074
075        // drain the output stream.
076        // TODO: we'll save this for the 1.0-alpha-8 line, so we can test it more. the -q arg in the
077        // unzip command should keep us until then...
078//            int avail = in.available();
079//            byte[] trashcan = new byte[1024];
080//
081//            while( ( avail = in.available() ) > 0 )
082//            {
083//                in.read( trashcan, 0, avail );
084//            }
085
086        return streams;
087    }
088}