Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to properly return a gzip encoded http request

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to properly return a gzip encoded http request

    Hello everyone,

    I'm currently working on my home automation project.
    the project required me to write my own "simple" webserver.

    i have some code to detect that the webbrowser accepts gzip encoding.
    if the browser supports it, i return gzip encoded websites but they seem to be rubbish since my browser (chrome) can't seem to display them.

    public byte[] getHTTPPage(HTTPRequestHandler request, String name, Hashtable<String, String> headers, Hashtable<String, String> parameters) throws UnsupportedEncodingException, IOException {
            if (CacheManager.getInstance().isCached(name, HTTPServer.getSessionID(parameters))) {
                return CacheManager.getInstance().getCache(name, HTTPServer.getSessionID(parameters)).getData();
            } else {
                BufferedReader reader = null;
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                InputStream resource;
                BufferedInputStream input = null;
                boolean isImage = false;
     
                boolean useGZip = false;
     
                useGZip = HTTPServer.isGZipEnabled(headers);
     
                if (name.endsWith(".do")) {
                    return HTTPServer.toByteArray(_generateHTTPPage(request, name, parameters));
                } else if (this.getClass().getResource(resourceLocation + name) == null) {
                    return new byte[0];
                }
     
                try {
                    resource = this.getClass().getResourceAsStream(resourceLocation + name);
                    if (!isImage(name)) {
                        reader = new BufferedReader(new InputStreamReader(resource));
                        while (reader.ready()) {
                            if (!isImage) {
                                bout.write(HTTPServer.toByteArray(reader.readLine() + "\r\n"));
                            } else {
                                bout.write(HTTPServer.toByteArray(reader.readLine()));
                            }
     
                        }
                    } else {
                        byte[] buffer = new byte[1024];
                        input = new BufferedInputStream(resource);
                        int length = -1;
                        while ((length = input.read(buffer, 0, buffer.length)) > -1) {
                            bout.write(buffer, 0, length);
                        }
                    }
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                    return null;
                } finally {
                    try {
                        if (reader != null) {
                            reader.close();
                        }
                    } catch (IOException ex) {
                    }
     
                    try {
                        if (input != null) {
                            input.close();
                        }
                    } catch (IOException ex) {
                    }
     
                    if (useGZip) {
                        ByteArrayOutputStream temp = new ByteArrayOutputStream();
                        temp.write(HTTPServer.encodeToGZip(bout.toByteArray()));
                        bout.reset();
                        bout.write(temp.toByteArray());
                        temp.reset();
                        temp = null;
                    }
     
                    CacheManager.getInstance().cache(name, bout.toByteArray(), HTTPServer.getSessionID(parameters));
                    return bout.toByteArray();
                }
            }
        }
     
     
    public static byte[] encodeToGZip(byte[] data) {
            ByteArrayOutputStream bos = null;
            GZIPOutputStream output = null;
            try {
                bos = new ByteArrayOutputStream();
                output = new GZIPOutputStream(bos);
                output.write(data);
                output.finish();
                output.close();
                //output.write(Base64.encodeBase64(data));
                return Base64.encodeBase64(bos.toByteArray());
    //            return bos.toByteArray();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
     
            //return bos.toByteArray();
            return null;
        }
     
    public static byte[] toByteArray(String data) throws UnsupportedEncodingException {
            return data.getBytes("utf-8");
        }

    i'm not sure why it doesn't work.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to properly return a gzip encoded http request

    Your code has a lot of classes that are not part of J2SE, so its difficult to comment on a lot of it. That being said, if you wish the browser to download a file make sure you are sending the appropriate header's to the browser - you should send the appropriate Content-Type header information. See
    HTTP/1.1: Header Field Definitions
    MIME - Wikipedia, the free encyclopedia

Similar Threads

  1. How to remove any BOM from UTF-8 encoded text files
    By efluvio in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: November 14th, 2012, 01:45 PM
  2. Calculating checksum when appending to GZIP stream
    By giorashc in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: August 4th, 2011, 05:11 AM
  3. [SOLVED] How to decode ENCODING=QUOTED-PRINTABLE encoded data?
    By efluvio in forum Algorithms & Recursion
    Replies: 8
    Last Post: June 13th, 2011, 04:10 PM
  4. Can't form a simple POST or GET http request. No data returns.
    By goodguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 29th, 2011, 05:04 AM
  5. [SOLVED] Problem accessing specific data in an array and getting it to return properly
    By Universalsoldja in forum Collections and Generics
    Replies: 3
    Last Post: February 4th, 2010, 04:26 PM

Tags for this Thread