Class: Kafka::GzipCodec

Inherits:
Object
  • Object
show all
Defined in:
lib/kafka/gzip_codec.rb

Instance Method Summary collapse

Constructor Details

#initializeGzipCodec


3
4
5
# File 'lib/kafka/gzip_codec.rb', line 3

def initialize
  require "zlib"
end

Instance Method Details

#codec_idObject


7
8
9
# File 'lib/kafka/gzip_codec.rb', line 7

def codec_id
  1
end

#compress(data) ⇒ Object


11
12
13
14
15
16
17
18
19
20
# File 'lib/kafka/gzip_codec.rb', line 11

def compress(data)
  buffer = StringIO.new
  buffer.set_encoding(Encoding::BINARY)

  writer = Zlib::GzipWriter.new(buffer, Zlib::DEFAULT_COMPRESSION, Zlib::DEFAULT_STRATEGY)
  writer.write(data)
  writer.close

  buffer.string
end

#decompress(data) ⇒ Object


22
23
24
25
26
# File 'lib/kafka/gzip_codec.rb', line 22

def decompress(data)
  buffer = StringIO.new(data)
  reader = Zlib::GzipReader.new(buffer)
  reader.read
end