copy_stream: enough to get this working under MRI 1.8
authorEric Wong <normalperson@yhbt.net>
Fri, 13 May 2011 02:13:57 +0000 (12 19:13 -0700)
committerEric Wong <normalperson@yhbt.net>
Fri, 13 May 2011 02:13:57 +0000 (12 19:13 -0700)
This way at least 1.8 users can have something akin to
IO.copy_stream.

lib/io/splice.rb
lib/io/splice/mri_18.rb [new file with mode: 0644]

index 54a3f04..574e681 100644 (file)
@@ -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 (file)
index 0000000..ad20152
--- /dev/null
@@ -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