1 # -*- encoding: binary -*-
2 require 'io_splice_ext'
8 # the version of IO::Splice, currently 2.0.0
11 # The maximum default capacity of the pipe in bytes.
12 # Under stock Linux, this is 65536 bytes as of 2.6.11, and 4096 before
13 # We detect this at runtime as it is easy to recompile the kernel
14 # and set a new value.
15 # Starting with Linux 2.6.35, pipe capacity will be tunable
16 # and this will only represent the default capacity of a
23 n += wr.write_nonblock(buf)
32 # copies the contents of the IO object given by +src+ to +dst+
33 # If len is specified, then only len bytes are copied. Otherwise
34 # the copy will be until EOF is reached on the +src+.
35 # +src+ and +dst+ must be IO objects or respond to +to_io+
36 def self.copy_stream(src, dst, len = nil, src_offset = nil)
37 src.kind_of?(String) and src = File.open(src, 'rb')
38 dst.kind_of?(String) and dst = File.open(dst, 'wb')
39 src, dst = src.to_io, dst.to_io
41 src.sysseek(src_offset) if src_offset
42 if src.stat.pipe? || dst.stat.pipe?
45 nr = len > PIPE_CAPA ? PIPE_CAPA : len
46 nr = IO.splice(src, nil, dst, nil, nr, F_MOVE)
48 raise EOFError, "unexpected EOF with #{len} bytes left"
55 nr = IO.splice(src, nil, dst, nil, PIPE_CAPA, F_MOVE)
66 nr = len > PIPE_CAPA ? PIPE_CAPA : len
67 nr_src = copy_stream(src, w, nr)
69 raise RuntimeError, "short splice from: #{nr_src} != #{nr}"
70 nr_dst = copy_stream(r, dst, nr)
72 raise RuntimeError, "short splice to: #{nr_dst} != #{nr}"
78 nr = IO.splice(src, nil, w, nil, PIPE_CAPA, F_MOVE)
81 nr -= IO.splice(r, nil, dst, nil, nr, F_MOVE)