views:

450

answers:

1

There is a strange problem I've run in using RIM compression API, I can't make it work as it's described in documentation.
If I gzip plain text file using win gzip tool, add gz to resources of blackberry project and in app try to decompress it, there will be infinite loop, gzis.read() never return -1...

try
{
    InputStream inputStream = getClass().getResourceAsStream("test.gz");
    GZIPInputStream gzis = new GZIPInputStream(inputStream);
    StringBuffer sb = new StringBuffer();

    char c;
    while ((c = (char)gzis.read()) != -1)           
    {
        sb.append(c);
    }

    String data = sb.toString();
    add(new RichTextField(data));
    gzis.close();
}
catch(IOException ioe)
{
}

After the compressed content there is repetition of 65535 value in gzis.read(). The only workaround I've found is dumb

while ((c = (char)gzis.read()) != -1 && c != 65535)

But I'm curious what is the reason, what I'm doing wrong, and why 65535?

+2  A: 

char is an unsigned, 16-bit data type. -1 cast to a char is 65535.

Change to:

int i;
while ((i = gzis.read()) != -1)           
{
  sb.append((char)i);
}

And it should work. The example on RIM's API can't possibly work, as no char will ever equal -1.

Kevin Montrose
Thanks Kevin, RIM demystified! I should be more careful :)
Max Gontar
Omg.. old question, but helped me just today.
Henrik P. Hessel