Skip to content

Commit

Permalink
Support output_io in Compressable
Browse files Browse the repository at this point in the history
  • Loading branch information
ganmacs committed Aug 30, 2016
1 parent 72dd08c commit 53e585f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
54 changes: 36 additions & 18 deletions lib/fluent/plugin/compressable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,50 @@
module Fluent
module Plugin
module Compressable
def compress(data)
io = StringIO.new
def compress(data, **kwargs)
output_io = kwargs[:output_io]
io = output_io || StringIO.new
Zlib::GzipWriter.wrap(io) do |gz|
gz.write data
end
io.string

output_io || io.string
end

# compressed_data is String like `compress(data1) + compress(data2) + ... + compress(dataN)`
# https://www.ruby-forum.com/topic/971591#979503
def decompress(compressed_data, **kargs)
io = kargs[:io] || StringIO.new(compressed_data)
ret = ''

loop do
gz = Zlib::GzipReader.new(io)
ret += gz.read
unused = gz.unused
gz.finish

break if unused.nil?
adjust = unused.length
io.pos -= adjust
end
def decompress(compressed_data, **kwargs)
return compressed_data if compressed_data.empty?
io = StringIO.new(compressed_data)

if kwargs[:output_io]
out = kwargs[:output_io]
loop do
gz = Zlib::GzipReader.new(io)
out.write(gz.read)
unused = gz.unused
gz.finish

ret
break if unused.nil?
adjust = unused.length
io.pos -= adjust
end
out
else
out = ''
loop do
gz = Zlib::GzipReader.new(io)
out += gz.read
unused = gz.unused
gz.finish

break if unused.nil?
adjust = unused.length
io.pos -= adjust
end

out
end
end
end
end
Expand Down
14 changes: 12 additions & 2 deletions test/plugin/test_compressable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ class CompressableTest < Test::Unit::TestCase
compressed_str = compress(str)
assert_not_equal compressed_str, str
end

test 'write compressed data to IO with output_io option' do
str = 'text data'
compressed_str = compress(str)
io = StringIO.new
compress(str, output_io: io)
assert_equal compressed_str, io.string
end
end

sub_test_case '#decompress' do
Expand All @@ -19,10 +27,12 @@ class CompressableTest < Test::Unit::TestCase
assert_equal str, decompress(compressed_str)
end

test 'decompress compressed data with io option' do
test 'write decompressed data to IO with output_io option' do
str = 'text data'
compressed_str = compress(str)
assert_equal str, decompress('', io: StringIO.new(compressed_str))
io = StringIO.new
decompress(compressed_str, output_io: io)
assert_equal str, io.string
end

test 'decompress multiple compressed data' do
Expand Down

0 comments on commit 53e585f

Please sign in to comment.