Source Code Cross Referenced for GzipResponseStream.java in  » Content-Management-System » openedit » com » openedit » servlet » gzip » 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 » Content Management System » openedit » com.openedit.servlet.gzip 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.openedit.servlet.gzip;
002:
003:        import java.io.ByteArrayOutputStream;
004:        import java.io.IOException;
005:        import java.io.OutputStream;
006:        import java.util.zip.GZIPOutputStream;
007:
008:        import javax.servlet.ServletOutputStream;
009:        import javax.servlet.http.HttpServletResponse;
010:
011:        public class GzipResponseStream extends ServletOutputStream {
012:            // abstraction of the output stream used for compression
013:            protected OutputStream bufferedOutput = null;
014:            // state keeping variable for if close() has been called
015:            protected boolean closed = false;
016:            // reference to original response
017:            protected HttpServletResponse response = null;
018:            // reference to the output stream to the client's browser
019:            protected ServletOutputStream output = null;
020:            // default size of the in-memory buffer
021:            private int bufferSize = 50000;
022:
023:            public GzipResponseStream(HttpServletResponse response)
024:                    throws IOException {
025:                super ();
026:                closed = false;
027:                this .response = response;
028:                this .output = response.getOutputStream();
029:                bufferedOutput = new ByteArrayOutputStream();
030:            }
031:
032:            public void close() throws IOException {
033:                // This hack shouldn't be needed, but it seems to make JBoss and Struts
034:                // like the code more without hurting anything.
035:                /*
036:                // verify the stream is yet to be closed
037:                if (closed) {
038:                  throw new IOException("This output stream has already been closed");
039:                }
040:                 */
041:                // if we buffered everything in memory, gzip it
042:                if (bufferedOutput instanceof  ByteArrayOutputStream) {
043:                    // get the content
044:                    ByteArrayOutputStream baos = (ByteArrayOutputStream) bufferedOutput;
045:                    // prepare a gzip stream
046:                    ByteArrayOutputStream compressedContent = new ByteArrayOutputStream();
047:                    GZIPOutputStream gzipstream = new GZIPOutputStream(
048:                            compressedContent);
049:                    byte[] bytes = baos.toByteArray();
050:                    gzipstream.write(bytes);
051:                    gzipstream.finish();
052:                    // get the compressed content
053:                    byte[] compressedBytes = compressedContent.toByteArray();
054:                    // set appropriate HTTP headers
055:                    response.setContentLength(compressedBytes.length);
056:                    response.addHeader("Content-Encoding", "gzip");
057:                    output.write(compressedBytes);
058:                    output.flush();
059:                    output.close();
060:                    closed = true;
061:                }
062:                // if things were not buffered in memory, finish the GZIP stream and response
063:                else if (bufferedOutput instanceof  GZIPOutputStream) {
064:                    // cast to appropriate type
065:                    GZIPOutputStream gzipstream = (GZIPOutputStream) bufferedOutput;
066:                    // finish the compression
067:                    gzipstream.finish();
068:                    // finish the response
069:                    output.flush();
070:                    output.close();
071:                    closed = true;
072:                }
073:            }
074:
075:            public void flush() throws IOException {
076:                if (closed) {
077:                    throw new IOException("Cannot flush a closed output stream");
078:                }
079:                bufferedOutput.flush();
080:            }
081:
082:            public void write(int b) throws IOException {
083:                if (closed) {
084:                    throw new IOException(
085:                            "Cannot write to a closed output stream");
086:                }
087:                // make sure we aren't over the buffer's limit
088:                checkBufferSize(1);
089:                // write the byte to the temporary output
090:                bufferedOutput.write((byte) b);
091:            }
092:
093:            private void checkBufferSize(int length) throws IOException {
094:                // check if we are buffering too large of a file
095:                if (bufferedOutput instanceof  ByteArrayOutputStream) {
096:                    ByteArrayOutputStream baos = (ByteArrayOutputStream) bufferedOutput;
097:                    if (baos.size() + length > bufferSize) {
098:                        // files too large to keep in memory are sent to the client without Content-Length specified
099:                        response.addHeader("Content-Encoding", "gzip");
100:                        // get existing bytes
101:                        byte[] bytes = baos.toByteArray();
102:                        // make new gzip stream using the response output stream
103:                        GZIPOutputStream gzipstream = new GZIPOutputStream(
104:                                output);
105:                        gzipstream.write(bytes);
106:                        // we are no longer buffering, send content via gzipstream
107:                        bufferedOutput = gzipstream;
108:                    }
109:                }
110:            }
111:
112:            public void write(byte b[]) throws IOException {
113:                write(b, 0, b.length);
114:            }
115:
116:            public void write(byte b[], int off, int len) throws IOException {
117:                //System.out.println("writing...");
118:                if (closed) {
119:                    throw new IOException(
120:                            "Cannot write to a closed output stream");
121:                }
122:                // make sure we aren't over the buffer's limit
123:                checkBufferSize(len);
124:                // write the content to the buffer
125:                bufferedOutput.write(b, off, len);
126:            }
127:
128:            public boolean closed() {
129:                return (this .closed);
130:            }
131:
132:            public void reset() {
133:                //noop
134:            }
135:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.