Class: Aliyun::OSS::HTTP::StreamWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/aliyun/oss/http.rb

Overview

A stream implementation A stream is any class that responds to :read(bytes, outbuf)

Instance Method Summary collapse

Constructor Details

#initializeStreamWriter



43
44
45
46
47
# File 'lib/aliyun/oss/http.rb', line 43

def initialize
  @chunks = []
  @producer = Fiber.new { yield self if block_given? }
  @producer.resume
end

Instance Method Details

#closed?Boolean



86
87
88
# File 'lib/aliyun/oss/http.rb', line 86

def closed?
  false
end

#inspectObject



90
91
92
# File 'lib/aliyun/oss/http.rb', line 90

def inspect
  "@chunks: " + @chunks.map { |c| c[0, 100] }.join(';')
end

#read(bytes = nil, outbuf = nil) ⇒ Object

FIXME: it may return more than bytes, not sure if that’s a problem



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aliyun/oss/http.rb', line 50

def read(bytes = nil, outbuf = nil)
  ret = ""
  loop do
    c = @chunks.shift
    ret << c if c && !c.empty?
    break if bytes && ret.size >= bytes
    if @producer.alive?
      @producer.resume
    else
      break
    end
  end

  if outbuf
    # WARNING: Using outbuf = '' here DOES NOT work!
    outbuf.clear
    outbuf << ret
  end

  # Conform to IO#read(length[, outbuf]):
  # At end of file, it returns nil or "" depend on
  # length. ios.read() and ios.read(nil) returns
  # "". ios.read(positive-integer) returns nil.
  return nil if ret.empty? && !bytes.nil? && bytes > 0

  ret
end

#write(chunk) ⇒ Object Also known as: <<



78
79
80
81
82
# File 'lib/aliyun/oss/http.rb', line 78

def write(chunk)
  @chunks << chunk
  Fiber.yield
  self
end