Class: AliyunSDK::OSS::HTTP::StreamWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/aliyun_sdk/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.



46
47
48
49
50
# File 'lib/aliyun_sdk/oss/http.rb', line 46

def initialize
  @buffer = ""
  @producer = Fiber.new { yield self if block_given? }
  @producer.resume
end

Instance Method Details

#closed?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/aliyun_sdk/oss/http.rb', line 98

def closed?
  false
end

#inspectObject



102
103
104
# File 'lib/aliyun_sdk/oss/http.rb', line 102

def inspect
  "@buffer: " + @buffer[0, 32].inspect + "...#{@buffer.size} bytes"
end

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



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
82
83
84
85
86
87
88
# File 'lib/aliyun_sdk/oss/http.rb', line 52

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) ⇒ Object Also known as: <<



90
91
92
93
94
# File 'lib/aliyun_sdk/oss/http.rb', line 90

def write(chunk)
  @buffer << chunk.to_s.force_encoding(Encoding::ASCII_8BIT)
  Fiber.yield
  self
end