class Protocol::HTTP::Body::ZStream

A body which compresses or decompresses the contents using the DEFLATE or GZIP algorithm.

def close(error = nil)

@parameter error [Exception | Nil] the error that caused the stream to be closed.

Close the stream.
def close(error = nil)
	if stream = @stream
		@stream = nil
		stream.close unless stream.closed?
	end
	
	super
end

def initialize(body, stream)

@parameter stream [Zlib::Deflate | Zlib::Inflate] the stream to use for compression or decompression.
@parameter body [Readable] the body to wrap.

Initialize the body with the given stream.
def initialize(body, stream)
	super(body)
	
	@stream = stream
	
	@input_length = 0
	@output_length = 0
end

def inspect

@returns [String] a string representation of the body.

Inspect the body, including the compression ratio.
def inspect
	"#{super} | \#<#{self.class} #{(ratio*100).round(2)}%>"
end

def length

The length of the output, if known. Generally, this is not known due to the nature of compression.
def length
	# We don't know the length of the output until after it's been compressed.
	nil
end

def ratio

@returns [Float] the compression ratio, e.g. 0.5 for 50% compression.

The compression ratio, according to the input and output lengths.
def ratio
	if @input_length != 0
		@output_length.to_f / @input_length.to_f
	else
		1.0
	end
end