Protocol::HTTPSourceProtocolHTTPBodyDeflate

class Deflate

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

Definitions

def self.for(body, window_size = GZIP, level = DEFAULT_LEVEL)

Create a new body which compresses the given body using the GZIP algorithm by default.

Signature

parameter body Readable

the body to wrap.

parameter window_size Integer

the window size to use for compression.

parameter level Integer

the compression level to use.

returns Deflate

the wrapped body.

Implementation

def self.for(body, window_size = GZIP, level = DEFAULT_LEVEL)
	self.new(body, Zlib::Deflate.new(level, window_size))
end

def read

Read a chunk from the underlying body and compress it. If the body is finished, the stream is flushed and finished, and the remaining data is returned.

Signature

returns String | Nil

the compressed chunk or nil if the stream is closed.

Implementation

def read
	return if @stream.finished?
	
	# The stream might have been closed while waiting for the chunk to come in.
	if chunk = super
		@input_length += chunk.bytesize
		
		chunk = @stream.deflate(chunk, Zlib::SYNC_FLUSH)
		
		@output_length += chunk.bytesize
		
		return chunk
	elsif !@stream.closed?
		chunk = @stream.finish
		
		@output_length += chunk.bytesize
		
		return chunk.empty? ? nil : chunk
	end
end