Class: Protocol::HTTP::Body::ZStream

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

Direct Known Subclasses

Deflate, Inflate

Constant Summary collapse

DEFAULT_LEVEL =
7
DEFLATE =
-Zlib::MAX_WBITS
GZIP =
Zlib::MAX_WBITS | 16
ENCODINGS =
{
	'deflate' => DEFLATE,
	'gzip' => GZIP,
}

Instance Attribute Summary collapse

Attributes inherited from Wrapper

#body

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Wrapper

#call, #empty?, #finish, #read, #ready?, #stream?, wrap

Methods inherited from Readable

#call, #each, #empty?, #finish, #join, #read, #ready?, #stream?

Constructor Details

#initialize(body, stream) ⇒ ZStream

Returns a new instance of ZStream.


51
52
53
54
55
56
57
58
# File 'lib/protocol/http/body/deflate.rb', line 51

def initialize(body, stream)
	super(body)
	
	@stream = stream
	
	@input_length = 0
	@output_length = 0
end

Instance Attribute Details

#input_lengthObject (readonly)

Returns the value of attribute input_length.


71
72
73
# File 'lib/protocol/http/body/deflate.rb', line 71

def input_length
  @input_length
end

#output_lengthObject (readonly)

Returns the value of attribute output_length.


72
73
74
# File 'lib/protocol/http/body/deflate.rb', line 72

def output_length
  @output_length
end

Class Method Details

.encoding_name(window_size) ⇒ Object


41
42
43
44
45
46
47
48
49
# File 'lib/protocol/http/body/deflate.rb', line 41

def self.encoding_name(window_size)
	if window_size <= -8
		return 'deflate'
	elsif window_size >= 16
		return 'gzip'
	else
		return 'compress'
	end
end

Instance Method Details

#close(error = nil) ⇒ Object


60
61
62
63
64
# File 'lib/protocol/http/body/deflate.rb', line 60

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

#inspectObject


82
83
84
# File 'lib/protocol/http/body/deflate.rb', line 82

def inspect
	"#{super} | \#<#{self.class} #{(ratio*100).round(2)}%>"
end

#lengthObject


66
67
68
69
# File 'lib/protocol/http/body/deflate.rb', line 66

def length
	# We don't know the length of the output until after it's been compressed.
	nil
end

#ratioObject


74
75
76
77
78
79
80
# File 'lib/protocol/http/body/deflate.rb', line 74

def ratio
	if @input_length != 0
		@output_length.to_f / @input_length.to_f
	else
		1.0
	end
end