mogilefs-client.git  about / heads / tags
MogileFS client library for Ruby
blob 9397e89ae6a801f34b7cf386eb202d44cec8cf7c 764 bytes (raw)
$ git show pipeline:lib/mogilefs/copy_stream.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
# -*- encoding: binary -*-

# internal compatibility class for older Rubies
module MogileFS::CopyStream # :nodoc:
  def self.copy_stream(src, dst)
    src_io = src.respond_to?(:to_str) ? File.open(src) : src
    dst_io = dst.respond_to?(:to_str) ? File.open(dst, "w") : dst
    buf = ""
    written = 0
    if src_io.respond_to?(:readpartial)
      begin
        src_io.readpartial(0x4000, buf)
        written += dst_io.write(buf)
      rescue EOFError
        break
      end while true
    else
      while src_io.read(0x4000, buf)
        written += dst_io.write(buf)
      end
    end
    dst_io.flush if dst_io.respond_to?(:flush)
    written
    ensure
      src_io.close if src.respond_to?(:to_str)
      dst_io.close if dst.respond_to?(:to_str)
  end
end

git clone https://yhbt.net/mogilefs-client.git