Class: Chef::CookbookSiteStreamingUploader::MultipartStream
- Defined in:
- lib/chef/cookbook_site_streaming_uploader.rb
Instance Method Summary collapse
-
#initialize(parts) ⇒ MultipartStream
constructor
A new instance of MultipartStream.
- #read(how_much, dst_buf = nil) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(parts) ⇒ MultipartStream
Returns a new instance of MultipartStream.
212 213 214 215 216 |
# File 'lib/chef/cookbook_site_streaming_uploader.rb', line 212 def initialize(parts) @parts = parts @part_no = 0 @part_offset = 0 end |
Instance Method Details
#read(how_much, dst_buf = nil) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/chef/cookbook_site_streaming_uploader.rb', line 222 def read(how_much, dst_buf = nil) if @part_no >= @parts.size dst_buf.replace('') if dst_buf return dst_buf end how_much_current_part = @parts[@part_no].size - @part_offset how_much_current_part = if how_much_current_part > how_much how_much else how_much_current_part end how_much_next_part = how_much - how_much_current_part current_part = @parts[@part_no].read(@part_offset, how_much_current_part) # recurse into the next part if the current one was not large enough if how_much_next_part > 0 @part_no += 1 @part_offset = 0 next_part = read(how_much_next_part) result = current_part + if next_part next_part else '' end else @part_offset += how_much_current_part result = current_part end dst_buf ? dst_buf.replace(result || '') : result end |
#size ⇒ Object
218 219 220 |
# File 'lib/chef/cookbook_site_streaming_uploader.rb', line 218 def size @parts.inject(0) {|size, part| size + part.size} end |