From c5677a072d0fb4866397642eb4c946ca141d0990 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 7 Feb 2024 23:06:21 +1300 Subject: [PATCH] Add support for `CompositeReadIO#close`. --- lib/multipart/post/composite_read_io.rb | 18 ++++++++++++++++++ spec/multipart/post/composite_read_io_spec.rb | 5 +++++ 2 files changed, 23 insertions(+) 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