lib/multipart/post/composite_read_io.rb in multipart-post-2.3.0 vs lib/multipart/post/composite_read_io.rb in multipart-post-2.4.0
- old
+ new
@@ -11,11 +11,11 @@
# Copyright, 2013, by Mislav Marohnić.
# Copyright, 2013, by Leo Cassarani.
# Copyright, 2019, by Olle Jonsson.
# Copyright, 2019, by Patrick Davey.
# Copyright, 2021, by Lewis Cowles.
-# Copyright, 2021-2022, by Samuel Williams.
+# Copyright, 2021-2024, by Samuel Williams.
module Multipart
module Post
# Concatenate together multiple IO objects into a single, composite IO object
# for purposes of reading as a single stream.
@@ -31,11 +31,29 @@
def initialize(*ios)
@ios = ios.flatten
@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
while io = current_io
if result = io.read(length)