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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(crc_enable = false, init_crc = 0) ⇒ StreamWriter

Returns a new instance of StreamWriter.



48
49
50
51
52
53
54
# File 'lib/aliyun/oss/http.rb', line 48

def initialize(crc_enable = false, init_crc = 0)
  @buffer = ""
  @producer = Fiber.new { yield self if block_given? }
  @producer.resume
  @data_crc = init_crc.to_i
  @crc_enable = crc_enable
end

Instance Attribute Details

#data_crcObject (readonly)

Returns the value of attribute data_crc.



46
47
48
# File 'lib/aliyun/oss/http.rb', line 46

def data_crc
  @data_crc
end

Instance Method Details

#closeObject



108
109
# File 'lib/aliyun/oss/http.rb', line 108

def close
end

#closed?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/aliyun/oss/http.rb', line 104

def closed?
  false
end

#inspectObject



111
112
113
# File 'lib/aliyun/oss/http.rb', line 111

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

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



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
89
90
91
92
93
94
# File 'lib/aliyun/oss/http.rb', line 56

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

  @data_crc = Aliyun::OSS::Util.crc(ret, @data_crc) if @crc_enable

  ret
end

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



96
97
98
99
100
# File 'lib/aliyun/oss/http.rb', line 96

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