Coverage Report - org.apache.maven.it.util.cli.StreamFeeder
 
Classes in this File Line Coverage Branch Coverage Complexity
StreamFeeder
0%
0/36
0%
0/8
2.2
 
 1  
 package org.apache.maven.it.util.cli;
 2  
 
 3  
 /*
 4  
  * The MIT License
 5  
  *
 6  
  * Copyright (c) 2004, The Codehaus
 7  
  *
 8  
  * Permission is hereby granted, free of charge, to any person obtaining a copy of
 9  
  * this software and associated documentation files (the "Software"), to deal in
 10  
  * the Software without restriction, including without limitation the rights to
 11  
  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 12  
  * of the Software, and to permit persons to whom the Software is furnished to do
 13  
  * so, subject to the following conditions:
 14  
  *
 15  
  * The above copyright notice and this permission notice shall be included in all
 16  
  * copies or substantial portions of the Software.
 17  
  *
 18  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 24  
  * SOFTWARE.
 25  
  */
 26  
 
 27  
 import java.io.IOException;
 28  
 import java.io.InputStream;
 29  
 import java.io.OutputStream;
 30  
 
 31  
 /**
 32  
  * Read from an InputStream and write the output to an OutputStream.
 33  
  *
 34  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 35  
  * @version $Id: StreamFeeder.java 467764 2006-10-25 21:04:07Z jvanzyl $
 36  
  */
 37  
 public class StreamFeeder
 38  
     extends Thread
 39  
 {
 40  
     private InputStream input;
 41  
 
 42  
     private OutputStream output;
 43  
 
 44  
     private boolean done;
 45  
 
 46  
     /**
 47  
      * Create a new StreamFeeder
 48  
      *
 49  
      * @param input  Stream to read from
 50  
      * @param output Stream to write to
 51  
      */
 52  
     public StreamFeeder( InputStream input, OutputStream output )
 53  0
     {
 54  0
         this.input = input;
 55  
 
 56  0
         this.output = output;
 57  0
     }
 58  
 
 59  
     // ----------------------------------------------------------------------
 60  
     // Runnable implementation
 61  
     // ----------------------------------------------------------------------
 62  
 
 63  
     public void run()
 64  
     {
 65  
         try
 66  
         {
 67  0
             feed();
 68  
         }
 69  0
         catch ( Throwable ex )
 70  
         {
 71  
             // Catched everything so the streams will be closed and flagged as done.
 72  
         }
 73  
         finally
 74  
         {
 75  0
             close();
 76  
 
 77  0
             done = true;
 78  
 
 79  0
             synchronized ( this )
 80  
             {
 81  0
                 this.notifyAll();
 82  0
             }
 83  0
         }
 84  0
     }
 85  
 
 86  
     // ----------------------------------------------------------------------
 87  
     //
 88  
     // ----------------------------------------------------------------------
 89  
 
 90  
     public void close()
 91  
     {
 92  0
         if ( input != null )
 93  
         {
 94  0
             synchronized ( input )
 95  
             {
 96  
                 try
 97  
                 {
 98  0
                     input.close();
 99  
                 }
 100  0
                 catch ( IOException ex )
 101  
                 {
 102  
                     // ignore
 103  0
                 }
 104  
 
 105  0
                 input = null;
 106  0
             }
 107  
         }
 108  
 
 109  0
         if ( output != null )
 110  
         {
 111  0
             synchronized ( output )
 112  
             {
 113  
                 try
 114  
                 {
 115  0
                     output.close();
 116  
                 }
 117  0
                 catch ( IOException ex )
 118  
                 {
 119  
                     // ignore
 120  0
                 }
 121  
 
 122  0
                 output = null;
 123  0
             }
 124  
         }
 125  0
     }
 126  
 
 127  
     public boolean isDone()
 128  
     {
 129  0
         return done;
 130  
     }
 131  
 
 132  
     // ----------------------------------------------------------------------
 133  
     //
 134  
     // ----------------------------------------------------------------------
 135  
 
 136  
     private void feed()
 137  
         throws IOException
 138  
     {
 139  0
         int data = input.read();
 140  
 
 141  0
         while ( !done && data != -1 )
 142  
         {
 143  0
             synchronized ( output )
 144  
             {
 145  0
                 output.write( data );
 146  
 
 147  0
                 data = input.read();
 148  0
             }
 149  
         }
 150  0
     }
 151  
 }