Convert zlib compressed inputStream to byte array

I have a Liberty 16.0.0.4 servlet consuming zlib compressed application_json. I want to take the input stream and convert it to a byte array.

@POST
@Consumes({ MediaType.APPLICATION_JSON })
public Response feedData(@Context final HttpServletRequest ctx, final InputStream content) {
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
final byte[] data = new byte[16384];
try {
while ((nRead = content.read(data, 0, data.length)) != -1) { //Fails
buffer.write(data, 0, nRead);
}
} catch (final IOException e) {
e.printStackTrace();
}

However, this is failing because it is trying to decompress it as gzip. I would prefer to not even decompress the data (I’m just passing on the byte[] to kafka). Is there a way to do the inputStream read into a bytearray without having it be decompressed and avoid that gzip error? The error is because the data is not gzip, it’s zlib (whose header starts with 120, or hex 78).

com.ibm.wsspi.http.channel.exception.IllegalHttpBodyException: Invalid gzip header, first byte=120
at com.ibm.ws.http.channel.internal.HttpServiceContextImpl.moveBuffers(HttpServiceContextImpl.java:3603)
at com.ibm.ws.http.channel.internal.HttpServiceContextImpl.readBodyBuffer(HttpServiceContextImpl.java:3474)
at com.ibm.ws.http.channel.internal.inbound.HttpInboundServiceContextImpl.getRequestBodyBuffer(HttpInboundServiceContextImpl.java:1542)
at com.ibm.ws.http.channel.internal.inbound.HttpInputStreamImpl.checkBuffer(HttpInputStreamImpl.java:101)
at com.ibm.ws.http.channel.internal.inbound.HttpInputStreamImpl.read(HttpInputStreamImpl.java:181)
at com.ibm.ws.webcontainer.srt.SRTInputStream.read(SRTInputStream.java:68)
at com.ibm.service.rest.MainIncomingHandler.produce(MainIncomingHandler.java:280)

Related:

Leave a Reply