KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > util > cache > CachedContent


1
2 package org.apache.roller.util.cache;
3
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.OutputStreamWriter JavaDoc;
7 import java.io.PrintWriter JavaDoc;
8 import java.io.Serializable JavaDoc;
9 import java.io.UnsupportedEncodingException JavaDoc;
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 JavaDoc {
18     
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 JavaDoc contentType = null;
29     
30     // Use a byte array output stream to cached the output bytes
31
private transient ByteArrayOutputStream JavaDoc outstream = null;
32     
33     // The PrintWriter that users will be writing to
34
private transient PrintWriter JavaDoc cachedWriter = null;
35     
36     
37     public CachedContent(int size) {
38         
39         // construct output stream
40
if(size > 0) {
41             this.outstream = new ByteArrayOutputStream JavaDoc(size);
42         } else {
43             this.outstream = new ByteArrayOutputStream JavaDoc(DEFAULT_SIZE);
44         }
45         
46         // construct writer from output stream
47
try {
48             this.cachedWriter =
49                     new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(this.outstream, "UTF-8"));
50         } catch(UnsupportedEncodingException JavaDoc e) {
51             // shouldn't be possible, java always supports utf-8
52
throw new RuntimeException JavaDoc("Encoding problem", e);
53         }
54     }
55     
56     public CachedContent(int size, String JavaDoc contentType) {
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() {
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 JavaDoc getContentAsString() {
81         try {
82             return new String JavaDoc(this.content,"UTF-8");
83         } catch (UnsupportedEncodingException JavaDoc uex) {
84             // shouldn't ever happen - violates Java Spec.
85
throw new RuntimeException JavaDoc(uex);
86         }
87     }
88     
89     
90     public PrintWriter JavaDoc getCachedWriter() {
91         return cachedWriter;
92     }
93     
94     
95     public String JavaDoc getContentType() {
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         
108         if(this.outstream == null) {
109             throw new IllegalStateException JavaDoc("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 JavaDoc {
123         
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 }
140
Popular Tags