Class: Aliyun::OSS::HTTP::StreamWriter
- Inherits:
-
Object
- Object
- Aliyun::OSS::HTTP::StreamWriter
- 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
- #closed? ⇒ Boolean
-
#initialize ⇒ StreamWriter
constructor
A new instance of StreamWriter.
- #inspect ⇒ Object
- #read(bytes = nil, outbuf = nil) ⇒ Object
- #write(chunk) ⇒ Object (also: #<<)
Constructor Details
#initialize ⇒ StreamWriter
Returns a new instance of StreamWriter.
44 45 46 47 48 |
# File 'lib/aliyun/oss/http.rb', line 44 def initialize @buffer = "" @producer = Fiber.new { yield self if block_given? } @producer.resume end |
Instance Method Details
#closed? ⇒ Boolean
91 92 93 |
# File 'lib/aliyun/oss/http.rb', line 91 def closed? false end |
#inspect ⇒ Object
95 96 97 |
# File 'lib/aliyun/oss/http.rb', line 95 def inspect "@buffer: " + @buffer[0, 32].inspect + "...#{@buffer.size} bytes" end |
#read(bytes = nil, outbuf = nil) ⇒ Object
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 77 78 79 80 81 |
# File 'lib/aliyun/oss/http.rb', line 50 def read(bytes = nil, outbuf = nil) ret = "" loop do if bytes ret << @buffer.slice!(0, bytes) break if ret.size >= bytes else ret << @buffer @buffer.clear end 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: <<
83 84 85 86 87 |
# File 'lib/aliyun/oss/http.rb', line 83 def write(chunk) @buffer << chunk.to_s.force_encoding(Encoding::ASCII_8BIT) Fiber.yield self end |