001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }