1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.filters.compression;
24  
25  import com.liferay.portal.kernel.util.ByteArrayMaker;
26  
27  import java.io.IOException;
28  import java.io.OutputStream;
29  
30  import java.util.zip.GZIPOutputStream;
31  
32  import javax.servlet.ServletOutputStream;
33  import javax.servlet.http.HttpServletResponse;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  /**
39   * <a href="CompressionStream.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Jayson Falkner
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class CompressionStream extends ServletOutputStream {
46  
47      public CompressionStream(HttpServletResponse res) throws IOException {
48          super();
49  
50          _res = res;
51          _output = res.getOutputStream();
52          _bufferedOutput = new ByteArrayMaker();
53          _closed = false;
54      }
55  
56      public void close() throws IOException {
57          if (_closed) {
58              throw new IOException();
59          }
60  
61          if (_bufferedOutput instanceof ByteArrayMaker) {
62              ByteArrayMaker baos = (ByteArrayMaker)_bufferedOutput;
63  
64              ByteArrayMaker compressedContent = new ByteArrayMaker();
65  
66              GZIPOutputStream gzipOutput =
67                  new GZIPOutputStream(compressedContent);
68  
69              gzipOutput.write(baos.toByteArray());
70              gzipOutput.finish();
71  
72              byte[] compressedBytes = compressedContent.toByteArray();
73  
74              _res.setContentLength(compressedBytes.length);
75              _res.addHeader(_CONTENT_ENCODING, _GZIP);
76  
77              _output.write(compressedBytes);
78              _output.flush();
79              _output.close();
80  
81              _closed = true;
82          }
83          else if (_bufferedOutput instanceof GZIPOutputStream) {
84              GZIPOutputStream gzipOutput = (GZIPOutputStream)_bufferedOutput;
85  
86              gzipOutput.finish();
87  
88              _output.flush();
89              _output.close();
90  
91              _closed = true;
92          }
93      }
94  
95      public void flush() throws IOException {
96          if (_closed) {
97              throw new IOException();
98          }
99  
100         _bufferedOutput.flush();
101     }
102 
103     public void write(int b) throws IOException {
104         if (_closed) {
105             throw new IOException();
106         }
107 
108         // LEP-649
109 
110         //_checkBufferSize(1);
111 
112         _bufferedOutput.write((byte)b);
113     }
114 
115     public void write(byte b[]) throws IOException {
116         write(b, 0, b.length);
117     }
118 
119     public void write(byte b[], int off, int len) throws IOException {
120         if (_closed) {
121             throw new IOException();
122         }
123 
124         // LEP-649
125 
126         //_checkBufferSize(len);
127 
128         try {
129             _bufferedOutput.write(b, off, len);
130         }
131         catch (IOException ioe) {
132             _log.warn(ioe.getMessage());
133         }
134     }
135 
136     public boolean closed() {
137         return _closed;
138     }
139 
140     public void reset() {
141     }
142 
143     private static final String _CONTENT_ENCODING = "Content-Encoding";
144 
145     private static final String _GZIP = "gzip";
146 
147     private static Log _log = LogFactory.getLog(CompressionStream.class);
148 
149     private HttpServletResponse _res = null;
150     private ServletOutputStream _output = null;
151     private OutputStream _bufferedOutput = null;
152     private boolean _closed = false;
153 
154 }