GzipProcessorUtils { public static ByteBuf fromGzip(ByteBuf src, int maxMessageSize) throws ParseException { if (CHECK) checkArgument(src.readRemaining() > 0); int expectedSize = readExpectedInputSize(src); check(expectedSize >= 0, src, INCORRECT_UNCOMPRESSED_INPUT_SIZE); check(expectedSize <= maxMessageSize, src, DECOMPRESSED_SIZE_EXCEEDS_EXPECTED_MAX_SIZE); processHeader(src); ByteBuf dst = ByteBufPool.allocate(expectedSize); Inflater decompressor = ensureDecompressor(); decompressor.setInput(src.array(), src.head(), src.readRemaining()); try { readDecompressedData(decompressor, src, dst, maxMessageSize); } catch (DataFormatException ignored) { moveDecompressorToPool(decompressor); src.recycle(); dst.recycle(); throw DATA_FORMAT_EXCEPTION; } moveDecompressorToPool(decompressor); check(expectedSize == dst.readRemaining(), src, dst, ACTUAL_DECOMPRESSED_DATA_SIZE_IS_NOT_EQUAL_TO_EXPECTED); check(src.readRemaining() == GZIP_FOOTER_SIZE, src, dst, COMPRESSED_DATA_WAS_NOT_READ_FULLY); src.recycle(); return dst; }  static ByteBuf fromGzip(ByteBuf src, int maxMessageSize); static ByteBuf toGzip(ByteBuf src); static final ParseException CORRUPTED_GZIP_HEADER; static final ParseException DECOMPRESSED_SIZE_EXCEEDS_EXPECTED_MAX_SIZE; static final ParseException COMPRESSED_DATA_WAS_NOT_READ_FULLY; static final ParseException DATA_FORMAT_EXCEPTION; static final ParseException ACTUAL_DECOMPRESSED_DATA_SIZE_IS_NOT_EQUAL_TO_EXPECTED; static final ParseException INCORRECT_ID_HEADER_BYTES; static final ParseException INCORRECT_UNCOMPRESSED_INPUT_SIZE; static final ParseException UNSUPPORTED_COMPRESSION_METHOD; }
@Test public void recycleByteBufInCaseOfBadInput() throws ParseException { expectedException.expect(ParseException.class); expectedException.expectMessage("Corrupted GZIP header"); ByteBuf badBuf = ByteBufPool.allocate(100); badBuf.put(new byte[]{-1, -1, -1, -1, -1, -1}); fromGzip(badBuf, 11_000_000); }
@Test public void testGzipOutputStreamDataIsCorrectlyDecoded() throws IOException, ParseException { ByteBuf encodedData = encodeWithGzipOutputStream(wrapUtf8(text)); ByteBuf decoded = fromGzip(encodedData, 11_000_000); assertEquals(text, decoded.asString(UTF_8)); }