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.

martoncsukas
  • 2,077
  • 20
  • 23
user3118357
  • 141
  • 1
  • 1
  • 4
  • See here http://json.org/java/, and here https://developer.mozilla.org/en-US/docs/JSON – elclanrs Dec 19 '13 at 08:38
  • 3
    Did you mean encode/decode or you meant compress/decompress? I am confused – Akshay Khandelwal Dec 19 '13 at 08: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. – GitaarLAB Dec 19 '13 at 08:39
  • Which webserver do you use? – hgoebl Dec 19 '13 at 08: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 – user3118357 Dec 19 '13 at 09:14

1 Answers1

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.

Community
  • 1
  • 1
Dropout
  • 13,653
  • 10
  • 56
  • 109
  • 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. – hgoebl Dec 19 '13 at 08:49
  • 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 – Dropout Dec 19 '13 at 08:52
  • 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. – hgoebl Dec 19 '13 at 08:55
  • 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? – Dropout Dec 19 '13 at 09:00
  • 3
    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. – hgoebl Dec 19 '13 at 09:02
  • 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. – user3118357 Dec 19 '13 at 09:07
  • True. I would give you one of the +1s if I could ;) I've edited it in. If you construct a more precise answer make sure you post it, I will gladly +1 you. – Dropout Dec 19 '13 at 09:08
  • I want to populate number of positions with information on google map from javascript, but the data is being provided to java script by the servercode in JSON. While the transfer is being done it is taking long time to load the map on browser – user3118357 Dec 19 '13 at 09:30