public static String getByteArrayDataAsString ( String contentEncoding , byte [ ] bytes ) { ByteArrayOutputStream byteout = null ; if ( contentEncoding != null && contentEncoding . equals ( "gzip" ) ) { // GZIP ByteArrayInputStream bytein = null ; GZIPInputStream zis = null ; try { bytein = new ByteArrayInputStream ( bytes ) ; zis = new GZIPInputStream ( bytein ) ; byteout = new ByteArrayOutputStream ( ) ; int res = 0 ; byte buf [ ] = new byte [ 1024 ] ; while ( res >= 0 ) { res = zis . read ( buf , 0 , buf . length ) ; if ( res > 0 ) { byteout . write ( buf , 0 , res ) ; } } zis . close ( ) ; bytein . close ( ) ; byteout . close ( ) ; return byteout . toString ( ) ; } catch ( Exception e ) { // No action to take } } else if ( contentEncoding != null && contentEncoding . equals ( "deflate" ) ) { try { // DEFLATE byte [ ] buffer = new byte [ 1024 ] ; Inflater decompresser = new Inflater ( ) ; byteout = new ByteArrayOutputStream ( ) ; decompresser . setInput ( bytes ) ; while ( ! decompresser . finished ( ) ) { int count = decompresser . inflate ( buffer ) ; byteout . write ( buffer , 0 , count ) ; } byteout . close ( ) ; decompresser . end ( ) ; return byteout . toString ( ) ; } catch ( Exception e ) { // No action to take } } return new String ( bytes ) ; }
Decodes stream data based on content encoding