2 # -*- encoding: binary -*-
4 # Example of using IO.splice to copy a file
5 # This can be significantly faster than IO.copy_stream as data
6 # is never copied into userspace.
10 usage = "#$0 SOURCE DEST"
11 source = ARGV.shift or abort usage
12 dest = ARGV.shift or abort usage
14 source = File.open(source, 'rb')
15 dest = File.open(dest, 'wb')
17 # We use a pipe as a ring buffer in kernel space.
18 # pipes may store up to IO::Splice::PIPE_CAPA bytes
19 rio, wio = pipe = IO.pipe
23 # first pull as many bytes as possible into the pipe
24 IO.splice(source, nil, wio, nil, IO::Splice::PIPE_CAPA, 0)
29 # now move the contents of the pipe buffer into the destination file
30 # the copied data never enters userspace
31 nwritten = IO.splice(rio, nil, dest, nil, nread, 0)
34 abort "short splice to destination file: #{nwritten} != #{nread}"