class Async::HTTP::Body::Buffered

A body which buffers all it’s contents.

def self.for(body)

def self.for(body)
	chunks = []
	
	body.each do |chunk|
		chunks << chunk
	end
	
	self.new(chunks)
end

def self.wrap(body)

Wraps an array into a buffered body.
def self.wrap(body)
	if body.is_a? Async::HTTP::Body::Readable
		return body
	elsif body.is_a? Array
		return self.new(body)
	else
		return self.for(body)
	end
end

def bytesize

def bytesize
	@bytesize ||= @chunks.inject(0) {|sum, chunk| sum + chunk.bytesize}
end

def close

def close
	self
end

def each

def each
	return to_enum unless block_given?
	
	while @index < @chunks.count
		yield @chunks[@index]
		@index += 1
	end
end

def empty?

def empty?
	@chunks.empty?
end

def initialize(chunks)

def initialize(chunks)
	@chunks = chunks
	@bytesize = nil
	
	@index = 0
end

def inspect

def inspect
	"\#<#{self.class} #{@chunks.count} chunks, #{self.bytesize} bytes>"
end

def read

def read
	if chunk = @chunks[@index]
		@index += 1
	end
	
	return chunk
end

def rewind

def rewind
	@index = 0
end