Skip to content

Commit

Permalink
fix up gzip/bzip2/deflate helpers back to accepting and using an exis…
Browse files Browse the repository at this point in the history
…ting read buffer
  • Loading branch information
brianmario committed Mar 21, 2010
1 parent 95a59e2 commit 937d53a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
13 changes: 13 additions & 0 deletions lib/yajl/bzip2/stream_reader.rb
Expand Up @@ -4,6 +4,19 @@ module Bzip2
# This is a wrapper around Bzip::Reader to allow it's #read method to adhere
# to the IO spec, allowing for two parameters (length, and buffer)
class StreamReader < ::Bzip2::Reader
# A helper method to allow use similar to IO#read
def read(len=nil, buffer=nil)
if val = super(len)
unless buffer.nil?
buffer.replace(val)
return buffer
end
super(len)
else
nil
end
end

# Helper method for one-off parsing from a bzip2-compressed stream
#
# See Yajl::Parser#parse for parameter documentation
Expand Down
13 changes: 10 additions & 3 deletions lib/yajl/deflate/stream_reader.rb
Expand Up @@ -12,9 +12,16 @@ def initialize(io, options)
end

# A helper method to allow use similar to IO#read
def read(len=nil)
return nil if finished? or ended?
inflate(@io.read(len))
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
Expand Down
13 changes: 13 additions & 0 deletions lib/yajl/gzip/stream_reader.rb
Expand Up @@ -4,6 +4,19 @@ module Gzip
# This is a wrapper around Zlib::GzipReader to allow it's #read method to adhere
# to the IO spec, allowing for two parameters (length, and buffer)
class StreamReader < ::Zlib::GzipReader
# A helper method to allow use similar to IO#read
def read(len=nil, buffer=nil)
if val = super(len)
unless buffer.nil?
buffer.replace(val)
return buffer
end
super(len)
else
nil
end
end

# Helper method for one-off parsing from a gzip-compressed stream
#
# See Yajl::Parser#parse for parameter documentation
Expand Down

0 comments on commit 937d53a

Please sign in to comment.