Source Code Cross Referenced for CachedContent.java in  » Blogger-System » apache-roller-3.1 » org » apache » roller » util » cache » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Blogger System » apache roller 3.1 » org.apache.roller.util.cache 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.roller.util.cache;
002:
003:        import java.io.ByteArrayOutputStream;
004:        import java.io.IOException;
005:        import java.io.OutputStreamWriter;
006:        import java.io.PrintWriter;
007:        import java.io.Serializable;
008:        import java.io.UnsupportedEncodingException;
009:        import org.apache.commons.logging.Log;
010:        import org.apache.commons.logging.LogFactory;
011:
012:        /**
013:         * A utility class for storing cached content written to a java.io.Writer.
014:         */
015:        public class CachedContent implements  Serializable {
016:
017:            private static Log log = LogFactory.getLog(CachedContent.class);
018:
019:            // default to an 8K buffered cache
020:            public static final int DEFAULT_SIZE = 8192;
021:
022:            // the byte array we use to maintain the cached content
023:            private byte[] content = new byte[0];
024:
025:            // content-type of data in byte array
026:            private String contentType = null;
027:
028:            // Use a byte array output stream to cached the output bytes
029:            private transient ByteArrayOutputStream outstream = null;
030:
031:            // The PrintWriter that users will be writing to
032:            private transient PrintWriter cachedWriter = null;
033:
034:            public CachedContent(int size) {
035:
036:                // construct output stream
037:                if (size > 0) {
038:                    this .outstream = new ByteArrayOutputStream(size);
039:                } else {
040:                    this .outstream = new ByteArrayOutputStream(DEFAULT_SIZE);
041:                }
042:
043:                // construct writer from output stream
044:                try {
045:                    this .cachedWriter = new PrintWriter(new OutputStreamWriter(
046:                            this .outstream, "UTF-8"));
047:                } catch (UnsupportedEncodingException e) {
048:                    // shouldn't be possible, java always supports utf-8
049:                    throw new RuntimeException("Encoding problem", e);
050:                }
051:            }
052:
053:            public CachedContent(int size, String contentType) {
054:                this (size);
055:                this .contentType = contentType;
056:            }
057:
058:            /**
059:             * Get the content cached in this object as a byte array.  If you convert
060:             * this back to a string yourself, be sure to re-encode in "UTF-8".
061:             *
062:             * NOTE: the content is only a representation of the data written to the
063:             *       enclosed Writer up until the last call to flush().
064:             */
065:            public byte[] getContent() {
066:                return this .content;
067:            }
068:
069:            /**
070:             * Get the content cached in this object as a String.
071:             *
072:             * NOTE: the content is only a representation of the data written to the
073:             *       enclosed Writer up until the last call to flush().
074:             */
075:            public String getContentAsString() {
076:                try {
077:                    return new String(this .content, "UTF-8");
078:                } catch (UnsupportedEncodingException uex) {
079:                    // shouldn't ever happen - violates Java Spec.
080:                    throw new RuntimeException(uex);
081:                }
082:            }
083:
084:            public PrintWriter getCachedWriter() {
085:                return cachedWriter;
086:            }
087:
088:            public String getContentType() {
089:                return contentType;
090:            }
091:
092:            /**
093:             * Called to flush any output in the cached Writer to
094:             * the cached content for more permanent storage.
095:             *
096:             * @throws IllegalStateException if calling flush() after a close()
097:             */
098:            public void flush() {
099:
100:                if (this .outstream == null) {
101:                    throw new IllegalStateException(
102:                            "Cannot flush() after a close()!");
103:                }
104:
105:                this .cachedWriter.flush();
106:                this .content = this .outstream.toByteArray();
107:
108:                log.debug("FLUSHED " + this .content.length);
109:            }
110:
111:            /**
112:             * Close this CachedContent from further writing.
113:             */
114:            public void close() throws IOException {
115:
116:                if (this .cachedWriter != null) {
117:                    this .cachedWriter.flush();
118:                    this .cachedWriter.close();
119:                    this .cachedWriter = null;
120:                }
121:
122:                if (this .outstream != null) {
123:                    this .content = this .outstream.toByteArray();
124:                    this .outstream.close();
125:                    this .outstream = null;
126:                }
127:
128:                log.debug("CLOSED");
129:            }
130:
131:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.