Class: Krakow::ConnectionFeatures::Deflate::Io
- Inherits:
-
Object
- Object
- Krakow::ConnectionFeatures::Deflate::Io
- Defined in:
- lib/krakow/connection_features/deflate.rb
Overview
Deflatable IO
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#deflator ⇒ Object
readonly
Returns the value of attribute deflator.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#inflator ⇒ Object
readonly
Returns the value of attribute inflator.
-
#io ⇒ Object
readonly
Returns the value of attribute io.
Instance Method Summary collapse
-
#close(*args) ⇒ TrueClass
Close the IO.
-
#initialize(io, args = {}) ⇒ Io
constructor
Create new deflatable IO.
-
#method_missing(*args) ⇒ Object
Proxy to underlying socket.
-
#read_stream ⇒ String
Read contents from stream.
-
#recv(n) ⇒ String
(also: #read)
Receive bytes from the IO.
-
#write(string) ⇒ Integer
Write string to IO.
Constructor Details
#initialize(io, args = {}) ⇒ Io
Create new deflatable IO
17 18 19 20 21 22 |
# File 'lib/krakow/connection_features/deflate.rb', line 17 def initialize(io, args={}) @io = io @buffer = '' @inflator = Zlib::Inflate.new(-Zlib::MAX_WBITS) @deflator = Zlib::Deflate.new(nil, -Zlib::MAX_WBITS) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
Proxy to underlying socket
28 29 30 |
# File 'lib/krakow/connection_features/deflate.rb', line 28 def method_missing(*args) io.__send__(*args) end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
11 12 13 |
# File 'lib/krakow/connection_features/deflate.rb', line 11 def buffer @buffer end |
#deflator ⇒ Object (readonly)
Returns the value of attribute deflator.
11 12 13 |
# File 'lib/krakow/connection_features/deflate.rb', line 11 def deflator @deflator end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
11 12 13 |
# File 'lib/krakow/connection_features/deflate.rb', line 11 def headers @headers end |
#inflator ⇒ Object (readonly)
Returns the value of attribute inflator.
11 12 13 |
# File 'lib/krakow/connection_features/deflate.rb', line 11 def inflator @inflator end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
11 12 13 |
# File 'lib/krakow/connection_features/deflate.rb', line 11 def io @io end |
Instance Method Details
#close(*args) ⇒ TrueClass
Close the IO
72 73 74 75 76 77 |
# File 'lib/krakow/connection_features/deflate.rb', line 72 def close(*args) super deflator.deflate(nil, Zlib::FINISH) deflator.close true end |
#read_stream ⇒ String
Read contents from stream
48 49 50 51 52 53 |
# File 'lib/krakow/connection_features/deflate.rb', line 48 def read_stream str = io.read unless(str.empty?) buffer << inflator.inflate(str) end end |
#recv(n) ⇒ String Also known as: read
Receive bytes from the IO
36 37 38 39 40 41 42 |
# File 'lib/krakow/connection_features/deflate.rb', line 36 def recv(n) until(buffer.length >= n) read_stream sleep(0.1) unless buffer.length >= n end buffer.slice!(0, n) end |
#write(string) ⇒ Integer
Write string to IO
59 60 61 62 63 64 65 66 67 |
# File 'lib/krakow/connection_features/deflate.rb', line 59 def write(string) unless(string.empty?) output = deflator.deflate(string) output << deflator.flush io.write(output) else 0 end end |