class Krakow::ConnectionFeatures::Deflate::Io

Deflatable IO

Attributes

buffer[R]
deflator[R]
headers[R]
inflator[R]
io[R]

Public Class Methods

new(io, args={}) click to toggle source

Create new deflatable IO

@param io [IO] IO to wrap @return [Io]

# 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

Public Instance Methods

close(*args) click to toggle source

Close the IO

@return [TrueClass]

Calls superclass method
# File lib/krakow/connection_features/deflate.rb, line 72
def close(*args)
  super
  deflator.deflate(nil, Zlib::FINISH)
  deflator.close
  true
end
method_missing(*args) click to toggle source

Proxy to underlying socket

@param args [Object] @return [Object]

# File lib/krakow/connection_features/deflate.rb, line 28
def method_missing(*args)
  io.__send__(*args)
end
read(n)
Alias for: recv
read_stream() click to toggle source

Read contents from stream

@return [String]

# 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) click to toggle source

Receive bytes from the IO

@param n [Integer] nuber of bytes @return [String]

# 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
Also aliased as: read
write(string) click to toggle source

Write string to IO

@param string [String] @return [Integer] number of bytes written

# 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