@@ -223,7 +223,7 @@ def read(self, b=-1):
223
223
if self .closed :
224
224
raise ValueError ("I/O operation on closed file" )
225
225
226
- if b :
226
+ if b >= 0 :
227
227
self ._read_bytes (b )
228
228
output .write (self .output_buffer [:b ])
229
229
self .output_buffer = self .output_buffer [b :]
@@ -539,13 +539,19 @@ def _read_bytes_to_framed_body(self, b):
539
539
"""
540
540
_LOGGER .debug ("collecting %s bytes" , b )
541
541
_b = b
542
- b = int (math .ceil (b / float (self .config .frame_length )) * self .config .frame_length )
543
- _LOGGER .debug ("%s bytes requested; reading %s bytes after normalizing to frame length" , _b , b )
542
+
543
+ if b > 0 :
544
+ _frames_to_read = math .ceil (b / float (self .config .frame_length ))
545
+ b = int (_frames_to_read * self .config .frame_length )
546
+ _LOGGER .debug ("%d bytes requested; reading %d bytes after normalizing to frame length" , _b , b )
547
+
544
548
plaintext = self .source_stream .read (b )
545
- _LOGGER .debug ("%s bytes read from source" , len (plaintext ))
549
+ plaintext_length = len (plaintext )
550
+ _LOGGER .debug ("%d bytes read from source" , plaintext_length )
551
+
546
552
finalize = False
547
553
548
- if len ( plaintext ) < b :
554
+ if b < 0 or plaintext_length < b :
549
555
_LOGGER .debug ("Final plaintext read from source" )
550
556
finalize = True
551
557
@@ -594,8 +600,8 @@ def _read_bytes(self, b):
594
600
:param int b: Number of bytes to read
595
601
:raises NotSupportedError: if content type is not supported
596
602
"""
597
- _LOGGER .debug ("%s bytes requested from stream with content type: %s" , b , self .content_type )
598
- if b <= len (self .output_buffer ) or self .source_stream .closed :
603
+ _LOGGER .debug ("%d bytes requested from stream with content type: %s" , b , self .content_type )
604
+ if 0 <= b <= len (self .output_buffer ) or self .source_stream .closed :
599
605
_LOGGER .debug ("No need to read from source stream or source stream closed" )
600
606
return
601
607
@@ -858,7 +864,8 @@ def _read_bytes(self, b):
858
864
_LOGGER .debug ("Source stream closed" )
859
865
return
860
866
861
- if b <= len (self .output_buffer ):
867
+ buffer_length = len (self .output_buffer )
868
+ if 0 <= b <= buffer_length :
862
869
_LOGGER .debug (
863
870
"%s bytes requested less than or equal to current output buffer size %s" , b , len (self .output_buffer )
864
871
)
0 commit comments