GZIPInputStream fails with IOException in Android 2.3, but works fine in all previous releases?

后端 未结 1 997
囚心锁ツ
囚心锁ツ 2021-01-06 02:59

I updated my phone to Gingerbread today (2.3.2) and fired up an app I developed and saw that it failed to load its data. The app runs fine on every other version of Android

相关标签:
1条回答
  • 2021-01-06 03:12

    I'm running into the same issue here. It looks like Gingerbread (2.3) changed the way GZipped streams are handled. Looking at the "magic block" characters indicates that openStream() automatically detects GZipped data and runs it through the correct stream decoder. Of course, if you attempt to run another GZIP decoder on the same stream, that will fail with an IOException.

    There are a couple ways to handle this. The first way is to switch to HttpClient/HttpGet. BUT there is no guarantee that this also won't change in the future. Basically, that is a hack to get it working again. A more complete solution might be to do:

    InputStream in = url.openStream();
    GZIPInputStream zin;
    try {
        zin = (GZIPInputStream)in;
    } catch (Exception e) {
        zin = new GZIPInputStream(in);
    }
    

    For older versions of Android, an exception fires while attempting the cast and, on newer versions of Android, the cast succeeds. Abusing an exception handler this way is not pretty but it works.

    This, of course, will be a problem for underlying data that is compressed or binary data that looks like it is GZIP compressed data. The Android devs think this change isn't a bug:

    http://code.google.com/p/android/issues/detail?id=16227

    I disagree. This is severe breakage.

    0 讨论(0)
提交回复
热议问题