class IO

Public Class Methods

copy_stream(src, dst, copy_length = nil, offset = nil) click to toggle source
# File lib/segment_upload.rb, line 14
def IO.copy_stream(src, dst, copy_length = nil, offset = nil)
  if src.stat.pipe?
    amount = copy_length.to_i
    buf_size = [amount, 2**16].min
    buffer = ''
    while amount > 0
      src.read(buf_size, buffer)
      amount_read = buffer.length
      dst.write(buffer)
      amount -= amount_read
      break if src.eof?
    end
    copy_length.to_i - amount
  else
    current_pos = src.pos
    count = dst.sendfile(src, offset || current_pos, copy_length)
    src.seek(count, IO::SEEK_CUR)
    count
  end
rescue EOFError
  0
end