class AliyunSDK::OSS::HTTP::StreamWriter

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

Public Class Methods

new() { |self| ... } click to toggle source
# File lib/aliyun_sdk/oss/http.rb, line 45
def initialize
  @buffer = ""
  @producer = Fiber.new { yield self if block_given? }
  @producer.resume
end

Public Instance Methods

<<(chunk)
Alias for: write
closed?() click to toggle source
# File lib/aliyun_sdk/oss/http.rb, line 97
def closed?
  false
end
inspect() click to toggle source
# File lib/aliyun_sdk/oss/http.rb, line 101
def inspect
  "@buffer: " + @buffer[0, 32].inspect + "...#{@buffer.size} bytes"
end
read(bytes = nil, outbuf = nil) click to toggle source
# File lib/aliyun_sdk/oss/http.rb, line 51
def read(bytes = nil, outbuf = nil)
  ret = ""
  loop do
    if bytes
      fail if bytes < 0
      piece = @buffer.slice!(0, bytes)
      if piece
        ret << piece
        bytes -= piece.size
        break if bytes == 0
      end
    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) click to toggle source
# File lib/aliyun_sdk/oss/http.rb, line 89
def write(chunk)
  @buffer << chunk.to_s.force_encoding(Encoding::ASCII_8BIT)
  Fiber.yield
  self
end
Also aliased as: <<