Clover coverage report - Code Coverage for tapestry release 3.1-alpha-1
Coverage timestamp: Mon Feb 21 2005 09:16:14 EST
file stats: LOC: 297   Methods: 15
NCLOC: 140   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ResponseOutputStream.java 63.6% 67.9% 66.7% 66.7%
coverage coverage
 1   
 // Copyright 2004, 2005 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.tapestry.request;
 16   
 
 17   
 import java.io.IOException;
 18   
 import java.io.OutputStream;
 19   
 import java.net.SocketException;
 20   
 
 21   
 import javax.servlet.http.HttpServletResponse;
 22   
 
 23   
 import org.apache.commons.logging.Log;
 24   
 import org.apache.commons.logging.LogFactory;
 25   
 import org.apache.tapestry.Tapestry;
 26   
 
 27   
 /**
 28   
  *  A special output stream works with a {@link HttpServletResponse}, buffering
 29   
  *  data so as to defer opening the response's output stream.
 30   
  *
 31   
  *  <p>The buffering is pretty simple because the code
 32   
  *  between {@link org.apache.tapestry.IMarkupWriter} and this shows lots of buffering
 33   
  *  after the <code>PrintWriter</code> and inside the <code>OutputStreamWriter</code> that
 34   
  *  can't be configured.
 35   
  *
 36   
  *  <p>This class performs some buffering, but it is not all that
 37   
  *  useful because the 
 38   
  *  {@link org.apache.tapestry.html.Body} component (which will
 39   
  *  be used on virtually all Tapestry pages), buffers its wrapped contents
 40   
  *  (that is, evertyhing inside the &lt;body&gt; tag in the generated HTML).
 41   
  *
 42   
  *  @author Howard Lewis Ship
 43   
  * 
 44   
  **/
 45   
 
 46   
 public class ResponseOutputStream extends OutputStream
 47   
 {
 48   
     private static final Log LOG = LogFactory.getLog(ResponseOutputStream.class);
 49   
 
 50   
     /**
 51   
      *  Default size for the buffer (2000 bytes).
 52   
      *
 53   
      **/
 54   
 
 55   
     public static final int DEFAULT_SIZE = 2000;
 56   
 
 57   
     private int _pos;
 58   
     private int _maxSize;
 59   
     private byte[] _buffer;
 60   
 
 61   
     private String _contentType;
 62   
     private HttpServletResponse _response;
 63   
     private OutputStream _out;
 64   
 
 65   
     private boolean _discard = false;
 66   
 
 67   
     /**
 68   
      *  Creates the stream with the default maximum buffer size.
 69   
      *
 70   
      **/
 71   
 
 72  198
     public ResponseOutputStream(HttpServletResponse response)
 73   
     {
 74  198
         this(response, DEFAULT_SIZE);
 75   
     }
 76   
 
 77   
     /**
 78   
      *  Standard constructor.
 79   
      *
 80   
      **/
 81   
 
 82  198
     public ResponseOutputStream(HttpServletResponse response, int maxSize)
 83   
     {
 84  198
         _response = response;
 85  198
         _maxSize = maxSize;
 86   
     }
 87   
 
 88   
     /**
 89   
      *  Does nothing.  This is because of chaining of <code>close()</code> from
 90   
      *  {@link org.apache.tapestry.IMarkupWriter#close()} ... see {@link #flush()}.
 91   
      * 
 92   
      **/
 93   
 
 94  195
     public void close() throws IOException
 95   
     {
 96   
         // Does nothing.
 97   
     }
 98   
 
 99   
     /**
 100   
      *  Flushes the underlying output stream, if is has been opened.  
 101   
      *
 102   
      *  <p>This method explicitly <em>does not</em> flush the internal buffer ...
 103   
      *  that's because when an {@link org.apache.tapestry.IMarkupWriter} is closed (for instance, because
 104   
      *  an exception is thrown), that <code>close()</code> spawns <code>flush()</code>es
 105   
      *  and <code>close()</code>s throughout the output stream chain, eventually
 106   
      *  reaching this method.
 107   
      *
 108   
      *  @see #forceFlush()
 109   
      *
 110   
      **/
 111   
 
 112  0
     public void flush() throws IOException
 113   
     {
 114  0
         try
 115   
         {
 116  0
             if (_out != null)
 117  0
                 _out.flush();
 118   
         }
 119   
         catch (SocketException ex)
 120   
         {
 121  0
             LOG.debug("Socket exception.");
 122   
         }
 123   
     }
 124   
 
 125   
     /**
 126   
      *  Writes the internal buffer to the output stream, opening it if necessary, then
 127   
      *  flushes the output stream.  Future writes will go directly to the output stream.
 128   
      *
 129   
      **/
 130   
 
 131  187
     public void forceFlush() throws IOException
 132   
     {
 133  187
         if (_out == null)
 134   
         {
 135   
 
 136   
             // In certain cases (such as when the Tapestry service sends a redirect),
 137   
             // there is no output to send back (and no content type set).  In this
 138   
             // case, forceFlush() does nothing.
 139   
 
 140  147
             if (_buffer == null)
 141  5
                 return;
 142   
 
 143  142
             open();
 144   
         }
 145   
 
 146  182
         try
 147   
         {
 148  182
             _out.flush();
 149   
         }
 150   
         catch (SocketException ex)
 151   
         {
 152  0
             LOG.debug("Socket exception.");
 153   
         }
 154   
     }
 155   
 
 156  0
     public String getContentType()
 157   
     {
 158  0
         return _contentType;
 159   
     }
 160   
 
 161  0
     public boolean getDiscard()
 162   
     {
 163  0
         return _discard;
 164   
     }
 165   
 
 166   
     /**
 167   
      *  Sets the response type to from the contentType property (which
 168   
      *  defaults to "text/html") and gets an output stream
 169   
      *  from the response, then writes the current buffer to it and
 170   
      *  releases the buffer.
 171   
      *
 172   
      *  @throws IOException if the content type has never been set.
 173   
      *
 174   
      **/
 175   
 
 176  182
     private void open() throws IOException
 177   
     {
 178  182
         if (_contentType == null)
 179  0
             throw new IOException(Tapestry.getMessage("ResponseOutputStream.content-type-not-set"));
 180   
 
 181  182
         _response.setContentType(_contentType);
 182   
 
 183  182
         _out = _response.getOutputStream();
 184   
 
 185  182
         innerWrite(_buffer, 0, _pos);
 186   
 
 187  182
         _pos = 0;
 188  182
         _buffer = null;
 189   
     }
 190   
 
 191   
     /**
 192   
      *  Discards all output in the buffer.  This is used after an error to
 193   
      *  restart the output (so that the error may be presented).
 194   
      *
 195   
      *  <p>Clears the discard flag.
 196   
      *
 197   
      **/
 198   
 
 199  49
     public void reset() throws IOException
 200   
     {
 201  49
         _pos = 0;
 202  49
         _discard = false;
 203   
     }
 204   
 
 205   
     /**
 206   
      *  Changes the maximum buffer size.  If the new buffer size is smaller
 207   
      *  than the number of
 208   
      *  bytes already in the buffer, the buffer is immediately flushed.
 209   
      *
 210   
      **/
 211   
 
 212  0
     public void setBufferSize(int value) throws IOException
 213   
     {
 214  0
         if (value < _pos)
 215   
         {
 216  0
             open();
 217  0
             return;
 218   
         }
 219   
 
 220  0
         _maxSize = value;
 221   
     }
 222   
 
 223  197
     public void setContentType(String value)
 224   
     {
 225  197
         _contentType = value;
 226   
     }
 227   
 
 228   
     /**
 229   
      *  Indicates whether the stream should ignore all data written to it.
 230   
      *
 231   
      **/
 232   
 
 233  30
     public void setDiscard(boolean value)
 234   
     {
 235  30
         _discard = value;
 236   
     }
 237   
 
 238  222
     private void innerWrite(byte[] b, int off, int len) throws IOException
 239   
     {
 240  222
         if (b == null || len == 0 || _discard)
 241  40
             return;
 242   
 
 243  182
         try
 244   
         {
 245  182
             _out.write(b, off, len);
 246   
         }
 247   
         catch (SocketException ex)
 248   
         {
 249  0
             LOG.debug("Socket exception.");
 250   
         }
 251   
     }
 252   
 
 253  235
     public void write(byte b[], int off, int len) throws IOException
 254   
     {
 255  235
         if (len == 0 || _discard)
 256  15
             return;
 257   
 
 258  220
         if (_out != null)
 259   
         {
 260  38
             _out.write(b, off, len);
 261  38
             return;
 262   
         }
 263   
 
 264   
         // If too large for the maximum size buffer, then open the output stream
 265   
         // write out and free the buffer, and write out the new stuff.
 266   
 
 267  182
         if (_pos + len >= _maxSize)
 268   
         {
 269  40
             open();
 270  40
             innerWrite(b, off, len);
 271  40
             return;
 272   
         }
 273   
 
 274   
         // Allocate the buffer when it is initially needed.
 275   
 
 276  142
         if (_buffer == null)
 277  142
             _buffer = new byte[_maxSize];
 278   
 
 279   
         // Copy the new bytes into the buffer and advance the position.
 280   
 
 281  142
         System.arraycopy(b, off, _buffer, _pos, len);
 282  142
         _pos += len;
 283   
     }
 284   
 
 285  0
     public void write(int b) throws IOException
 286   
     {
 287  0
         if (_discard)
 288  0
             return;
 289   
 
 290   
         // This method is rarely called so this little inefficiency is better than
 291   
         // maintaining that ugly buffer expansion code in two places.
 292   
 
 293  0
         byte[] tiny = new byte[] {(byte) b };
 294   
 
 295  0
         write(tiny, 0, 1);
 296   
     }
 297   
 }