Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.http.gzip;
 
 
 
 
 /* ------------------------------------------------------------ */
Skeletal implementation of a CompressedStream. This class adds compression features to a ServletOutputStream and takes care of setting response headers, etc. Major work and configuration is done here. Subclasses using different kinds of compression only have to implement the abstract methods doCompress() and setContentEncoding() using the desired compression and setting the appropriate Content-Encoding header string.
 
 public abstract class AbstractCompressedStream extends ServletOutputStream 
 {
     private final String _encoding;
     protected HttpServletRequest _request;
     protected HttpServletResponse _response;
     protected OutputStream _out;
     protected ByteArrayOutputStream2 _bOut;
     protected boolean _closed;
     protected int _bufferSize;
     protected int _minCompressSize;
     protected long _contentLength;
     protected boolean _doNotCompress;

    
Instantiates a new compressed stream.

Parameters:
request the request
response the response
contentLength the content length
bufferSize the buffer size
minCompressSize the min compress size
Throws:
java.io.IOException Signals that an I/O exception has occurred.
 
     public AbstractCompressedStream(String encoding,HttpServletRequest requestHttpServletResponse responselong contentLengthint bufferSizeint minCompressSize)
             throws IOException
     {
         =encoding;
          = request;
          = response;
          = contentLength;
          = bufferSize;
          = minCompressSize;
         if (minCompressSize == 0)
             doCompress();
     }

    
Reset buffer.
 
     public void resetBuffer()
     {
         if (.isCommitted())
             throw new IllegalStateException("Committed");
          = false;
          = null;
          = null;
         if ( != null)
             .setHeader("Content-Encoding",null);
          = null;
          = false;
     }

    
Sets the content length.

Parameters:
length the new content length
    public void setContentLength(long length)
    {
         = length;
        if ( && length >= 0)
        {
            if ( < .)
                .setContentLength((int));
            else
                .setHeader("Content-Length",Long.toString());
        }
    }
    /* ------------------------------------------------------------ */
    
    @Override
    public void flush() throws IOException
    {
        if ( == null ||  != null)
        {
            if ( > 0 &&  < )
                doNotCompress();
            else
                doCompress();
        }
        .flush();
    }
    /* ------------------------------------------------------------ */
    
    @Override
    public void close() throws IOException
    {
        if ()
            return;
        if (.getAttribute("javax.servlet.include.request_uri") != null)
            flush();
        else
        {
            if ( != null)
            {
                if ( < 0)
                     = .getCount();
                if ( < )
                    doNotCompress();
                else
                    doCompress();
            }
            else if ( == null)
            {
                doNotCompress();
            }
            if ( != null)
                .close();
            else
                .close();
             = true;
        }
    }

    
Finish.

Throws:
java.io.IOException Signals that an I/O exception has occurred.
    public void finish() throws IOException
    {
        if (!)
        {
            if ( == null ||  != null)
            {
                if ( > 0 &&  < )
                    doNotCompress();
                else
                    doCompress();
            }
            if ( != null && !)
            {
                 = true;
                .close();
            }
        }
    }
    /* ------------------------------------------------------------ */
    
    @Override
    public void write(int bthrows IOException
    {
        checkOut(1);
        .write(b);
    }
    /* ------------------------------------------------------------ */
    
    @Override
    public void write(byte b[]) throws IOException
    {
        checkOut(b.length);
        .write(b);
    }
    /* ------------------------------------------------------------ */
    
    @Override
    public void write(byte b[], int offint lenthrows IOException
    {
        checkOut(len);
        .write(b,off,len);
    }
    
    
Do compress.

Throws:
java.io.IOException Signals that an I/O exception has occurred.
    public void doCompress() throws IOException
    {
        if (==null
        {
            if (.isCommitted())
                throw new IllegalStateException();
            
            setHeader("Content-Encoding");            
            if (.containsHeader("Content-Encoding"))
            {
                ==createStream();
                if (!=null)
                {
                    .write(.getBuf(),0,.getCount());
                    =null;
                }
            }
            else 
                doNotCompress();
        }
    }

    
Do not compress.

Throws:
java.io.IOException Signals that an I/O exception has occurred.
    public void doNotCompress() throws IOException
    {
        if ( != null)
            throw new IllegalStateException("Compressed output stream is already assigned.");
        if ( == null ||  != null)
        {
             = true;
             = .getOutputStream();
            setContentLength();
            if ( != null)
                .write(.getBuf(),0,.getCount());
             = null;
        }
    }

    
Check out.

Parameters:
length the length
Throws:
java.io.IOException Signals that an I/O exception has occurred.
    private void checkOut(int lengththrows IOException
    {
        if ()
            throw new IOException("CLOSED");
        if ( == null)
        {
            if (.isCommitted() || ( >= 0 &&  < ))
                doNotCompress();
            else if (length > )
                doCompress();
            else
                 =  = new ByteArrayOutputStream2();
        }
        else if ( != null)
        {
            if (.isCommitted() || ( >= 0 &&  < ))
                doNotCompress();
            else if (length >= (.getBuf().length - .getCount()))
                doCompress();
        }
    }

    

See also:
org.eclipse.jetty.http.gzip.CompressedStream#getOutputStream()
    public OutputStream getOutputStream()
    {
        return ;
    }

    

See also:
org.eclipse.jetty.http.gzip.CompressedStream#isClosed()
    public boolean isClosed()
    {
        return ;
    }
    
    
Allows derived implementations to replace PrintWriter implementation.
    protected PrintWriter newWriter(OutputStream outString encodingthrows UnsupportedEncodingException
    {
        return encoding == null?new PrintWriter(out):new PrintWriter(new OutputStreamWriter(out,encoding));
    }
    protected void setHeader(String name,String value)
    {
        .setHeader(namevalue);
    }
    
    
Create the stream fitting to the underlying compression type.

Throws:
java.io.IOException Signals that an I/O exception has occurred.
    protected abstract DeflaterOutputStream createStream() throws IOException;
New to GrepCode? Check out our FAQ X