Class: Protocol::HTTP::Body::Deflate

Inherits:
ZStream show all
Defined in:
lib/protocol/http/body/deflate.rb

Overview

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

Constant Summary

Constants inherited from ZStream

ZStream::DEFAULT_LEVEL, ZStream::DEFLATE, ZStream::ENCODINGS, ZStream::GZIP

Instance Attribute Summary

Attributes inherited from ZStream

#input_length, #input_length the total number of bytes read from the input., #output_length, #output_length the total number of bytes written to the output.

Attributes inherited from Wrapper

#body

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ZStream

#close, #initialize, #inspect, #length, #ratio

Methods inherited from Wrapper

#The wrapped body.=, #as_json, #buffered, #close, #discard, #empty?, #initialize, #inspect, #length, #ready?, #rewind, #rewindable?, #to_json, wrap

Methods inherited from Readable

#as_json, #buffered, #call, #close, #discard, #each, #empty?, #finish, #join, #length, #ready?, #rewind, #rewindable?, #stream?, #to_json

Constructor Details

This class inherits a constructor from Protocol::HTTP::Body::ZStream

Class Method Details

.for(body, window_size = GZIP, level = DEFAULT_LEVEL) ⇒ Object

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



94
95
96
# File 'lib/protocol/http/body/deflate.rb', line 94

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

Instance Method Details

#readObject

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.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/protocol/http/body/deflate.rb', line 101

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