lib/io/splice.rb in io_splice-4.0.0 vs lib/io/splice.rb in io_splice-4.1.0
- old
+ new
@@ -23,10 +23,15 @@
wr.close
rd.close
n
end
+ def self.need_open?(obj) # :nodoc:
+ return false if obj.respond_to?(:to_io)
+ obj.respond_to?(:to_path) || obj.kind_of?(String)
+ end
+
# copies the contents of the IO object given by +src+ to +dst+
# If +len+ is specified, then only +len+ bytes are copied and
# +EOFError+ is raised if fewer than +len+ bytes could be copied.
# Otherwise the copy will be until EOF is reached on the +src+.
# +src+ and +dst+ must be IO objects or respond to +to_io+
@@ -34,13 +39,13 @@
# This is nearly a drop-in replacement for IO.copy_stream (in Ruby 1.9)
# but does not take into account userspace I/O buffers nor IO-like
# objects with no underlying file descriptor (e.g. StringIO).
def self.copy_stream(src, dst, len = nil, src_offset = nil)
close = []
- src.kind_of?(String) and close << (src = File.open(src))
- dst.kind_of?(String) and close << (dst = File.open(dst, "w"))
- src, dst = src.to_io, dst.to_io
+ need_open?(src) and close << (src = File.open(src))
+ need_open?(dst) and close << (dst = File.open(dst, "w"))
rv = len
+ src, dst = src.to_io, dst.to_io
if src.stat.pipe? || dst.stat.pipe?
if len
len -= full(src, dst, len, src_offset) until len == 0
else