module Protocol::HTTP::Body::Stream::Reader

def read_nonblock(length, buffer = nil, exception: nil)

@parameter buffer [String | Nil] The buffer to read into.
@parameter length [Integer] The maximum number of bytes to read.

Read data from the stream without blocking if possible.
def read_nonblock(length, buffer = nil, exception: nil)
	@buffer ||= read_next
	chunk = nil
	
	unless @buffer
		buffer&.clear
		return
	end
	
	if @buffer.bytesize > length
		chunk = @buffer.byteslice(0, length)
		@buffer = @buffer.byteslice(length, @buffer.bytesize)
	else
		chunk = @buffer
		@buffer = nil
	end
	
	if buffer
		buffer.replace(chunk)
	else
		buffer = chunk
	end
	
	return buffer
end