Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 221   Methods: 9
NCLOC: 106   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
StringInputStream.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002 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   
 package org.apache.tools.ant.filters;
 55   
 
 56   
 import java.io.IOException;
 57   
 import java.io.InputStream;
 58   
 import java.io.StringReader;
 59   
 
 60   
 /**
 61   
  * Wraps a String as an InputStream.
 62   
  *
 63   
  * @author Magesh Umasankar
 64   
  */
 65   
 public class StringInputStream
 66   
     extends InputStream {
 67   
     /** Source string, stored as a StringReader */
 68   
     private StringReader in;
 69   
 
 70   
     private String encoding;
 71   
 
 72   
     private byte[] slack;
 73   
 
 74   
     private int begin;
 75   
 
 76   
     /**
 77   
      * Composes a stream from a String
 78   
      *
 79   
      * @param source The string to read from. Must not be <code>null</code>.
 80   
      */
 81  0
     public StringInputStream(String source) {
 82  0
         in = new StringReader(source);
 83   
     }
 84   
 
 85   
     /**
 86   
      * Composes a stream from a String with the specified encoding
 87   
      *
 88   
      * @param source The string to read from. Must not be <code>null</code>.
 89   
      * @param encoding The encoding scheme.
 90   
      */
 91  0
     public StringInputStream(String source, String encoding) {
 92  0
         in = new StringReader(source);
 93  0
         this.encoding = encoding;
 94   
     }
 95   
 
 96   
     /**
 97   
      * Reads from the Stringreader, returning the same value.
 98   
      *
 99   
      * @return the value of the next character in the StringReader
 100   
      *
 101   
      * @exception IOException if the original StringReader fails to be read
 102   
      */
 103  0
     public synchronized int read() throws IOException {
 104  0
         if (in == null) {
 105  0
             throw new IOException("Stream Closed");
 106   
         }
 107   
 
 108  0
         byte result;
 109  0
         if (slack != null && begin < slack.length) {
 110  0
             result = slack[begin];
 111  0
             if (++begin == slack.length) {
 112  0
                 slack = null;
 113   
             }
 114   
         } else {
 115  0
             byte[] buf = new byte[1];
 116  0
             if (read(buf, 0, 1) <= 0) {
 117  0
                 return -1;
 118   
             }
 119  0
             result = buf[0];
 120   
         }
 121  0
         if (result < 0) {
 122  0
             return 256 + result;
 123   
         } else {
 124  0
             return result;
 125   
         }
 126   
     }
 127   
 
 128  0
     public synchronized int read(byte[] b, int off, int len)
 129   
         throws IOException {
 130   
 
 131  0
         if (in == null) {
 132  0
             throw new IOException("Stream Closed");
 133   
         }
 134   
 
 135  0
         while (slack == null) {
 136  0
             char[] buf = new char[len]; // might read too much
 137  0
             int n = in.read(buf);
 138  0
             if (n == -1) {
 139  0
                 return -1;
 140   
             }
 141  0
             if (n > 0) {
 142  0
                 String s = new String(buf, 0, n);
 143  0
                 if (encoding == null) {
 144  0
                     slack = s.getBytes();
 145   
                 } else {
 146  0
                     slack = s.getBytes(encoding);
 147   
                 }
 148  0
                 begin = 0;
 149   
             }
 150   
         }
 151   
 
 152  0
         if (len > slack.length - begin) {
 153  0
             len = slack.length - begin;
 154   
         }
 155   
 
 156  0
         System.arraycopy(slack, begin, b, off, len);
 157   
 
 158  0
         if ((begin += len) >= slack.length) {
 159  0
             slack = null;
 160   
         }
 161  0
         return len;
 162   
     }
 163   
 
 164   
     /**
 165   
      * Marks the read limit of the StringReader.
 166   
      *
 167   
      * @param limit the maximum limit of bytes that can be read before the
 168   
      *              mark position becomes invalid
 169   
      */
 170  0
     public synchronized void mark(final int limit) {
 171  0
         try {
 172  0
             in.mark(limit);
 173   
         } catch (IOException ioe) {
 174  0
             throw new RuntimeException(ioe.getMessage());
 175   
         }
 176   
     }
 177   
 
 178   
 
 179  0
     public synchronized int available() throws IOException {
 180  0
         if (in == null) {
 181  0
             throw new IOException("Stream Closed");
 182   
         }
 183  0
         if (slack != null) {
 184  0
             return slack.length - begin;
 185   
         }
 186  0
         if (in.ready()) {
 187  0
             return 1;
 188   
         } else {
 189  0
             return 0;
 190   
         }
 191   
     }
 192   
 
 193  0
     public boolean markSupported () {
 194  0
         return false;   // would be imprecise
 195   
     }
 196   
 
 197   
     /**
 198   
      * Resets the StringReader.
 199   
      *
 200   
      * @exception IOException if the StringReader fails to be reset
 201   
      */
 202  0
     public synchronized void reset() throws IOException {
 203  0
         if (in == null) {
 204  0
             throw new IOException("Stream Closed");
 205   
         }
 206  0
         slack = null;
 207  0
         in.reset();
 208   
     }
 209   
 
 210   
     /**
 211   
      * Closes the Stringreader.
 212   
      *
 213   
      * @exception IOException if the original StringReader fails to be closed
 214   
      */
 215  0
     public synchronized void close() throws IOException {
 216  0
         in.close();
 217  0
         slack = null;
 218  0
         in = null;
 219   
     }
 220   
 }
 221