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

Returns a new instance of StreamWriter.



41
42
43
44
45
# File 'lib/aliyun/oss/http.rb', line 41

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

Instance Method Details

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?
  false
end

#inspectObject



82
83
84
# File 'lib/aliyun/oss/http.rb', line 82

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/aliyun/oss/http.rb', line 48

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

  ret.empty? ? nil : ret
end

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



70
71
72
73
74
# File 'lib/aliyun/oss/http.rb', line 70

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