Skip to content

Commit f755edc

Browse files
committed
Support output buffer in read_partial/readpartial.
1 parent a472da1 commit f755edc

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

lib/protocol/http/body/stream.rb

+16-6
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,22 @@ def read(length = nil, buffer = nil)
8989
# Will avoid reading from the underlying stream if there is buffered data available.
9090
#
9191
# @parameter length [Integer] The maximum number of bytes to read.
92-
def read_partial(length = nil)
92+
def read_partial(length = nil, buffer = nil)
9393
if @buffer
94-
buffer = @buffer
95-
@buffer = nil
94+
if buffer
95+
buffer.replace(@buffer)
96+
else
97+
buffer = @buffer
98+
@buffer = nil
99+
end
96100
else
97-
buffer = read_next
101+
chunk = read_next
102+
103+
if buffer and chunk
104+
buffer.replace(chunk)
105+
else
106+
buffer = chunk
107+
end
98108
end
99109

100110
if buffer and length
@@ -111,8 +121,8 @@ def read_partial(length = nil)
111121
end
112122

113123
# Similar to {read_partial} but raises an `EOFError` if the stream is at EOF.
114-
def readpartial(length)
115-
read_partial(length) or raise EOFError, "End of file reached!"
124+
def readpartial(length, buffer = nil)
125+
read_partial(length, buffer) or raise EOFError, "End of file reached!"
116126
end
117127

118128
# Read data from the stream without blocking if possible.

test/protocol/http/body/stream.rb

+10
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,14 @@
224224
expect(stream).to be(:closed?)
225225
end
226226
end
227+
228+
with 'IO.copy_stream' do
229+
let(:output) {StringIO.new}
230+
231+
it "can copy input to output" do
232+
::IO.copy_stream(stream, output)
233+
234+
expect(output.string).to be == "HelloWorld"
235+
end
236+
end
227237
end

0 commit comments

Comments
 (0)