14

I have a large amount of data being sent from server to Javascript, which is taking quite a long time to load.

I was wondering how I can implement compression at server and decompression in Javascript. I would appreciate any help.

5
  • See here json.org/java, and here developer.mozilla.org/en-US/docs/JSON Commented Dec 19, 2013 at 8:38
  • 3
    Did you mean encode/decode or you meant compress/decompress? I am confused Commented Dec 19, 2013 at 8:39
  • 3
    try using gzip compression, as most browsers will handle that by default. If that is not enough, we'd need to know what kind of data you are sending to try and assist you in shrinking this data further down. Commented Dec 19, 2013 at 8:39
  • Which webserver do you use? Commented Dec 19, 2013 at 8:45
  • Thanks for the quick response, My actual problem is I have huge JSON data returning from java program to java script. I was wondering if there is any way to reduce the size of the JSON object. Jboss5 is my application server Commented Dec 19, 2013 at 9:14

1 Answer 1

16

To compress your String you can use:

public static String compress(String str) throws IOException {
    if (str == null || str.length() == 0) {
        return str;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(str.getBytes());
    gzip.close();
    String outStr = out.toString("UTF-8");
    return outStr;
 }

GZIPOutputStream is from java.util.zip

Most browsers should be able to handle gzip compressed content without the need of manual decompression.

Docs: GZIPOutputStream

See Loading GZIP JSON file using AJAX if you're using Ajax for the data acquisition on the client-side. It is necessary to set the headers for your response as @hgoebl mentioned.

Sign up to request clarification or add additional context in comments.

8 Comments

Sorry, but this should not be done in your code. Webserver or Tomcat or a ServletFilter should do the job. BTW encoding the GZIP bytes ISO-8859-1 is simply wrong.
Is he mentioning any application server? He explicitly asked how to compress some content and that's what was provided in the answer. However you're totally right on the ISO part. I'll edit it to UTF-8. Thanks
It is still the wrong way and only the very small part of the work. How would he uncompress this client-side? Use a GZIP JavaScript library? This should be handled transparently by Webserver or Tomcat/Jersey/RESTeasy/... and web browser on client-side.
Actually we can both probably agree that the way to do this is progressive loading of the JSON data, not compression. I doubt that he needs all of the data immediately after load. But decompression should be handled automatically by the browser. For example after recieving it from an Ajax call the browser does everything necessary to show the content correctly after identifying the header from the recieved data, as far as I know. Or am I wrong?
I agree an the first part. He should not send all data but use some kind of paging or master-detail lookup. But the way you compress is not visible to the browser. He'll only get unparsable rubbish. You have to set HTTP header so the browser knows that the payload is compressed.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.