Class: Baidubce::Http::BaseHttpClient::BufWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/baidubce/http/base_http_client.rb

Instance Method Summary collapse

Constructor Details

#initializeBufWriter

Returns a new instance of BufWriter.



143
144
145
146
147
# File 'lib/baidubce/http/base_http_client.rb', line 143

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

Instance Method Details

#closeObject



193
194
# File 'lib/baidubce/http/base_http_client.rb', line 193

def close
end

#closed?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/baidubce/http/base_http_client.rb', line 189

def closed?
    false
end

#inspectObject



196
197
198
# File 'lib/baidubce/http/base_http_client.rb', line 196

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

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/baidubce/http/base_http_client.rb', line 149

def read(bytes = nil, outbuf = nil)
    ret = ""
    while true
        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
        outbuf.clear
        outbuf << ret
    end

    return nil if ret.empty? && !bytes.nil? && bytes > 0
    ret
end

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



181
182
183
184
185
# File 'lib/baidubce/http/base_http_client.rb', line 181

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