简体   繁体   English

java.io.IOException:未知格式

[英]java.io.IOException: unknown format

I use the code below to decompress coming data that is arrived via socket.io . 我使用下面的代码解压缩通过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 ...> ) (从Node.js发送的伪数据: <Buffer a8 b2 ...>

However when decompressing data which has been sent in a JSONArray I get java.io.IOException: unknown format error. 但是,当解压缩以JSONArray发送的数据时,我得到java.io.IOException: unknown format错误。 (Pseudo data which is sent from Node.js: [<Buffer a8 b2 ..>, <Buffer c4 f0 ..>] ) (从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? 有机会克服这个问题吗?

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. 然后就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM