lib/protocol/http/body/stream.rb in protocol-http-0.28.1 vs lib/protocol/http/body/stream.rb in protocol-http-0.28.2

- old
+ new

@@ -87,16 +87,29 @@ # If the length is given, at most length bytes will be read. Otherwise, one chunk of data from the underlying stream will be read. # # Will avoid reading from the underlying stream if there is buffered data available. # # @parameter length [Integer] The maximum number of bytes to read. - def read_partial(length = nil) + def read_partial(length = nil, buffer = nil) if @buffer - buffer = @buffer + if buffer + buffer.replace(@buffer) + else + buffer = @buffer + end @buffer = nil else - buffer = read_next + if chunk = read_next + if buffer + buffer.replace(chunk) + else + buffer = chunk + end + else + buffer&.clear + buffer = nil + end end if buffer and length if buffer.bytesize > length # This ensures the subsequent `slice!` works correctly. @@ -109,11 +122,11 @@ return buffer end # Similar to {read_partial} but raises an `EOFError` if the stream is at EOF. - def readpartial(length) - read_partial(length) or raise EOFError, "End of file reached!" + def readpartial(length, buffer = nil) + read_partial(length, buffer) or raise EOFError, "End of file reached!" end # Read data from the stream without blocking if possible. def read_nonblock(length, buffer = nil, exception: nil) @buffer ||= read_next