Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions lib/rb/lib/thrift/transport/buffered_transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ def read(sz)
ret = @rbuf.slice(@index - sz, sz) || Bytes.empty_byte_buffer

if ret.length == 0
@rbuf = @transport.read([sz, DEFAULT_BUFFER].max)
# An empty result tells read_all that the input ended.
@rbuf = @transport.read([sz, DEFAULT_BUFFER].max) || Bytes.empty_byte_buffer
@index = sz
ret = @rbuf.slice(0, sz) || Bytes.empty_byte_buffer
end
Expand All @@ -90,10 +91,7 @@ def read_byte

begin
# If the read buffer is exhausted, try to read up to DEFAULT_BUFFER more bytes into it.
if @index >= @rbuf.size
@rbuf = @transport.read(DEFAULT_BUFFER)
@index = 0
end
fill_read_buffer if @index >= @rbuf.size

# The read buffer has some data now, read a single byte. Using get_string_byte() avoids
# allocating a temp string of size 1 unnecessarily.
Expand All @@ -114,10 +112,7 @@ def read_into_buffer(buffer, size)
i = 0
while i < size
# If the read buffer is exhausted, try to read up to DEFAULT_BUFFER more bytes into it.
if @index >= @rbuf.size
@rbuf = @transport.read(DEFAULT_BUFFER)
@index = 0
end
fill_read_buffer if @index >= @rbuf.size

# The read buffer has some data now, so copy bytes over to the output buffer.
byte = Bytes.get_string_byte(@rbuf, @index)
Expand Down Expand Up @@ -156,6 +151,14 @@ def to_s

private

# Raises at the end of the input instead of reading past the buffer. The
# underlying transport may return nil or an empty string there.
def fill_read_buffer
@rbuf = @transport.read(DEFAULT_BUFFER) || Bytes.empty_byte_buffer
@index = 0
raise TransportException.new(TransportException::END_OF_FILE, "No more data available") if @rbuf.empty?
end

# Replace exhausted storage; clearing it in place could change returned slices.
def clear_consumed_read_buffer
unless @rbuf.empty?
Expand Down
35 changes: 35 additions & 0 deletions lib/rb/spec/buffered_transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,41 @@ def flush
expect(sink).to receive(:close).ordered.and_raise(IOError, "close failed")
expect { transport.close }.to raise_error(IOError, "close failed")
end

describe "at the end of the underlying input" do
# IOStreamTransport#read returns nil at EOF, as IO#read does; other transports may return "".
def each_input
["", "ab"].each do |data|
stream = Thrift::IOStreamTransport.new(StringIO.new(data.b), StringIO.new)
custom = double("Transport")
allow(custom).to receive(:read).and_return(data.b, "".b)
[stream, custom].each { |underlying| yield data, described_class.new(underlying) }
end
end

def raise_eof
raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::END_OF_FILE) }
end

it "returns an empty string from read, so read_all reports EOF" do
each_input do |data, buffered|
expect(buffered.read(4)).to eq(data)
expect(buffered.read(4)).to eq("")
end
each_input { |_, buffered| expect { buffered.read_all(4) }.to raise_eof }
end

it "raises EOF from read_byte after the last byte" do
each_input do |data, buffered|
expect(Array.new(data.bytesize) { buffered.read_byte }).to eq(data.bytes)
expect { buffered.read_byte }.to raise_eof
end
end

it "raises EOF from read_into_buffer when the input ends early" do
each_input { |_, buffered| expect { buffered.read_into_buffer(Thrift::Bytes.empty_byte_buffer(4), 4) }.to raise_eof }
end
end
end

describe Thrift::BufferedTransportFactory do
Expand Down
Loading