Class: Snappy::Reader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) {|_self| ... } ⇒ Reader

Returns a new instance of Reader.

Yields:

  • (_self)

Yield Parameters:



5
6
7
8
9
10
# File 'lib/snappy/reader.rb', line 5

def initialize(io)
  @io = io
  @io.set_encoding Encoding::ASCII_8BIT unless RUBY_VERSION =~ /^1\.8/
  read_header!
  yield self if block_given?
end

Instance Attribute Details

#default_versionObject (readonly)

Returns the value of attribute default_version.



3
4
5
# File 'lib/snappy/reader.rb', line 3

def default_version
  @default_version
end

#ioObject (readonly)

Returns the value of attribute io.



3
4
5
# File 'lib/snappy/reader.rb', line 3

def io
  @io
end

#magicObject (readonly)

Returns the value of attribute magic.



3
4
5
# File 'lib/snappy/reader.rb', line 3

def magic
  @magic
end

#minimum_compatible_versionObject (readonly)

Returns the value of attribute minimum_compatible_version.



3
4
5
# File 'lib/snappy/reader.rb', line 3

def minimum_compatible_version
  @minimum_compatible_version
end

Instance Method Details

#eachObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/snappy/reader.rb', line 12

def each
  until @io.eof?
    if @chunked
      size = @io.read(4).unpack('N').first
      yield Snappy.inflate(@io.read(size)) if block_given?
    else
      yield Snappy.inflate @io.read if block_given?
    end
  end
end

#each_line(sep_string = $/) {|last| ... } ⇒ Object

Yields:

  • (last)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/snappy/reader.rb', line 31

def each_line(sep_string=$/)
  last = ""
  each do |chunk|
    chunk = last + chunk
    lines = chunk.split(sep_string)
    last = lines.pop
    lines.each do |line|
      yield line if block_given?
    end
  end
  yield last
end

#readObject



23
24
25
26
27
28
29
# File 'lib/snappy/reader.rb', line 23

def read
  @buff = StringIO.new
  each do |chunk|
    @buff << chunk
  end
  @buff.string
end