Class: Krakow::ConnectionFeatures::Deflate::Io

Inherits:
Object
  • Object
show all
Defined in:
lib/krakow/connection_features/deflate.rb

Overview

Deflatable IO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, args = {}) ⇒ Io

Create new deflatable IO

Parameters:

  • io (IO)

    IO to wrap



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

Parameters:

  • args (Object)

Returns:

  • (Object)


28
29
30
# File 'lib/krakow/connection_features/deflate.rb', line 28

def method_missing(*args)
  io.__send__(*args)
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



11
12
13
# File 'lib/krakow/connection_features/deflate.rb', line 11

def buffer
  @buffer
end

#deflatorObject (readonly)

Returns the value of attribute deflator.



11
12
13
# File 'lib/krakow/connection_features/deflate.rb', line 11

def deflator
  @deflator
end

#headersObject (readonly)

Returns the value of attribute headers.



11
12
13
# File 'lib/krakow/connection_features/deflate.rb', line 11

def headers
  @headers
end

#inflatorObject (readonly)

Returns the value of attribute inflator.



11
12
13
# File 'lib/krakow/connection_features/deflate.rb', line 11

def inflator
  @inflator
end

#ioObject (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

Returns:

  • (TrueClass)


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_streamString

Read contents from stream

Returns:

  • (String)


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

Parameters:

  • n (Integer)

    nuber of bytes

Returns:

  • (String)


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

Parameters:

  • string (String)

Returns:

  • (Integer)

    number of bytes written



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