//# 0 errors, 150 messages
//#

    //#CachedContent.java:1:1: class: org.apache.roller.weblogger.util.cache.CachedContent
package org.apache.roller.weblogger.util.cache;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * A utility class for storing cached content written to a java.io.Writer.
 */
public class CachedContent implements Serializable {
    
    private static Log log = LogFactory.getLog(CachedContent.class);
    //#CachedContent.java:19: method: org.apache.roller.weblogger.util.cache.CachedContent.org.apache.roller.weblogger.util.cache.CachedContent__static_init
    //#CachedContent.java:19: Warning: method not available
    //#    -- call on Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.util.cache.CachedContent
    //#    method: org.apache.roller.weblogger.util.cache.CachedContent__static_init
    //#    unanalyzed callee: Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#output(org.apache.roller.weblogger.util.cache.CachedContent__static_init): __Descendant_Table[org/apache/roller/weblogger/util/cache/CachedContent]
    //#output(org.apache.roller.weblogger.util.cache.CachedContent__static_init): __Dispatch_Table.close()V
    //#output(org.apache.roller.weblogger.util.cache.CachedContent__static_init): __Dispatch_Table.flush()V
    //#output(org.apache.roller.weblogger.util.cache.CachedContent__static_init): __Dispatch_Table.getCachedWriter()Ljava/io/PrintWriter;
    //#output(org.apache.roller.weblogger.util.cache.CachedContent__static_init): __Dispatch_Table.getContent()[B
    //#output(org.apache.roller.weblogger.util.cache.CachedContent__static_init): __Dispatch_Table.getContentAsString()Ljava/lang/String;
    //#output(org.apache.roller.weblogger.util.cache.CachedContent__static_init): __Dispatch_Table.getContentType()Ljava/lang/String;
    //#output(org.apache.roller.weblogger.util.cache.CachedContent__static_init): log
    //#post(org.apache.roller.weblogger.util.cache.CachedContent__static_init): __Descendant_Table[org/apache/roller/weblogger/util/cache/CachedContent] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.util.cache.CachedContent__static_init): __Dispatch_Table.close()V == &close
    //#post(org.apache.roller.weblogger.util.cache.CachedContent__static_init): __Dispatch_Table.flush()V == &flush
    //#post(org.apache.roller.weblogger.util.cache.CachedContent__static_init): __Dispatch_Table.getCachedWriter()Ljava/io/PrintWriter; == &getCachedWriter
    //#post(org.apache.roller.weblogger.util.cache.CachedContent__static_init): __Dispatch_Table.getContent()[B == &getContent
    //#post(org.apache.roller.weblogger.util.cache.CachedContent__static_init): __Dispatch_Table.getContentAsString()Ljava/lang/String; == &getContentAsString
    //#post(org.apache.roller.weblogger.util.cache.CachedContent__static_init): __Dispatch_Table.getContentType()Ljava/lang/String; == &getContentType
    //#post(org.apache.roller.weblogger.util.cache.CachedContent__static_init): init'ed(log)
    //#CachedContent.java:19: end of method: org.apache.roller.weblogger.util.cache.CachedContent.org.apache.roller.weblogger.util.cache.CachedContent__static_init
    
    // default to an 8K buffered cache
    public static final int DEFAULT_SIZE = 8192;
    
    // the byte array we use to maintain the cached content
    private byte[] content = new byte[0];
    
    // content-type of data in byte array
    private String contentType = null;
    
    // Use a byte array output stream to cached the output bytes
    private transient ByteArrayOutputStream outstream = null;
    
