File Source: CachedContent.java

     1  
     2  package org.apache.roller.weblogger.util.cache;
     3  
     4  import java.io.ByteArrayOutputStream;
     5  import java.io.IOException;
     6  import java.io.OutputStreamWriter;
     7  import java.io.PrintWriter;
     8  import java.io.Serializable;
     9  import java.io.UnsupportedEncodingException;
    10  import org.apache.commons.logging.Log;
    11  import org.apache.commons.logging.LogFactory;
    12  
    13  
    14  /**
    15   * A utility class for storing cached content written to a java.io.Writer.
    16   */
    17  public class CachedContent implements Serializable {
    18      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.util.cache.CachedContent__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    19      private static Log log = LogFactory.getLog(CachedContent.class);
    20      
    21      // default to an 8K buffered cache
    22      public static final int DEFAULT_SIZE = 8192;
    23      
    24      // the byte array we use to maintain the cached content
    25      private byte[] content = new byte[0];
    26      
    27      // content-type of data in byte array
    28      private String contentType = null;
    29      
    30      // Use a byte array output stream to cached the output bytes
    31      private transient ByteArrayOutputStream outstream = null;
    32      
    33      // The PrintWriter that users will be writing to
    34      private transient PrintWriter cachedWriter = null;
    35      
    36      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.util.cache.CachedContent(int)
              * 
              *  Postconditions:
              *    this.cachedWriter == &new PrintWriter(CachedContent#4)
              *    this.content == &new byte[](CachedContent#1)
              *    this.contentType == null
              *    this.outstream in Addr_Set{&new ByteArrayOutputStream(CachedContent#3),&new ByteArrayOutputStream(CachedContent#2)}
              *    new ByteArrayOutputStream(CachedContent#2) num objects <= 1
              *    new ByteArrayOutputStream(CachedContent#3) num objects <= 1
              *    new PrintWriter(CachedContent#4) num objects == 1
              *    new byte[](CachedContent#1) num objects == 1
              *    this.content.length == 0
              * 
              *  Test Vectors:
              *    size: {-231..0}, {1..232-1}
              */
    37      public CachedContent(int size) {
    38          
    39          // construct output stream
    40          if(size > 0) {
    41              this.outstream = new ByteArrayOutputStream(size);
    42          } else {
    43              this.outstream = new ByteArrayOutputStream(DEFAULT_SIZE);
    44          }
    45          
    46          // construct writer from output stream
    47          try {
    48              this.cachedWriter =
    49                      new PrintWriter(new OutputStreamWriter(this.outstream, "UTF-8"));
    50          } catch(UnsupportedEncodingException e) {
    51              // shouldn't be possible, java always supports utf-8
    52              throw new RuntimeException("Encoding problem", e);
    53          }
    54      }
    55      
    56      public CachedContent(int size, String contentType) {
                 /* 
    P/P           *  Method: void org.apache.roller.weblogger.util.cache.CachedContent(int, String)
                  * 
                  *  Postconditions:
                  *    this.cachedWriter == &new PrintWriter(CachedContent#4)
                  *    this.content == &new byte[](CachedContent#1)
                  *    this.contentType == contentType
                  *    init'ed(this.contentType)
                  *    this.outstream == One-of{&new ByteArrayOutputStream(CachedContent#2), &new ByteArrayOutputStream(CachedContent#3)}
                  *    this.outstream in Addr_Set{&new ByteArrayOutputStream(CachedContent#2),&new ByteArrayOutputStream(CachedContent#3)}
                  *    new ByteArrayOutputStream(CachedContent#2) num objects <= 1
                  *    new ByteArrayOutputStream(CachedContent#3) num objects <= 1
                  *    new PrintWriter(CachedContent#4) num objects == 1
                  *    new byte[](CachedContent#1) num objects == 1
                  *    ...
                  */
    57          this(size);
    58          this.contentType = contentType;
    59      }
    60      
    61      
    62      /**
    63       * Get the content cached in this object as a byte array.  If you convert
    64       * this back to a string yourself, be sure to re-encode in "UTF-8".
    65       *
    66       * NOTE: the content is only a representation of the data written to the
    67       *       enclosed Writer up until the last call to flush().
    68       */
    69      public byte[] getContent() {
                 /* 
    P/P           *  Method: byte[] getContent()
                  * 
                  *  Preconditions:
                  *    init'ed(this.content)
                  * 
                  *  Postconditions:
                  *    return_value == this.content
                  *    init'ed(return_value)
                  */
    70          return this.content;
    71      }
    72      
    73      
    74      /**
    75       * Get the content cached in this object as a String.
    76       *
    77       * NOTE: the content is only a representation of the data written to the
    78       *       enclosed Writer up until the last call to flush().
    79       */
    80      public String getContentAsString() {
    81          try {
                     /* 
    P/P               *  Method: String getContentAsString()
                      * 
                      *  Preconditions:
                      *    init'ed(this.content)
                      * 
                      *  Postconditions:
                      *    return_value == &new String(getContentAsString#1)
                      *    new String(getContentAsString#1) num objects == 1
                      */
    82              return new String(this.content,"UTF-8");
    83          } catch (UnsupportedEncodingException uex) {
    84              // shouldn't ever happen - violates Java Spec.
    85              throw new RuntimeException(uex);
    86          }
    87      }
    88      
    89      
    90      public PrintWriter getCachedWriter() {
                 /* 
    P/P           *  Method: PrintWriter getCachedWriter()
                  * 
                  *  Preconditions:
                  *    init'ed(this.cachedWriter)
                  * 
                  *  Postconditions:
                  *    return_value == this.cachedWriter
                  *    init'ed(return_value)
                  */
    91          return cachedWriter;
    92      }
    93      
    94      
    95      public String getContentType() {
                 /* 
    P/P           *  Method: String getContentType()
                  * 
                  *  Preconditions:
                  *    init'ed(this.contentType)
                  * 
                  *  Postconditions:
                  *    return_value == this.contentType
                  *    init'ed(return_value)
                  */
    96          return contentType;
    97      }
    98      
    99      
   100      /**
   101       * Called to flush any output in the cached Writer to
   102       * the cached content for more permanent storage.
   103       *
   104       * @throws IllegalStateException if calling flush() after a close()
   105       */
   106      public void flush() {
   107          
                 /* 
    P/P           *  Method: void flush()
                  * 
                  *  Preconditions:
                  *    log != null
                  *    this.cachedWriter != null
                  *    this.outstream != null
                  * 
                  *  Presumptions:
                  *    java.io.ByteArrayOutputStream:toByteArray(...)@113 != null
                  *    this.content.length@113 <= 232-1
                  * 
                  *  Postconditions:
                  *    (soft) this.content != null
                  */
   108          if(this.outstream == null) {
   109              throw new IllegalStateException("Cannot flush() after a close()!");
   110          }
   111          
   112          this.cachedWriter.flush();
   113          this.content = this.outstream.toByteArray();
   114          
   115          log.debug("FLUSHED "+this.content.length);
   116      }
   117      
   118      
   119      /**
   120       * Close this CachedContent from further writing.
   121       */
   122      public void close() throws IOException {
   123          
                 /* 
    P/P           *  Method: void close()
                  * 
                  *  Preconditions:
                  *    log != null
                  *    init'ed(this.cachedWriter)
                  *    init'ed(this.outstream)
                  * 
                  *  Postconditions:
                  *    this.cachedWriter == null
                  *    possibly_updated(this.content)
                  *    this.outstream == null
                  * 
                  *  Test Vectors:
                  *    this.cachedWriter: Addr_Set{null}, Inverse{null}
                  *    this.outstream: Addr_Set{null}, Inverse{null}
                  */
   124          if(this.cachedWriter != null) {
   125              this.cachedWriter.flush();
   126              this.cachedWriter.close();
   127              this.cachedWriter = null;
   128          }
   129          
   130          if(this.outstream != null) {
   131              this.content = this.outstream.toByteArray();
   132              this.outstream.close();
   133              this.outstream = null;
   134          }
   135          
   136          log.debug("CLOSED");
   137      }
   138      
   139  }








SofCheck Inspector Build Version : 2.18479
CachedContent.java 2009-Jan-02 14:25:00
CachedContent.class 2009-Sep-04 03:12:32