Class: Yajl::Deflate::StreamReader

Inherits:
Zlib::Inflate
  • Object
show all
Defined in:
lib/yajl/deflate/stream_reader.rb

Overview

This is a wrapper around Zlib::Inflate, creating a #read method that adheres to the IO spec, allowing for two parameters (length, and buffer)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, options) ⇒ StreamReader

Wrapper to the initialize method so we can set the initial IO to parse from.



9
10
11
12
# File 'lib/yajl/deflate/stream_reader.rb', line 9

def initialize(io, options)
  @io = io
  super(options)
end

Class Method Details

.parse(input, options = {}, buffer_size = nil, &block) ⇒ Object

Helper method for one-off parsing from a deflate-compressed stream

See Yajl::Parser#parse for parameter documentation



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/yajl/deflate/stream_reader.rb', line 24

def self.parse(input, options={}, buffer_size=nil, &block)
  if input.is_a?(String)
    input = StringIO.new(input)
  end
  
  if options.is_a?(Hash)
    deflate_options = options.delete(:deflate_options)
    Yajl::Parser.new(options).parse(new(input, deflate_options), buffer_size, &block)
  elsif options.is_a?(Fixnum)
    Yajl::Parser.new.parse(new(input, options), buffer_size, &block)
  end
end

Instance Method Details

#read(len = nil, buffer = nil) ⇒ Object

A helper method to allow use similar to IO#read



15
16
17
18
# File 'lib/yajl/deflate/stream_reader.rb', line 15

def read(len=nil, buffer=nil)
  buffer.replace inflate(@io.read(len)) and return unless buffer.nil?
  inflate(@io.read(len))
end