FileDocCategorySizeDatePackage
GZIPSupport.javaAPI DocphoneME MR2 API (J2ME)5552Wed May 02 18:00:34 BST 2007com.sun.perseus.platform

GZIPSupport

public class GZIPSupport extends Object
This class is used to provide GZIP support to the Perseus engine by using a custom implementation of GZIPInputStream com.sun.perseus.platform.GZIPInputStream

Fields Summary
public static final String
HTTP_ACCEPT_ENCODING
Used for HTTP request headers
static final String
HTTP_GZIP_ENCODING
GZIP encoding
Constructors Summary
Methods Summary
public static java.io.InputStreamhandleGZIP(java.io.InputStream is)
This method checks if the input stream is a GZIP stream. It reads the first two bytes of the stream and compares the short it reads with the GZIP magic number (see java.util.zip.GZIPInputStream.GZIP_MAGIC). If the magic number matches, the method returns a java.util.zip.GZIPInputStream which wraps the input stream. Otherwise, the method returns either the unchanged stream or a BufferedInputStream if the input stream did not support marking, as defined by the java.io.InputStream.mark method.

param
is the input stream that might be GZIPPed.
return
a stream that handles GZIP uncompression, if any is needed.
throws
IOException if an I/O error happens while building a GZIPInputStream.

            // Wrap the stream if it does not support marking.
            // BufferedInputStream supports marking.

            //Temporary: wrap all input streams to workaround bug in WTK (CR 6335742)
            //Temporary: //if (!is.markSupported())
            is = new BufferedInputStream(is);

            int magicIn = 0;
            try {
                is.mark(2);
                magicIn = 0x0000ffff & (is.read() | is.read() << 8);
            } catch (IOException ex) {
                // We were not able to read at least two bytes,
                // this cannot be a GZIP stream as it does not 
                // even have the magic number header
                is.reset();

                return is;

            } finally {
                try {

                    is.reset();

                } catch (IOException ioe) {
                    // This should _never_ happen because we do not fall
                    // into _any_ of the conditions that might cause a 
                    // reset() to throw an IOException. If we got into that
                    // situation, it means we are in serious troubles.
                    throw new Error();
                }
            }
            if (magicIn == GZIPInputStream.GZIP_MAGIC) {

                return new GZIPInputStream(is);

            }

            return is;

        
public static java.io.InputStreamopenHandleGZIP(java.lang.String svgURI)
If GZIP encoding is supported, this method should setup the HTTP Request Header to declare that GZIP encoding is supported.

param
svgURI the url of the requested SVG resource.
return
a stream that does not handles GZIP uncompression.
throws
IOException if an I/O error happens while opening the requested URI.


                                                                    
           

        InputConnection svgURLConnection = 
            (InputConnection)Connector.open(svgURI, Connector.READ);

        try {
            if (svgURLConnection instanceof HttpConnection) {
                setupHttpEncoding((HttpConnection)svgURLConnection);
            }

            return(svgURLConnection.openInputStream());

        } finally {
            svgURLConnection.close();
        }
    
static voidsetupHttpEncoding(javax.microedition.io.HttpConnection httpC)

            String encodings = 
                httpC.getRequestProperty(HTTP_ACCEPT_ENCODING);
        
            if (encodings == null) {
                encodings = "";
            }
        
            if (encodings.trim().length() > 0) {
                encodings += ",";
            }
            encodings += HTTP_GZIP_ENCODING;
            httpC.setRequestProperty(HTTP_ACCEPT_ENCODING, encodings);