Protocol::HTTPSourceProtocolHTTPBodyWritable

class Writable

A dynamic body which you can write to and read from.

Nested

Definitions

def initialize(length = nil, queue: Thread::Queue.new)

Implementation

def initialize(length = nil, queue: Thread::Queue.new)
	@length = length
	@queue = queue
	@count = 0
	@error = nil
end

def close(error = nil)

Stop generating output; cause the next call to write to fail with the given error. Does not prevent existing chunks from being read. In other words, this indicates both that no more data will be or should be written to the body.

Implementation

def close(error = nil)
	@error ||= error
	
	@queue.clear
	@queue.close
	
	super
end

def empty?

Has the producer called #finish and has the reader consumed the nil token?

Implementation

def empty?
	@queue.empty? && @queue.closed?
end

def read

Read the next available chunk.

Implementation

def read
	if @error
		raise @error
	end
	
	# This operation may result in @error being set.
	chunk = @queue.pop
	
	if @error
		raise @error
	end
	
	return chunk
end

def write(chunk)

Write a single chunk to the body. Signal completion by calling #finish.

Implementation

def write(chunk)
	if @queue.closed?
		raise(@error || Closed)
	end
	
	@queue.push(chunk)
	@count += 1
end

def output

Create an output wrapper which can be used to write chunks to the body.

Implementation

def output
	output = Output.new(self)
	
	unless block_given?
		return output
	end
	
	begin
		yield output
	rescue => error
		raise error
	ensure
		output.close(error)
	end
end