Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 238   Methods: 9
NCLOC: 79   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
DemuxOutputStream.java 80% 78.9% 66.7% 77.2%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant;
 56   
 
 57   
 import java.io.ByteArrayOutputStream;
 58   
 import java.io.IOException;
 59   
 import java.io.OutputStream;
 60   
 import java.util.Hashtable;
 61   
 
 62   
 /**
 63   
  * Logs content written by a thread and forwards the buffers onto the
 64   
  * project object which will forward the content to the appropriate
 65   
  * task.
 66   
  *
 67   
  * @since 1.4
 68   
  * @author Conor MacNeill
 69   
  */
 70   
 public class DemuxOutputStream extends OutputStream {
 71   
 
 72   
     /**
 73   
      * A data class to store information about a buffer. Such information
 74   
      * is stored on a per-thread basis.
 75   
      */
 76   
     private static class BufferInfo {
 77   
         /**
 78   
          * The per-thread output stream.
 79   
          */
 80   
         private ByteArrayOutputStream buffer;
 81   
         
 82   
         /** 
 83   
          * Whether or not the next line-terminator should be skipped in terms
 84   
          * of processing the buffer. Used to avoid \r\n invoking
 85   
          * processBuffer twice.
 86   
          */
 87   
          private boolean skip = false;
 88   
     }
 89   
     
 90   
     /** Maximum buffer size. */
 91   
     private static final int MAX_SIZE = 1024;
 92   
     
 93   
     /** Mapping from thread to buffer (Thread to BufferInfo). */
 94   
     private Hashtable buffers = new Hashtable();
 95   
 
 96   
     /**
 97   
      * The project to send output to.
 98   
      */
 99   
     private Project project;
 100   
 
 101   
     /**
 102   
      * Whether or not this stream represents an error stream.
 103   
      */
 104   
     private boolean isErrorStream;
 105   
     
 106   
     /**
 107   
      * Creates a new instance of this class.
 108   
      *
 109   
      * @param project The project instance for which output is being 
 110   
      *                demultiplexed. Must not be <code>null</code>.
 111   
      * @param isErrorStream <code>true</code> if this is the error string, 
 112   
      *                      otherwise a normal output stream. This is 
 113   
      *                      passed to the project so it knows
 114   
      *                      which stream it is receiving.
 115   
      */
 116  4
     public DemuxOutputStream(Project project, boolean isErrorStream) {
 117  4
         this.project = project;
 118  4
         this.isErrorStream = isErrorStream;
 119   
     }
 120   
 
 121   
     /**
 122   
      * Returns the buffer associated with the current thread.
 123   
      * 
 124   
      * @return a BufferInfo for the current thread to write data to
 125   
      */
 126  16626
     private BufferInfo getBufferInfo() {
 127  16626
         Thread current = Thread.currentThread();
 128  16626
         BufferInfo bufferInfo = (BufferInfo) buffers.get(current);
 129  16626
         if (bufferInfo == null) {
 130  14
             bufferInfo = new BufferInfo();
 131  14
             bufferInfo.buffer = new ByteArrayOutputStream();
 132  14
             bufferInfo.skip = false;
 133  14
             buffers.put(current, bufferInfo);
 134   
         }
 135  16626
         return bufferInfo;
 136   
     }
 137   
 
 138   
     /**
 139   
      * Resets the buffer for the current thread.
 140   
      */
 141  283
     private void resetBufferInfo() {    
 142  283
         Thread current = Thread.currentThread();
 143  283
         BufferInfo bufferInfo = (BufferInfo) buffers.get(current);
 144  283
         try {
 145  283
             bufferInfo.buffer.close();
 146   
         } catch (IOException e) {
 147   
             // Shouldn't happen
 148   
         }
 149  283
         bufferInfo.buffer = new ByteArrayOutputStream();
 150  283
         bufferInfo.skip = false;
 151   
     }
 152   
     
 153   
     /**
 154   
      * Removes the buffer for the current thread.
 155   
      */
 156  0
     private void removeBuffer() {    
 157  0
         Thread current = Thread.currentThread();
 158  0
         buffers.remove (current);
 159   
     }
 160   
 
 161   
     /**
 162   
      * Writes the data to the buffer and flushes the buffer if a line
 163   
      * separator is detected or if the buffer has reached its maximum size.
 164   
      *
 165   
      * @param cc data to log (byte).
 166   
      * @exception IOException if the data cannot be written to the stream
 167   
      */
 168  13596
     public void write(int cc) throws IOException {
 169  13596
         final byte c = (byte) cc;
 170   
 
 171  13596
         BufferInfo bufferInfo = getBufferInfo();
 172  13596
         if ((c == '\n') || (c == '\r')) {
 173  281
             if (!bufferInfo.skip) {
 174  281
                 processBuffer(bufferInfo.buffer);
 175   
             }
 176   
         } else {
 177  13315
             bufferInfo.buffer.write(cc);
 178  13315
             if (bufferInfo.buffer.size() > MAX_SIZE) {
 179  2
                 processBuffer(bufferInfo.buffer);
 180   
             }
 181   
         }
 182  13596
         bufferInfo.skip = (c == '\r');
 183   
     }
 184   
 
 185   
     /**
 186   
      * Converts the buffer to a string and sends it to the project.
 187   
      *
 188   
      * @param buffer the ByteArrayOutputStream used to collect the output
 189   
      * until a line separator is seen.
 190   
      * 
 191   
      * @see Project#demuxOutput(String,boolean)
 192   
      */
 193  283
     protected void processBuffer(ByteArrayOutputStream buffer) {
 194  283
         String output = buffer.toString();
 195  283
         project.demuxOutput(output, isErrorStream);
 196  283
         resetBufferInfo();
 197   
     }
 198   
 
 199   
     /**
 200   
      * Converts the buffer to a string and sends it to the project.
 201   
      *
 202   
      * @param buffer the ByteArrayOutputStream used to collect the output
 203   
      * until a line separator is seen.
 204   
      * 
 205   
      * @see Project#demuxOutput(String,boolean)
 206   
      */
 207  0
     protected void processFlush(ByteArrayOutputStream buffer) {
 208  0
         String output = buffer.toString();
 209  0
         project.demuxFlush(output, isErrorStream);
 210  0
         resetBufferInfo();
 211   
     }
 212   
 
 213   
     /**
 214   
      * Equivalent to flushing the stream.
 215   
      *
 216   
      * @exception IOException if there is a problem closing the stream.
 217   
      * 
 218   
      * @see #flush
 219   
      */
 220  0
     public void close() throws IOException {
 221  0
         flush();
 222  0
         removeBuffer();
 223   
     }
 224   
 
 225   
     /**
 226   
      * Writes all remaining data in the buffer associated
 227   
      * with the current thread to the project.
 228   
      *
 229   
      * @exception IOException if there is a problem flushing the stream.
 230   
      */
 231  3030
     public void flush() throws IOException {
 232  3030
         BufferInfo bufferInfo = getBufferInfo();
 233  3030
         if (bufferInfo.buffer.size() > 0) {
 234  0
             processFlush(bufferInfo.buffer);
 235   
         }
 236   
     }
 237   
 }
 238