    // The PrintWriter that users will be writing to
    private transient PrintWriter cachedWriter = null;
    
    
    public CachedContent(int size) {
    //#CachedContent.java:37: method: void org.apache.roller.weblogger.util.cache.CachedContent.org.apache.roller.weblogger.util.cache.CachedContent(int)
    //#input(void org.apache.roller.weblogger.util.cache.CachedContent(int)): size
    //#input(void org.apache.roller.weblogger.util.cache.CachedContent(int)): this
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int)): new ByteArrayOutputStream(CachedContent#2) num objects
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int)): new ByteArrayOutputStream(CachedContent#3) num objects
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int)): new PrintWriter(CachedContent#4) num objects
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int)): new byte[](CachedContent#1) num objects
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int)): this.content.length
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int)): this.cachedWriter
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int)): this.content
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int)): this.contentType
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int)): this.outstream
    //#new obj(void org.apache.roller.weblogger.util.cache.CachedContent(int)): new ByteArrayOutputStream(CachedContent#2)
    //#new obj(void org.apache.roller.weblogger.util.cache.CachedContent(int)): new ByteArrayOutputStream(CachedContent#3)
    //#new obj(void org.apache.roller.weblogger.util.cache.CachedContent(int)): new PrintWriter(CachedContent#4)
    //#new obj(void org.apache.roller.weblogger.util.cache.CachedContent(int)): new byte[](CachedContent#1)
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int)): this.cachedWriter == &new PrintWriter(CachedContent#4)
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int)): this.content == &new byte[](CachedContent#1)
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int)): this.contentType == null
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int)): this.outstream in Addr_Set{&new ByteArrayOutputStream(CachedContent#3),&new ByteArrayOutputStream(CachedContent#2)}
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int)): new ByteArrayOutputStream(CachedContent#2) num objects <= 1
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int)): new ByteArrayOutputStream(CachedContent#3) num objects <= 1
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int)): new PrintWriter(CachedContent#4) num objects == 1
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int)): new byte[](CachedContent#1) num objects == 1
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int)): this.content.length == 0
    //#test_vector(void org.apache.roller.weblogger.util.cache.CachedContent(int)): size: {-2_147_483_648..0}, {1..4_294_967_295}
        
        // construct output stream
        if(size > 0) {
            this.outstream = new ByteArrayOutputStream(size);
        } else {
            this.outstream = new ByteArrayOutputStream(DEFAULT_SIZE);
        }
        
        // construct writer from output stream
        try {
            this.cachedWriter =
                    new PrintWriter(new OutputStreamWriter(this.outstream, "UTF-8"));
        } catch(UnsupportedEncodingException e) {
            // shouldn't be possible, java always supports utf-8
            throw new RuntimeException("Encoding problem", e);
        }
    }
    //#CachedContent.java:54: end of method: void org.apache.roller.weblogger.util.cache.CachedContent.org.apache.roller.weblogger.util.cache.CachedContent(int)
    
    public CachedContent(int size, String contentType) {
        this(size);
    //#CachedContent.java:57: method: void org.apache.roller.weblogger.util.cache.CachedContent.org.apache.roller.weblogger.util.cache.CachedContent(int, String)
    //#input(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): contentType
    //#input(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): size
    //#input(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): this
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): new ByteArrayOutputStream(CachedContent#2) num objects
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): new ByteArrayOutputStream(CachedContent#3) num objects
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): new PrintWriter(CachedContent#4) num objects
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): new byte[](CachedContent#1) num objects
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): new byte[](CachedContent#1).length
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): this.cachedWriter
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): this.content
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): this.contentType
    //#output(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): this.outstream
    //#new obj(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): new ByteArrayOutputStream(CachedContent#2)
    //#new obj(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): new ByteArrayOutputStream(CachedContent#3)
    //#new obj(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): new PrintWriter(CachedContent#4)
    //#new obj(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): new byte[](CachedContent#1)
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): this.cachedWriter == &new PrintWriter(CachedContent#4)
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): this.content == &new byte[](CachedContent#1)
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): this.contentType == contentType
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): init'ed(this.contentType)
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): this.outstream == One-of{&new ByteArrayOutputStream(CachedContent#2), &new ByteArrayOutputStream(CachedContent#3)}
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): this.outstream in Addr_Set{&new ByteArrayOutputStream(CachedContent#2),&new ByteArrayOutputStream(CachedContent#3)}
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): new ByteArrayOutputStream(CachedContent#2) num objects <= 1
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): new ByteArrayOutputStream(CachedContent#3) num objects <= 1
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): new PrintWriter(CachedContent#4) num objects == 1
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): new byte[](CachedContent#1) num objects == 1
    //#post(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): new byte[](CachedContent#1).length == 0
    //#unanalyzed(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): Effects-of-calling:java.io.ByteArrayOutputStream
    //#unanalyzed(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): Effects-of-calling:java.io.OutputStreamWriter
    //#unanalyzed(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): Effects-of-calling:java.io.PrintWriter
    //#unanalyzed(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): Effects-of-calling:java.lang.Throwable:__curr_excep_obj
    //#unanalyzed(void org.apache.roller.weblogger.util.cache.CachedContent(int, String)): Effects-of-calling:java.lang.RuntimeException
        this.contentType = contentType;
    }
    //#CachedContent.java:59: end of method: void org.apache.roller.weblogger.util.cache.CachedContent.org.apache.roller.weblogger.util.cache.CachedContent(int, String)
    
    
    /**
     * Get the content cached in this object as a byte array.  If you convert
     * this back to a string yourself, be sure to re-encode in "UTF-8".
     *
     * NOTE: the content is only a representation of the data written to the
     *       enclosed Writer up until the last call to flush().
     */
    public byte[] getContent() {
        return this.content;
    //#CachedContent.java:70: method: byte[] org.apache.roller.weblogger.util.cache.CachedContent.getContent()
    //#input(byte[] getContent()): this
    //#input(byte[] getContent()): this.content
    //#output(byte[] getContent()): return_value
    //#pre[2] (byte[] getContent()): init'ed(this.content)
    //#post(byte[] getContent()): return_value == this.content
    //#post(byte[] getContent()): init'ed(return_value)
    //#CachedContent.java:70: end of method: byte[] org.apache.roller.weblogger.util.cache.CachedContent.getContent()
    }
    
    
    /**
     * Get the content cached in this object as a String.
     *
     * NOTE: the content is only a representation of the data written to the
     *       enclosed Writer up until the last call to flush().
     */
    public String getContentAsString() {
        try {
            return new String(this.content,"UTF-8");
    //#CachedContent.java:82: method: String org.apache.roller.weblogger.util.cache.CachedContent.getContentAsString()
    //#input(String getContentAsString()): this
    //#input(String getContentAsString()): this.content
    //#output(String getContentAsString()): new String(getContentAsString#1) num objects
    //#output(String getContentAsString()): return_value
    //#new obj(String getContentAsString()): new String(getContentAsString#1)
    //#pre[2] (String getContentAsString()): init'ed(this.content)
    //#post(String getContentAsString()): return_value == &new String(getContentAsString#1)
    //#post(String getContentAsString()): new String(getContentAsString#1) num objects == 1
        } catch (UnsupportedEncodingException uex) {
            // shouldn't ever happen - violates Java Spec.
            throw new RuntimeException(uex);
    //#CachedContent.java:85: end of method: String org.apache.roller.weblogger.util.cache.CachedContent.getContentAsString()
        }
    }
    
    
    public PrintWriter getCachedWriter() {
        return cachedWriter;
    //#CachedContent.java:91: method: PrintWriter org.apache.roller.weblogger.util.cache.CachedContent.getCachedWriter()
    //#input(PrintWriter getCachedWriter()): this
    //#input(PrintWriter getCachedWriter()): this.cachedWriter
    //#output(PrintWriter getCachedWriter()): return_value
    //#pre[2] (PrintWriter getCachedWriter()): init'ed(this.cachedWriter)
    //#post(PrintWriter getCachedWriter()): return_value == this.cachedWriter
    //#post(PrintWriter getCachedWriter()): init'ed(return_value)
    //#CachedContent.java:91: end of method: PrintWriter org.apache.roller.weblogger.util.cache.CachedContent.getCachedWriter()
    }
    
    
    public String getContentType() {
        return contentType;
    //#CachedContent.java:96: method: String org.apache.roller.weblogger.util.cache.CachedContent.getContentType()
    //#input(String getContentType()): this
    //#input(String getContentType()): this.contentType
    //#output(String getContentType()): return_value
    //#pre[2] (String getContentType()): init'ed(this.contentType)
    //#post(String getContentType()): return_value == this.contentType
    //#post(String getContentType()): init'ed(return_value)
    //#CachedContent.java:96: end of method: String org.apache.roller.weblogger.util.cache.CachedContent.getContentType()
    }
    
    
    /**
     * Called to flush any output in the cached Writer to
     * the cached content for more permanent storage.
     *
     * @throws IllegalStateException if calling flush() after a close()
     */
    public void flush() {
        
        if(this.outstream == null) {
    //#CachedContent.java:108: method: void org.apache.roller.weblogger.util.cache.CachedContent.flush()
    //#input(void flush()): "FLUSHED "._tainted
    //#input(void flush()): log
    //#input(void flush()): this
    //#input(void flush()): this.cachedWriter
    //#input(void flush()): this.content.length
    //#input(void flush()): this.outstream
    //#output(void flush()): this.content
    //#pre[1] (void flush()): log != null
    //#pre[3] (void flush()): this.cachedWriter != null
    //#pre[5] (void flush()): this.outstream != null
    //#presumption(void flush()): java.io.ByteArrayOutputStream:toByteArray(...)@113 != null
    //#presumption(void flush()): this.content.length@113 <= 4_294_967_295
    //#post(void flush()): (soft) this.content != null
            throw new IllegalStateException("Cannot flush() after a close()!");
        }
        
        this.cachedWriter.flush();
        this.content = this.outstream.toByteArray();
        
        log.debug("FLUSHED "+this.content.length);
    //#CachedContent.java:115: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.util.cache.CachedContent
    //#    method: void flush()
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
    }
    //#CachedContent.java:116: end of method: void org.apache.roller.weblogger.util.cache.CachedContent.flush()
    
    
    /**
     * Close this CachedContent from further writing.
     */
    public void close() throws IOException {
        
        if(this.cachedWriter != null) {
    //#CachedContent.java:124: method: void org.apache.roller.weblogger.util.cache.CachedContent.close()
    //#input(void close()): log
    //#input(void close()): this
    //#input(void close()): this.cachedWriter
    //#input(void close()): this.outstream
    //#output(void close()): this.cachedWriter
    //#output(void close()): this.content
    //#output(void close()): this.outstream
    //#pre[1] (void close()): log != null
    //#pre[2] (void close()): init'ed(this.cachedWriter)
    //#pre[4] (void close()): init'ed(this.outstream)
    //#post(void close()): this.cachedWriter == null
    //#post(void close()): possibly_updated(this.content)
    //#post(void close()): this.outstream == null
    //#test_vector(void close()): this.cachedWriter: Addr_Set{null}, Inverse{null}
    //#test_vector(void close()): this.outstream: Addr_Set{null}, Inverse{null}
            this.cachedWriter.flush();
            this.cachedWriter.close();
            this.cachedWriter = null;
        }
        
        if(this.outstream != null) {
            this.content = this.outstream.toByteArray();
            this.outstream.close();
            this.outstream = null;
        }
        
        log.debug("CLOSED");
    //#CachedContent.java:136: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.util.cache.CachedContent
    //#    method: void close()
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
    }
    //#CachedContent.java:137: end of method: void org.apache.roller.weblogger.util.cache.CachedContent.close()
    
}
    //#CachedContent.java:: end of class: org.apache.roller.weblogger.util.cache.CachedContent
