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:



10
11
12
13
14
# File 'lib/snappy/reader.rb', line 10

def initialize(io)
  @io = Snappy.set_encoding io
  read_header!
  yield self if block_given?
end

Instance Attribute Details

#default_versionObject (readonly)

Returns the value of attribute default_version.



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

def default_version
  @default_version
end

#ioObject (readonly)

Returns the value of attribute io.



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

def io
  @io
end

#magicObject (readonly)

Returns the value of attribute magic.



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

def magic
  @magic
end

#minimum_compatible_versionObject (readonly)

Returns the value of attribute minimum_compatible_version.



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

def minimum_compatible_version
  @minimum_compatible_version
end

Instance Method Details

#eachObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/snappy/reader.rb', line 16

def each
  return to_enum unless block_given?

  until @io.eof?
    if @chunked
      size = @io.read(4).unpack1("N")
      yield Snappy.inflate(@io.read(size))
    else
      yield Snappy.inflate(@io.read)
    end
  end
end

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

Yields:

  • (last)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/snappy/reader.rb', line 37

def each_line(sep_string = $/)
  return to_enum(:each_line, sep_string) unless block_given?

  last = ""
  each do |chunk|
    chunk = last + chunk
    lines = chunk.split(sep_string)
    last = lines.pop
    lines.each do |line|
      yield line
    end
  end
  yield last
end

#readObject



29
30
31
32
33
34
35
# File 'lib/snappy/reader.rb', line 29

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