1 # -*- encoding: binary -*-
2 require 'io_splice_ext'
8 # the version of IO::Splice, currently 0.1.0
11 # The maximum 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.
20 n += wr.write_nonblock(buf)
29 # copies the contents of the IO object given by +src+ to +dst+
30 # If len is specified, then only len bytes are copied. Otherwise
31 # the copy will be until EOF is reached on the +src+.
32 # +src+ and +dst+ must be IO objects or respond to +to_io+
33 def self.copy_stream(src, dst, len = nil, src_offset = nil)
34 src.kind_of?(String) and src = File.open(src, 'rb')
35 dst.kind_of?(String) and dst = File.open(dst, 'wb')
36 src, dst = src.to_io, dst.to_io
38 src.sysseek(src_offset) if src_offset
39 if src.stat.pipe? || dst.stat.pipe?
42 nr = len > PIPE_CAPA ? PIPE_CAPA : len
43 nr = IO.splice(src, nil, dst, nil, nr, F_MOVE)
45 raise EOFError, "unexpected EOF with #{len} bytes left"
52 nr = IO.splice(src, nil, dst, nil, PIPE_CAPA, F_MOVE)
63 nr = len > PIPE_CAPA ? PIPE_CAPA : len
64 nr_src = copy_stream(src, w, nr)
66 raise RuntimeError, "short splice from: #{nr_src} != #{nr}"
67 nr_dst = copy_stream(r, dst, nr)
69 raise RuntimeError, "short splice to: #{nr_dst} != #{nr}"
75 nr = IO.splice(src, nil, w, nil, PIPE_CAPA, F_MOVE)
78 nr -= IO.splice(r, nil, dst, nil, nr, F_MOVE)