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
18 changes: 18 additions & 0 deletions lib/multipart/post/composite_read_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,26 @@ def initialize(*ios)
@index = 0
end

# Close all the underyling IOs.
def close
@ios.each do |io|
io.close if io.respond_to?(:close)
end

@ios = nil
@index = 0
end

def closed?
@ios.nil?
end

# Read from IOs in order until `length` bytes have been received.
def read(length = nil, outbuf = nil)
if @ios.nil?
raise IOError, "CompositeReadIO is closed!"
end

got_result = false
outbuf = outbuf ? outbuf.replace("") : String.new

Expand Down
9 changes: 9 additions & 0 deletions spec/multipart/post/composite_read_io_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
MULTIBYTE = File.dirname(__FILE__) + '/../../fixtures/multibyte.txt'

RSpec.shared_context "composite io" do
it "can close all the ios" do
subject.close
expect(subject.closed?).to be_truthy

expect do
subject.read
end.to raise_error(IOError)
end

it "test_full_read_from_several_ios" do
expect(subject.read).to be == 'the quick brown fox'
end
Expand Down