Sha256: 38944fc8513d677d0e70c1aee33d4e9ee3d72b196ffb88e298162b8a16530bf8

Contents?: true

Size: 799 Bytes

Versions: 4

Compression:

Stored size: 799 Bytes

Contents

# -*- encoding: binary -*-
# Copyright (C) 2009-2014, Eric Wong <normalperson@yhbt.net> et. al.
# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
require 'io/extra' # gem install io-extra

module Yahns::SendfileCompat
  def trysendfile(io, offset, count)
    return 0 if count == 0
    count = 0x4000 if count > 0x4000
    str = IO.pread(io.fileno, count, offset)
    if count > str.bytesize
      raise EOFError, "end of file reached"
    end
    n = 0
    case rv = kgio_trywrite(str)
    when String # partial write, keep trying
      n += (str.bytesize - rv.bytesize)
      str = rv
    when :wait_writable, :wait_readable
      return n > 0 ? n : rv
    when nil
      return n + str.bytesize # yay!
    end while true
  end
end

class IO
  include Yahns::SendfileCompat
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
yahns-1.4.0 lib/yahns/sendfile_compat.rb
yahns-1.3.1 lib/yahns/sendfile_compat.rb
yahns-1.3.0 lib/yahns/sendfile_compat.rb
yahns-1.2.0 lib/yahns/sendfile_compat.rb