6969// -----------------------------------------------------------------------
7070
7171using System ;
72+ using System . Buffers ;
7273using SharpCompress . Algorithms ;
7374using SharpCompress . Common ;
7475
@@ -1726,9 +1727,12 @@ CompressionStrategy strategy
17261727 hash_mask = hash_size - 1 ;
17271728 hash_shift = ( ( hash_bits + MIN_MATCH - 1 ) / MIN_MATCH ) ;
17281729
1729- window = new byte [ w_size * 2 ] ;
1730- prev = new short [ w_size ] ;
1731- head = new short [ hash_size ] ;
1730+ window = ArrayPool < byte > . Shared . Rent ( w_size * 2 ) ;
1731+ prev = ArrayPool < short > . Shared . Rent ( w_size ) ;
1732+ head = ArrayPool < short > . Shared . Rent ( hash_size ) ;
1733+ Array . Clear ( window , 0 , w_size * 2 ) ;
1734+ Array . Clear ( prev , 0 , w_size ) ;
1735+ Array . Clear ( head , 0 , hash_size ) ;
17321736
17331737 // for memLevel==8, this will be 16384, 16k
17341738 lit_bufsize = 1 << ( memLevel + 6 ) ;
@@ -1737,7 +1741,8 @@ CompressionStrategy strategy
17371741 // the output distance codes, and the output length codes (aka tree).
17381742 // orig comment: This works just fine since the average
17391743 // output size for (length,distance) codes is <= 24 bits.
1740- pending = new byte [ lit_bufsize * 4 ] ;
1744+ pending = ArrayPool < byte > . Shared . Rent ( lit_bufsize * 4 ) ;
1745+ Array . Clear ( pending , 0 , lit_bufsize * 4 ) ;
17411746 _distanceOffset = lit_bufsize ;
17421747 _lengthOffset = ( 1 + 2 ) * lit_bufsize ;
17431748
@@ -1776,20 +1781,46 @@ internal void Reset()
17761781
17771782 internal int End ( )
17781783 {
1784+ var result = ZlibConstants . Z_OK ;
17791785 if ( status != INIT_STATE && status != BUSY_STATE && status != FINISH_STATE )
17801786 {
1781- return ZlibConstants . Z_STREAM_ERROR ;
1787+ result = ZlibConstants . Z_STREAM_ERROR ;
1788+ }
1789+ else if ( status == BUSY_STATE )
1790+ {
1791+ result = ZlibConstants . Z_DATA_ERROR ;
17821792 }
17831793
17841794 // Deallocate in reverse order of allocations:
1785- pending = null ;
1786- head = null ;
1787- prev = null ;
1788- window = null ;
1795+ ReturnBuffers ( ) ;
17891796
17901797 // free
17911798 // dstate=null;
1792- return status == BUSY_STATE ? ZlibConstants . Z_DATA_ERROR : ZlibConstants . Z_OK ;
1799+ return result ;
1800+ }
1801+
1802+ private void ReturnBuffers ( )
1803+ {
1804+ if ( pending is not null )
1805+ {
1806+ ArrayPool < byte > . Shared . Return ( pending , clearArray : true ) ;
1807+ pending = null ;
1808+ }
1809+ if ( head is not null )
1810+ {
1811+ ArrayPool < short > . Shared . Return ( head , clearArray : true ) ;
1812+ head = null ;
1813+ }
1814+ if ( prev is not null )
1815+ {
1816+ ArrayPool < short > . Shared . Return ( prev , clearArray : true ) ;
1817+ prev = null ;
1818+ }
1819+ if ( window is not null )
1820+ {
1821+ ArrayPool < byte > . Shared . Return ( window , clearArray : true ) ;
1822+ window = null ;
1823+ }
17931824 }
17941825
17951826 private void SetDeflater ( ) =>
0 commit comments