From a301f99563deeb03c93d275330e20ff672519680 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 6 Jun 2010 17:59:10 +0000 Subject: [PATCH] copy_stream: cleanup after ourselves It's a good idea to clean up any file descriptors opened within our method to avoid unnecessary GC invocations. --- lib/io/splice.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/io/splice.rb b/lib/io/splice.rb index 2c5f5ac..3cac251 100644 --- a/lib/io/splice.rb +++ b/lib/io/splice.rb @@ -34,8 +34,9 @@ class IO # the copy will be until EOF is reached on the +src+. # +src+ and +dst+ must be IO objects or respond to +to_io+ def self.copy_stream(src, dst, len = nil, src_offset = nil) - src.kind_of?(String) and src = File.open(src, 'rb') - dst.kind_of?(String) and dst = File.open(dst, 'wb') + close = [] + src.kind_of?(String) and close << (src = File.open(src, 'rb')) + dst.kind_of?(String) and close << (dst = File.open(dst, 'wb')) src, dst = src.to_io, dst.to_io rv = len src.sysseek(src_offset) if src_offset @@ -60,7 +61,8 @@ class IO end else begin - r, w = IO.pipe + r, w = tmp = IO.pipe + close.concat(tmp) if len while len > 0 nr = len > PIPE_CAPA ? PIPE_CAPA : len @@ -84,13 +86,12 @@ class IO break end while true end - ensure - r.close - w.close end end rv + ensure + close.each { |io| io.close } end end -- 2.11.4.GIT