javasocket.iogzipgzipinputstreamsocket.io-java-client

java.io.IOException: unknown format


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?


Solution

  • 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.