Sha256: a1557ca40a24e1c63fa2332b49e7a4b34073e89b2b0ffb74b14be52e0bc0984f

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require 'snappy/shim'
require 'snappy/writer'

module Snappy
  class Reader
    attr_reader :io, :magic, :default_version, :minimum_compatible_version

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

    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

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

    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

    private

    def read_header!
      header = @io.read Snappy::Writer::MAGIC.length
      if header.length == Snappy::Writer::MAGIC.length && header == Snappy::Writer::MAGIC
        @magic, @default_version, @minimum_compatible_version = header, *@io.read(8).unpack('NN')
        @chunked = true
      else
        @io.rewind
        @chunked = false
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
snappy-0.0.17-java lib/snappy/reader.rb
snappy-0.0.17 lib/snappy/reader.rb
snappy-0.0.16-java lib/snappy/reader.rb
snappy-0.0.16 lib/snappy/reader.rb