2

I must be doing something really wrong. Running following code on Android it produces truncated file (_items_) without any exceptions or problems in the log. Running the same code with OpenJDK 7 it decompresses the file correctly.

try {
    final InputStream fis = new GZIPInputStream(new FileInputStream("/storage/sdcard/_items"));
    try {
        final FileOutputStream fos = new FileOutputStream("/storage/sdcard/_items_");
        try {
            final byte[] buffer = new byte[1024];
            int n;
            while ((n = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, n);
            }
        } finally {
            fos.close();
        }
    } finally {
        fis.close();
    }
} catch (final IOException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
}

I've tried this with Android emulator (API 18) and on Desire HD (Android 2.3.5) with the same buggy result.

Input file (_items): https://drive.google.com/file/d/0B6M72P2gzYmwaHg4SzRTYnRMOVk/edit?usp=sharing

Android truncated output file (_items_): https://drive.google.com/file/d/0B6M72P2gzYmwMUZIZ2FEaHNZUFk/edit?usp=sharing

Martin Ždila
  • 2,998
  • 3
  • 31
  • 35
  • I see the same behavior. Please file a bug on http://b.android.com/. – fadden Dec 18 '13 at 19:22
  • Done - https://code.google.com/p/android/issues/detail?id=63873. Any idea for workaround? – Martin Ždila Dec 18 '13 at 19:51
  • I don't know if it helps you, but recompressing the file (gunzip then gzip) resulted in a compressed data file that was correctly handled by my device. What did you use to compress the data? – fadden Dec 18 '13 at 20:18
  • File _items_ was obtained from Glassfish 4 REST (Jersey) HTTP Response with: Transfer-Encoding: chunked Content-Encoding: gzip Actually file was only created from raw body of HTTP response for debugging purpose only. My original code was InputStream is = new GZIPInputStream(httpUrlConnection.getInputStream()); – Martin Ždila Dec 18 '13 at 20:57
  • I've tried to use com.jcraft/jzlib/1.1.3 but it has exactly the same problem. Maybe Android uses it too. – Martin Ždila Dec 19 '13 at 09:53

2 Answers2

1

The AOSP bug has been updated with analysis by an engineer from the Dalvik team.

Summary: the gzip stream has multiple members concatenated together, and the decompressor is expected to process them all, but Dalvik's implementation is stopping after the first.

Unless there's a way to convince the data source to compress its streams differently, you will need to find a replacement for GZIPInputStream.

fadden
  • 51,356
  • 5
  • 116
  • 166
1

Workaround is to use GZIPInputStream from JZlib (currently only in concatenated_gzip_streams branch). See https://github.com/ymnk/jzlib/issues/12 for more details.

Martin Ždila
  • 2,998
  • 3
  • 31
  • 35