001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.servlet.filters.gzip;
016    
017    import com.liferay.portal.util.PropsValues;
018    
019    import java.io.IOException;
020    import java.io.OutputStream;
021    
022    import java.util.zip.GZIPOutputStream;
023    
024    import javax.servlet.ServletOutputStream;
025    
026    /**
027     * @author Shuyang Zhou
028     * @author Raymond Augé
029     */
030    public class GZipServletOutputStream extends ServletOutputStream {
031    
032            public GZipServletOutputStream(OutputStream outputStream)
033                    throws IOException {
034    
035                    _gZipOutputStream = new GZIPOutputStream(outputStream) {
036    
037                            {
038                                    def.setLevel(PropsValues.GZIP_COMPRESSION_LEVEL);
039                            }
040    
041                    };
042            }
043    
044            @Override
045            public void close() throws IOException {
046                    _gZipOutputStream.close();
047            }
048    
049            @Override
050            public void flush() throws IOException {
051                    _gZipOutputStream.flush();
052            }
053    
054            @Override
055            public void write(byte[] bytes) throws IOException {
056                    _gZipOutputStream.write(bytes);
057            }
058    
059            @Override
060            public void write(byte[] bytes, int offset, int length)
061                    throws IOException {
062    
063                    _gZipOutputStream.write(bytes, offset, length);
064            }
065    
066            @Override
067            public void write(int b) throws IOException {
068                    _gZipOutputStream.write(b);
069            }
070    
071            private GZIPOutputStream _gZipOutputStream;
072    
073    }