'Distinguishing GZIP and non-GZIP format doesn't work

I'm working on an NBT scanner software. It is supposed to read NBT data from files both with or without GZIP compression. I use the following code to determine if the file is compressed in GZIP format. Assuming it is valid. (I'm using a FileInputStream extension that supports mark/reset)

public static InputStream GZIPCheck(InputStream in) throws IOException{
    in.mark(1);
    int header = in.read();
    in.reset();
    if(header > Byte.MAX_VALUE){
        return new GZIPInputStream(in);
    }
    return in;
}

The NBT format's prefix is a signed short (any value) that cannot be negative and the GZIP format starts with a negative short:

public final static int GZIP_MAGIC = 0x8b1f;

That makes it easy to distinguish between the two. I read the first byte, if it is negative, (unsigned version is bigger than Byte.MAX_VALUE) then it can't be NBT, so I try the GZIPInputStream. (If it fails, then the file is not valid.) If the first byte is positive, (or zero) then it can't be GZIP since it is inconsistent with GZIP_MAGIC, so I don't use it.

Here's the thing: My method always returns the in parameter no matter what valid file I read. Why?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source