IO.splice(io_in, off_in, io_out, off_out, len) => integer IO.splice(io_in, off_in, io_out, off_out, len, flags) => integer source
Splice
len
bytes from/to a pipe. Either io_in
or io_out
MUST be a pipe. io_in
and io_out
may BOTH be pipes as of Linux 2.6.31 or later.
off_in
and off_out
if non-nil may be used to specify an offset for the non-pipe file descriptor.
flags
defaults to zero if unspecified. flags
may be a bitmask of the following flags:
IO::Splice::F_MOVE
IO::Splice::F_NONBLOCK
IO::Splice::F_MORE
Returns the number of bytes spliced. Raises EOFError when io_in
has reached end of file. Raises Errno::EAGAIN if the IO::Splice::F_NONBLOCK flag is set and the pipe has no data to read from or space to write to. May also raise Errno::EAGAIN if the non-pipe descriptor has no data to read from or space to write to.
As splice never exposes buffers to userspace, it will not take into account userspace buffering done by Ruby or stdio. It is also not subject to encoding/decoding filters under Ruby 1.9.
Consider using IO.trysplice
if io_out
is a pipe or if you are using non-blocking I/O on both descriptors as it avoids the cost of raising common Errno::EAGAIN exceptions.
See manpage for full documentation: kernel.org/doc/man-pages/online/pages/man2/splice.2.html
IO.tee(io_in, io_out, len) => integer IO.tee(io_in, io_out, len, flags) => integer source
Copies up to len
bytes of data from io_in
to io_out
. io_in
and io_out
must both refer to pipe descriptors. io_in
and io_out
may not be endpoints of the same pipe.
flags
may be zero (the default) or a combination of:
IO::Splice::F_NONBLOCK
Other IO::Splice
flags are currently unimplemented or have no effect.
Returns the number of bytes duplicated if successful. Raises EOFError when io_in
is closed and emptied. Raises Errno::EAGAIN when io_in
is empty and/or io_out
is full and flags
contains IO::Splice::F_NONBLOCK
Consider using IO.trytee
if you are using IO::Splice::F_NONBLOCK as it avoids the cost of raising common Errno::EAGAIN exceptions.
See manpage for full documentation: kernel.org/doc/man-pages/online/pages/man2/tee.2.html
IO.trysplice(io_in, off_in, io_out, off_out, len) => integer IO.trysplice(io_in, off_in, io_out, off_out, len, flags) => integer source
Exactly like IO.splice
, except :EAGAIN
is returned when either the read or write end would block instead of raising Errno::EAGAIN.
IO::Splice::F_NONBLOCK is always passed for the pipe descriptor, but this can still block if the non-pipe descriptor is blocking.
See IO.splice
documentation for more details.
This method is recommended whenever io_out
is a pipe.
IO.trytee(io_in, io_out, len) => integer IO.trytee(io_in, io_out, len, flags) => integer source
Exactly like IO.tee
, except :EAGAIN
is returned when either the read or write end would block instead of raising Errno::EAGAIN.
IO::Splice::F_NONBLOCK is always passed for the pipe descriptor, but this can still block if the non-pipe descriptor is blocking.
See IO.tee
documentation for more details.
IO.vmsplice(io, string_array) => integer IO.vmsplice(io, string_array, flags) => integer IO.vmsplice(io, string) => integer IO.vmsplice(io, string, flags) => integer source
Transfers an array of strings into the pipe descriptor given by io. io
must be the writable end of a pipe.
This may allow the kernel to avoid data copies in some cases. but is (probably) of limited usefulness in Ruby. If you have use cases or ideas for making this more useful for Ruby users, please tell us at ruby-io-splice@yhbt.net!
Also consider the "sendfile" RubyGem or IO.copy_stream in Ruby 1.9 if you want to do zero-copy file transfers to pipes or sockets. As of Linux 2.6.33, sendfile(2) can copy to any output descriptor, not just sockets.
See manpage for full documentation: kernel.org/doc/man-pages/online/pages/man2/vmsplice.2.html
reader, writer = IO.pipe reader.pipe_size => integer source
Returns the pipe capacity of the underlying pipe in bytes. The default capacity is 65536 bytes since Linux 2.6.11, and 4096 bytes in previous kernels.
Since the pipe is a circular buffer in the same kernel, the size of the reader is exactly the same as the size of the writer.
This method is only exposed on Linux 2.6.35 or later.
reader, writer = IO.pipe reader.pipe_size = integer source
Sets and returns the pipe capacity of the underlying pipe in bytes.
This MUST be a power-of-two, or Errno::EINVAL will be raised. Linux will silently increase this to be equal to the page size (4096 bytes on most architectures) if the specified value is less than the size of a page.
For users without CAP_SYS_RESOURCE, this raises Errno::EPERM when attempting to specify a value greater than the value in /proc/sys/fs/pipe-max-size.
Since the pipe is a circular buffer in the same kernel, the size of the reader is exactly the same as the size of the writer.
Raises Errno::EBUSY if the assigned value is less than the currently filled portion of the pipe.
This method is only exposed on Linux 2.6.35 or later.
mail archives: https://yhbt.net/ruby-io-splice/ public: ruby-io-splice@yhbt.net source code: git clone https://yhbt.net/ruby_io_splice.git