diff --git a/lib/multipart/post/composite_read_io.rb b/lib/multipart/post/composite_read_io.rb index 44704b7..6773e39 100644 --- a/lib/multipart/post/composite_read_io.rb +++ b/lib/multipart/post/composite_read_io.rb @@ -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 diff --git a/spec/multipart/post/composite_read_io_spec.rb b/spec/multipart/post/composite_read_io_spec.rb index 1bd2cc2..9b38eda 100644 --- a/spec/multipart/post/composite_read_io_spec.rb +++ b/spec/multipart/post/composite_read_io_spec.rb @@ -12,6 +12,11 @@ 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 + end + it "test_full_read_from_several_ios" do expect(subject.read).to be == 'the quick brown fox' end