2010-11-30 16:27:59 +08:00
|
|
|
module Zip
|
2013-06-03 02:33:03 +08:00
|
|
|
class Inflater < Decompressor #:nodoc:all
|
|
|
|
def initialize(input_stream)
|
2010-11-30 16:27:59 +08:00
|
|
|
super
|
2013-06-03 02:33:03 +08:00
|
|
|
@zlib_inflater = ::Zlib::Inflate.new(-Zlib::MAX_WBITS)
|
|
|
|
@output_buffer = ''
|
|
|
|
@has_returned_empty_string = false
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2011-11-18 04:53:04 +08:00
|
|
|
|
2013-06-03 02:33:03 +08:00
|
|
|
def sysread(number_of_bytes = nil, buf = nil)
|
|
|
|
readEverything = number_of_bytes.nil?
|
|
|
|
while (readEverything || @output_buffer.bytesize < number_of_bytes)
|
2011-11-18 04:53:04 +08:00
|
|
|
break if internal_input_finished?
|
2013-06-03 02:33:03 +08:00
|
|
|
@output_buffer << internal_produce_input(buf)
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2013-06-03 02:33:03 +08:00
|
|
|
return value_when_finished if @output_buffer.bytesize == 0 && input_finished?
|
|
|
|
end_index = number_of_bytes.nil? ? @output_buffer.bytesize : number_of_bytes
|
|
|
|
@output_buffer.slice!(0...end_index)
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2011-11-18 04:53:04 +08:00
|
|
|
|
2010-11-30 16:27:59 +08:00
|
|
|
def produce_input
|
2013-06-03 02:33:03 +08:00
|
|
|
if (@output_buffer.empty?)
|
|
|
|
internal_produce_input
|
2010-11-30 16:27:59 +08:00
|
|
|
else
|
2013-06-03 02:33:03 +08:00
|
|
|
@output_buffer.slice!(0...(@output_buffer.length))
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# to be used with produce_input, not read (as read may still have more data cached)
|
|
|
|
# is data cached anywhere other than @outputBuffer? the comment above may be wrong
|
|
|
|
def input_finished?
|
2013-06-03 02:33:03 +08:00
|
|
|
@output_buffer.empty? && internal_input_finished?
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2013-06-03 02:33:03 +08:00
|
|
|
|
2010-11-30 16:27:59 +08:00
|
|
|
alias :eof :input_finished?
|
|
|
|
alias :eof? :input_finished?
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def internal_produce_input(buf = nil)
|
|
|
|
retried = 0
|
|
|
|
begin
|
2013-06-03 02:33:03 +08:00
|
|
|
@zlib_inflater.inflate(@input_stream.read(Decompressor::CHUNK_SIZE, buf))
|
2010-11-30 16:27:59 +08:00
|
|
|
rescue Zlib::BufError
|
2013-06-03 02:33:03 +08:00
|
|
|
raise if retried >= 5 # how many times should we retry?
|
2010-11-30 16:27:59 +08:00
|
|
|
retried += 1
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal_input_finished?
|
2013-06-03 02:33:03 +08:00
|
|
|
@zlib_inflater.finished?
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
2013-06-03 02:33:03 +08:00
|
|
|
def value_when_finished # mimic behaviour of ruby File object.
|
|
|
|
return if @has_returned_empty_string
|
|
|
|
@has_returned_empty_string = true
|
|
|
|
''
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Copyright (C) 2002, 2003 Thomas Sondergaard
|
|
|
|
# rubyzip is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the ruby license.
|