From 1ec67bc61d534232b5eec5c3f32da362898c6508 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 12 May 2011 19:13:57 -0700 Subject: [PATCH] copy_stream: enough to get this working under MRI 1.8 This way at least 1.8 users can have something akin to IO.copy_stream. --- lib/io/splice.rb | 4 ++++ lib/io/splice/mri_18.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 lib/io/splice/mri_18.rb diff --git a/lib/io/splice.rb b/lib/io/splice.rb index 54a3f04..574e681 100644 --- a/lib/io/splice.rb +++ b/lib/io/splice.rb @@ -101,3 +101,7 @@ module IO::Splice rv end end +if (! defined?(RUBY_ENGINE) || RUBY_ENGINE == "ruby") && + RUBY_VERSION.to_f <= 1.8 + require "io/splice/mri_18" +end diff --git a/lib/io/splice/mri_18.rb b/lib/io/splice/mri_18.rb new file mode 100644 index 0000000..ad20152 --- /dev/null +++ b/lib/io/splice/mri_18.rb @@ -0,0 +1,34 @@ +# :enddoc: +require "io/nonblock" +module IO::Splice + class << self + remove_method :full + remove_method :partial + end + def self.full(src, dst, len, src_offset) + dst.to_io.nonblock = src.to_io.nonblock = true + spliced = 0 + case n = IO.trysplice(src, src_offset, dst, nil, len, IO::Splice::F_MOVE) + when :EAGAIN + src.to_io.wait + IO.select(nil, [dst]) + when Integer + spliced += n + len -= n + src_offset += n if src_offset + when nil + break + end while len > 0 + spliced + end + + def self.partial(src, dst, len, src_offset) + dst.to_io.nonblock = src.to_io.nonblock = true + begin + src.to_io.wait + IO.select(nil, [dst]) + rv = IO.trysplice(src, src_offset, dst, nil, len, IO::Splice::F_MOVE) + end while rv == :EAGAIN + rv + end +end -- 2.11.4.GIT