Class: Avro::DataFile::DeflateCodec

Inherits:
Object
  • Object
show all
Defined in:
lib/avro/data_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level = Zlib::DEFAULT_COMPRESSION) ⇒ DeflateCodec

Returns a new instance of DeflateCodec.



310
311
312
# File 'lib/avro/data_file.rb', line 310

def initialize(level=Zlib::DEFAULT_COMPRESSION)
  @level = level
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



308
309
310
# File 'lib/avro/data_file.rb', line 308

def level
  @level
end

Instance Method Details

#codec_nameObject



314
# File 'lib/avro/data_file.rb', line 314

def codec_name; 'deflate'; end

#compress(data) ⇒ Object



327
328
329
330
331
332
333
# File 'lib/avro/data_file.rb', line 327

def compress(data)
  zstream = Zlib::Deflate.new(level, -Zlib::MAX_WBITS)
  compressed = zstream.deflate(data)
  compressed << zstream.finish
ensure
  zstream.close
end

#decompress(compressed) ⇒ Object



316
317
318
319
320
321
322
323
324
325
# File 'lib/avro/data_file.rb', line 316

def decompress(compressed)
  # Passing a negative number to Inflate puts it into "raw" RFC1951 mode
  # (without the RFC1950 header & checksum). See the docs for
  # inflateInit2 in http://www.zlib.net/manual.html
  zstream = Zlib::Inflate.new(-Zlib::MAX_WBITS)
  data = zstream.inflate(compressed)
  data << zstream.finish
ensure
  zstream.close
end