Class: Async::HTTP::Body::ZStream

Inherits:
Wrapper show all
Defined in:
lib/async/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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Wrapper

#close, #empty?, #read

Methods inherited from Readable

#close, #each, #empty?, #join, #read

Constructor Details

#initialize(body, stream) ⇒ ZStream

Returns a new instance of ZStream.



49
50
51
52
53
54
55
56
# File 'lib/async/http/body/deflate.rb', line 49

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.



63
64
65
# File 'lib/async/http/body/deflate.rb', line 63

def input_length
  @input_length
end

#output_lengthObject (readonly)

Returns the value of attribute output_length.



64
65
66
# File 'lib/async/http/body/deflate.rb', line 64

def output_length
  @output_length
end

Class Method Details

.encoding_name(window_size) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/async/http/body/deflate.rb', line 39

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

#inspectObject



81
82
83
# File 'lib/async/http/body/deflate.rb', line 81

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

#lengthObject



58
59
60
61
# File 'lib/async/http/body/deflate.rb', line 58

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

#ratioObject



66
67
68
69
70
71
72
# File 'lib/async/http/body/deflate.rb', line 66

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

#stop(error) ⇒ Object



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

def stop(error)
	# There are two ways for the stream to be closed. Either #read returns nil or #stop is called.
	@stream.close unless @stream.closed?
	
	super
end