0

I use the code below to decompress coming data that is arrived via socket.io.

When decompressing data which has been sent as compressed the code below works great. (Pseudo data which is sent from Node.js: <Buffer a8 b2 ...>)

However when decompressing data which has been sent in a JSONArray I get java.io.IOException: unknown format error. (Pseudo data which is sent from Node.js: [<Buffer a8 b2 ..>, <Buffer c4 f0 ..>])

mSocket.on("fired", new Emitter.Listener() {
    @Override
    public void call(final Object... args) {
        JSONArray compressedDataArray = (JSONArray) args[0];
        byte[] compressedData = compressedDataArray.getString(0).toString().getBytes();
        String unzipped = new String(decompress(compressedData));
        Log.v("SAMPLE_TAG", "unzipped: " + unzipped) // LOGGING THE RESULT
    }
})


public byte[] decompress(byte[] data) throws IOException {
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    GZIPInputStream gzipIS = new GZIPInputStream(new ByteArrayInputStream(data));

    byte[] buffer = new byte[1024];

    int len;
    while ((len = gzipIS.read(buffer)) > 0) {
      byteArrayOS.write(buffer, 0, len);
    }

    byteArrayOS.close();
    gzipIS.close();

    return byteArrayOS.toByteArray();
  }

Is there any chance to overcome this issue?

efkan
  • 12,991
  • 6
  • 73
  • 106
  • What's the reason for decompressing an uncompressed JSONArray? – andih May 25 '17 at 04:26
  • I've store data as compressed on Redis. In some cases I have to return more than one compressed data to the user. Because of this, I add the compressed data into an array then send. – efkan May 25 '17 at 04:31
  • Another remark `compressedDataArray.getString(0).toString()` the last `toString()` seems to be redundant. `getString(index)` already returns a `String` – andih May 25 '17 at 04:34
  • You have a JSON Array which contains compressed data. 1) I don't see that you iterate over the JSON Array. 2) Independent of you have one or n compressed elements, send them as JSON Array which contains n elements of compressed data so you can treat them equally. – andih May 25 '17 at 04:40
  • Thanks but this is not important in this case. Thanks anyway.. – efkan May 25 '17 at 04:41
  • I've just written an example. `byte[] compressedData = compressedDataArray.getString(0).getBytes();` and `String unzipped = new String(decompress(compressedData));` lines enough to reproduce the exception. That's the reason. 2) Why not if I can decompression. Thank you again.. – efkan May 25 '17 at 04:43
  • 1
    You can't decompress an uncompressed array even when the elements are compress. You can only decompress compressed elements. One after another. Use always the same structure / abstraction for your data so you can easily handle them. A possible abstraction is always to use an array of compressed elements independent of number of elements. – andih May 25 '17 at 05:25
  • Thank you for your attention. You spent your time with me about this issue. I've added an answer which works. Have a fabulous day.. – efkan May 25 '17 at 05:31

1 Answers1

0

I've overcome this issue by changing the line below:

byte[] compressedData = compressedDataArray.getString(0).toString().getBytes();

changed as:

byte[] compressedData = (byte[]) compressedDataArray.get(0);

Then it works.

efkan
  • 12,991
  • 6
  • 73
  • 106
  • It's the call .toString() that makes the first statement fail. Just remove it and it should work just fine. – ichalos Jul 12 '18 at 15:56