@@ -479,40 +479,41 @@ public static byte[] setBit(byte[] data, int pos, int val) {
479479
480480 public static byte [] compress (byte [] data ) throws EventBloomException {
481481 Deflater deflater = new Deflater ();
482- deflater .setInput (data );
483482
484- ByteArrayOutputStream outputStream = new ByteArrayOutputStream (data .length );
483+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream (data .length )) {
484+ deflater .setInput (data );
485+ deflater .finish ();
486+ byte [] buffer = new byte [1024 ];
485487
486- deflater .finish ();
487- byte [] buffer = new byte [1024 ];
488- while (!deflater .finished ()) {
489- int count = deflater .deflate (buffer ); // returns the generated code... index
490- outputStream .write (buffer , 0 , count );
491- }
492- try {
493- outputStream .close ();
488+ while (!deflater .finished ()) {
489+ int count = deflater .deflate (buffer ); // returns the generated code... index
490+ outputStream .write (buffer , 0 , count );
491+ }
492+
493+ return outputStream .toByteArray ();
494494 } catch (IOException e ) {
495495 throw new EventBloomException ("compress data failed" );
496+ } finally {
497+ deflater .end ();
496498 }
497- byte [] output = outputStream .toByteArray ();
498-
499- return output ;
500499 }
501500
502501 public static byte [] decompress (byte [] data ) throws IOException , DataFormatException {
503502 Inflater inflater = new Inflater ();
504- inflater .setInput (data );
505503
506- ByteArrayOutputStream outputStream = new ByteArrayOutputStream (data .length );
507- byte [] buffer = new byte [ 1024 ] ;
508- while (! inflater . finished ()) {
509- int count = inflater . inflate ( buffer );
510- outputStream . write ( buffer , 0 , count );
511- }
512- outputStream .close ( );
513- byte [] output = outputStream . toByteArray ();
504+ try ( ByteArrayOutputStream outputStream = new ByteArrayOutputStream (data .length )) {
505+ inflater . setInput ( data ) ;
506+ byte [] buffer = new byte [ 1024 ];
507+
508+ while (! inflater . finished ()) {
509+ int count = inflater . inflate ( buffer );
510+ outputStream .write ( buffer , 0 , count );
511+ }
514512
515- return output ;
513+ return outputStream .toByteArray ();
514+ } finally {
515+ inflater .end ();
516+ }
516517 }
517518
518519}
0 commit comments