Source Code Cross Referenced for BufferedResponse.java in  » Ajax » zk » org » zkoss » web » servlet » 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 » Ajax » zk » org.zkoss.web.servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* BufferedResponse.java
002:
003:        {{IS_NOTE
004:        	Purpose:
005:        		
006:        	Description:
007:        		
008:        	History:
009:        		Mon Jan 17 14:02:29     2005, Created by tomyeh
010:        }}IS_NOTE
011:
012:        Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014:        {{IS_RIGHT
015:        	This program is distributed under GPL Version 2.0 in the hope that
016:        	it will be useful, but WITHOUT ANY WARRANTY.
017:        }}IS_RIGHT
018:         */
019:        package org.zkoss.web.servlet;
020:
021:        import java.io.Writer;
022:        import java.io.PrintWriter;
023:        import java.io.StringWriter;
024:        import java.io.OutputStream;
025:        import java.io.OutputStreamWriter;
026:        import java.io.ByteArrayOutputStream;
027:        import java.io.IOException;
028:
029:        import javax.servlet.ServletOutputStream;
030:        import javax.servlet.ServletResponse;
031:        import javax.servlet.ServletResponseWrapper;
032:        import javax.servlet.http.HttpServletResponse;
033:
034:        import org.zkoss.web.servlet.ServletOutputStreamWrapper;
035:        import org.zkoss.web.servlet.http.HttpBufferedResponse;
036:
037:        /**
038:         * A servlet response that uses another writer or stream as
039:         * the output.
040:         *
041:         * @author tomyeh
042:         */
043:        public class BufferedResponse extends ServletResponseWrapper {
044:            private Writer _writer;
045:            private OutputStream _stream;
046:
047:            private PrintWriter _pwt;
048:            private ServletOutputStream _sos;
049:
050:            /** Returns a buffered response with a writer, if writer is not null;
051:             * or the original response if writer is null.
052:             * If reponse is HttpServletResponse, {@link HttpBufferedResponse#getInstance}
053:             * is returned.
054:             * It is smart enough not to wrap the same writer twice.
055:             */
056:            public static final ServletResponse getInstance(
057:                    ServletResponse response, Writer writer) {
058:                if (response instanceof  HttpServletResponse)
059:                    return HttpBufferedResponse.getInstance(
060:                            (HttpServletResponse) response, writer);
061:
062:                if (writer != null
063:                        && (!(response instanceof  BufferedResponse) || ((BufferedResponse) response)._writer != writer))
064:                    return new BufferedResponse(response, writer);
065:                return response;
066:            }
067:
068:            /** Returns a buffered response with a output stream, if stream is not null;
069:             * or the original response if stream is null.
070:             * If reponse is HttpServletResponse, {@link HttpBufferedResponse#getInstance}
071:             * is returned.
072:             * It is smart enough not to wrap the same stream twice.
073:             */
074:            public static final ServletResponse getInstance(
075:                    ServletResponse response, OutputStream stream) {
076:                if (response instanceof  HttpServletResponse)
077:                    return HttpBufferedResponse.getInstance(
078:                            (HttpServletResponse) response, stream);
079:
080:                if (stream != null
081:                        && (!(response instanceof  BufferedResponse) || ((BufferedResponse) response)._stream != stream))
082:                    return new BufferedResponse(response, stream);
083:                return response;
084:            }
085:
086:            /** Constructs a buffered response with a writer. */
087:            private BufferedResponse(ServletResponse response, Writer writer) {
088:                super (response);
089:                if (writer == null)
090:                    throw new NullPointerException("writer");
091:                _writer = writer;
092:            }
093:
094:            /** Constructs a buffered response with an output stream. */
095:            private BufferedResponse(ServletResponse response,
096:                    OutputStream stream) {
097:                super (response);
098:                if (stream == null)
099:                    throw new NullPointerException("stream");
100:                _stream = stream;
101:            }
102:
103:            //super//
104:            public PrintWriter getWriter() throws IOException {
105:                if (_sos != null)
106:                    throw new IllegalStateException(
107:                            "getOutputStream was called");
108:
109:                if (_pwt == null) {
110:                    if (_writer != null) {
111:                        if (_writer instanceof  PrintWriter) {
112:                            _pwt = (PrintWriter) _writer;
113:                        } else {
114:                            _pwt = new PrintWriter(_writer);
115:                        }
116:                    } else {
117:                        _pwt = new PrintWriter(new OutputStreamWriter(_stream,
118:                                getCharacterEncoding()));
119:                    }
120:                }
121:                return _pwt;
122:            }
123:
124:            public ServletOutputStream getOutputStream() throws IOException {
125:                if (_pwt != null)
126:                    throw new IllegalStateException("getWriter was called");
127:
128:                if (_sos == null) {
129:                    if (_stream != null) {
130:                        _sos = ServletOutputStreamWrapper.getInstance(_stream);
131:                    } else {
132:                        throw new UnsupportedOperationException(
133:                                "ServletOutputStream cannot wrap PrintWriter");
134:                    }
135:                }
136:                return _sos;
137:            }
138:
139:            public void flushBuffer() throws IOException {
140:                if (_writer != null)
141:                    _writer.flush();
142:                else
143:                    _stream.flush();
144:            }
145:
146:            /** Useful only if StringWriter or ByteArrayOutputStream is used
147:             * to construct this object.
148:             */
149:            public void resetBuffer() {
150:                if (_writer instanceof  StringWriter) {
151:                    ((StringWriter) _writer).getBuffer().setLength(0);
152:                } else if (_stream instanceof  ByteArrayOutputStream) {
153:                    ((ByteArrayOutputStream) _stream).reset();
154:                }
155:            }
156:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.