class Async::HTTP::Body::Chunked

def close(error = nil)

def close(error = nil)
	# We only close the connection if we haven't completed reading the entire body:
	unless @finished
		@protocol.close
		@finished = true
	end
	
	super
end

def empty?

def empty?
	@finished
end

def initialize(protocol)

def initialize(protocol)
	@protocol = protocol
	@finished = false
	
	@length = 0
	@count = 0
end

def inspect

def inspect
	"\#<#{self.class} #{@length} bytes read in #{@count} chunks>"
end

def read

def read
	return nil if @finished
	
	length = @protocol.read_line.to_i(16)
	
	if length == 0
		@finished = true
		@protocol.read_line
		
		return nil
	end
	
	chunk = @protocol.stream.read(length)
	@protocol.read_line # Consume the trailing CRLF
	
	@length += length
	@count += 1
	
	return chunk
end