netty/netty — pull request #2269 by bucjac (2014-02-27 19:02:13+00:00)
Title: Add ZLIB_OR_NONE support to JdkZlibDecoder [#2016]

I attempted to resolve #2016. I am trying to detect if a stream is a zlib stream by looking at its first two bytes. Obviously this heuristic is not perfect as such a 'valid' byte combination may also appear in a normal DEFLATE stream ... this is however quite unlikely.

According to the spec (http://tools.ietf.org/html/rfc1950#section-2.2), the first byte of a ZLIB stream is 78 (7 means that the sliding window size is 32KB (ld(32*1024) - 8 is the formular) and 8 stands for the DEFLATE algorithm. The second byte contains some more information that are ignored however and I just use it to ensure that "... when viewed as a 16-bit unsigned integer stored in MSB order (CMF*256 + FLG) is a multiple of 31."

Signed/Unsigned shouldn't be an issue here as the theoretical biggest possible short value 0x78FF is still in the range of a 16bit signed integer.