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

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

Overview

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

Direct Known Subclasses

Deflate, Inflate

Constant Summary collapse

DEFAULT_LEVEL =

The default compression level.

7
DEFLATE =

The DEFLATE window size.

-Zlib::MAX_WBITS
GZIP =

The GZIP window size.

Zlib::MAX_WBITS | 16
ENCODINGS =

The supported encodings.

{
	"deflate" => DEFLATE,
	"gzip" => GZIP,
}

Instance Attribute Summary collapse

Attributes inherited from Wrapper

#body

Instance Method Summary collapse

Methods inherited from Wrapper

#The wrapped body.=, #as_json, #buffered, #discard, #empty?, #read, #ready?, #rewind, #rewindable?, #to_json, wrap

Methods inherited from Readable

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

Constructor Details

#initialize(body, stream) ⇒ ZStream

Initialize the body with the given stream.



34
35
36
37
38
39
40
41
# File 'lib/protocol/http/body/deflate.rb', line 34

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.



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

def input_length
  @input_length
end

#input_length the total number of bytes read from the input.(thetotalnumberofbytesreadfromtheinput.) ⇒ Object (readonly)



62
# File 'lib/protocol/http/body/deflate.rb', line 62

attr :input_length

#output_lengthObject (readonly)

Returns the value of attribute output_length.



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

def output_length
  @output_length
end

#output_length the total number of bytes written to the output.(thetotalnumberofbyteswrittentotheoutput.) ⇒ Object (readonly)



65
# File 'lib/protocol/http/body/deflate.rb', line 65

attr :output_length

Instance Method Details

#close(error = nil) ⇒ Object

Close the stream.



46
47
48
49
50
51
52
53
# File 'lib/protocol/http/body/deflate.rb', line 46

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

#inspectObject

Inspect the body, including the compression ratio.



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

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

#lengthObject

The length of the output, if known. Generally, this is not known due to the nature of compression.



56
57
58
59
# File 'lib/protocol/http/body/deflate.rb', line 56

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

#ratioObject

The compression ratio, according to the input and output lengths.



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

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