Sha256: bad1d7fec05bfd9d96295fb80bd7debfc134285b2a55c9fb11e4ef485d06d7c9

Contents?: true

Size: 1.32 KB

Versions: 11

Compression:

Stored size: 1.32 KB

Contents

# encoding: UTF-8
module Yajl
  module Deflate
    # 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 StreamReader < ::Zlib::Inflate

      # Wrapper to the initialize method so we can set the initial IO to parse from.
      def initialize(io, options)
        @io = io
        super(options)
      end

      # A helper method to allow use similar to IO#read
      def read(len=nil, buffer=nil)
        if val = @io.read(len)
          unless buffer.nil?
            buffer.replace(inflate(val))
            return buffer
          end
          inflate(@io.read(len))
        else
          nil
        end
      end

      # Helper method for one-off parsing from a deflate-compressed stream
      #
      # See Yajl::Parser#parse for parameter documentation
      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
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
yajl-ruby-0.8.3 lib/yajl/deflate/stream_reader.rb
yajl-ruby-0.8.2 lib/yajl/deflate/stream_reader.rb
yajl-ruby-0.8.1 lib/yajl/deflate/stream_reader.rb
yajl-ruby-0.8.0 lib/yajl/deflate/stream_reader.rb
yajl-ruby-0.7.9 lib/yajl/deflate/stream_reader.rb
yajl-ruby-0.7.8 lib/yajl/deflate/stream_reader.rb
yajl-ruby-0.7.7 lib/yajl/deflate/stream_reader.rb
benofsky-yajl-ruby-0.7.7 lib/yajl/deflate/stream_reader.rb
benofsky-yajl-ruby-0.7.6 lib/yajl/deflate/stream_reader.rb
yajl-ruby-0.7.6 lib/yajl/deflate/stream_reader.rb
yajl-ruby-0.7.5 lib/yajl/deflate/stream_reader.rb