Class: Snappy::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/snappy/writer.rb

Constant Summary collapse

MAGIC =
Snappy.b("\x82SNAPPY\x0")
DEFAULT_VERSION =
1
MINIMUM_COMPATIBLE_VERSION =
1
DEFAULT_BLOCK_SIZE =
32 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, block_size = DEFAULT_BLOCK_SIZE) ⇒ Writer

Returns a new instance of Writer.



14
15
16
17
18
19
20
21
22
23
# File 'lib/snappy/writer.rb', line 14

def initialize(io, block_size = DEFAULT_BLOCK_SIZE)
  @block_size = block_size
  @buffer = String.new
  @io = Snappy.set_encoding io
  write_header!
  if block_given?
    yield self
    dump!
  end
end

Instance Attribute Details

#block_sizeObject (readonly)

Returns the value of attribute block_size.



12
13
14
# File 'lib/snappy/writer.rb', line 12

def block_size
  @block_size
end

#ioObject (readonly)

Returns the value of attribute io.



12
13
14
# File 'lib/snappy/writer.rb', line 12

def io
  @io
end

Instance Method Details

#<<(msg) ⇒ Object Also known as: write



25
26
27
28
# File 'lib/snappy/writer.rb', line 25

def <<(msg)
  @buffer << msg.to_s
  dump! if @buffer.size > @block_size
end

#closeObject



41
42
43
# File 'lib/snappy/writer.rb', line 41

def close
  @io.close
end

#dump!Object Also known as: flush



32
33
34
35
36
37
# File 'lib/snappy/writer.rb', line 32

def dump!
  compressed = Snappy.deflate(@buffer)
  @io << [compressed.size, compressed].pack("Na#{compressed.size}")
  @io.flush
  @buffer.clear
end