@@ -127,13 +127,12 @@ def add_chunk(self, data, compress=None):
127127 snappy's performance. If compress == True, compression always happens,
128128 and if compress == False, compression never happens.
129129 """
130+ out = bytearray ()
130131 if not self ._header_chunk_written :
131132 self ._header_chunk_written = True
132- out = [struct .pack ("<L" , _IDENTIFIER_CHUNK +
133- (len (_STREAM_IDENTIFIER ) << 8 )),
134- _STREAM_IDENTIFIER ]
135- else :
136- out = []
133+ out .extend (struct .pack ("<L" , _IDENTIFIER_CHUNK +
134+ (len (_STREAM_IDENTIFIER ) << 8 )))
135+ out .extend (_STREAM_IDENTIFIER )
137136 for i in range (0 , len (data ), _CHUNK_MAX ):
138137 chunk = data [i :i + _CHUNK_MAX ]
139138 crc = _masked_crc32c (chunk )
@@ -151,10 +150,10 @@ def add_chunk(self, data, compress=None):
151150 chunk_type = _COMPRESSED_CHUNK
152151 else :
153152 chunk_type = _UNCOMPRESSED_CHUNK
154- out .append (struct .pack ("<LL" , chunk_type + ((len (chunk ) + 4 ) << 8 ),
153+ out .extend (struct .pack ("<LL" , chunk_type + ((len (chunk ) + 4 ) << 8 ),
155154 crc ))
156- out .append (chunk )
157- return b"" . join (out )
155+ out .extend (chunk )
156+ return bytes (out )
158157
159158 def compress (self , data ):
160159 """This method is simply an alias for compatibility with zlib
@@ -193,7 +192,7 @@ class StreamDecompressor(object):
193192 __slots__ = ["_buf" , "_header_found" ]
194193
195194 def __init__ (self ):
196- self ._buf = b""
195+ self ._buf = bytearray ()
197196 self ._header_found = False
198197
199198 @staticmethod
@@ -222,11 +221,11 @@ def decompress(self, data):
222221 the decompress() method. Some of the input data may be preserved in
223222 internal buffers for later processing.
224223 """
225- self ._buf += data
226- uncompressed = []
224+ self ._buf . extend ( data )
225+ uncompressed = bytearray ()
227226 while True :
228227 if len (self ._buf ) < 4 :
229- return b"" . join (uncompressed )
228+ return bytes (uncompressed )
230229 chunk_type = struct .unpack ("<L" , self ._buf [:4 ])[0 ]
231230 size = (chunk_type >> 8 )
232231 chunk_type &= 0xff
@@ -240,7 +239,7 @@ def decompress(self, data):
240239 raise UncompressError (
241240 "stream received unskippable but unknown chunk" )
242241 if len (self ._buf ) < 4 + size :
243- return b"" . join (uncompressed )
242+ return bytes (uncompressed )
244243 chunk , self ._buf = self ._buf [4 :4 + size ], self ._buf [4 + size :]
245244 if chunk_type == _IDENTIFIER_CHUNK :
246245 if chunk != _STREAM_IDENTIFIER :
@@ -256,7 +255,7 @@ def decompress(self, data):
256255 chunk = _uncompress (chunk )
257256 if struct .pack ("<L" , _masked_crc32c (chunk )) != crc :
258257 raise UncompressError ("crc mismatch" )
259- uncompressed . append ( chunk )
258+ uncompressed += chunk
260259
261260 def flush (self ):
262261 """All pending input is processed, and a string containing the
@@ -274,7 +273,7 @@ def copy(self):
274273 to speed up random seeks into the stream at a future point.
275274 """
276275 copy = StreamDecompressor ()
277- copy ._buf , copy ._header_found = self ._buf , self ._header_found
276+ copy ._buf , copy ._header_found = bytearray ( self ._buf ) , self ._header_found
278277 return copy
279278
280279
0 commit comments