Mimic IO#read return values in Decompressor#read

This commit is contained in:
Jan-Joost Spanjers 2020-01-05 16:09:18 +01:00
parent c66277db58
commit 456bd4d92c
2 changed files with 3 additions and 16 deletions

View File

@ -5,17 +5,16 @@ module Zip
@buffer = ''.dup
@zlib_inflater = ::Zlib::Inflate.new(-Zlib::MAX_WBITS)
@has_returned_empty_string = false
end
def read(length = nil, outbuf = '')
return ((length.nil? || length.zero?) ? "" : nil) if eof
while length.nil? || (@buffer.bytesize < length)
break if input_finished?
@buffer << produce_input
end
return value_when_finished if eof?
outbuf.replace(@buffer.slice!(0...(length || @buffer.bytesize)))
end
@ -43,12 +42,6 @@ module Zip
def input_finished?
@zlib_inflater.finished?
end
def value_when_finished # mimic behaviour of ruby File object.
return if @has_returned_empty_string
@has_returned_empty_string = true
''
end
end
end

View File

@ -3,16 +3,10 @@ module Zip
def initialize(*args)
super
@read_so_far = 0
@has_returned_empty_string = false
end
def read(length = nil, outbuf = '')
if eof?
has_returned_empty_string_val = @has_returned_empty_string
@has_returned_empty_string = true
return '' unless has_returned_empty_string_val
return
end
return ((length.nil? || length.zero?) ? "" : nil) if eof
if length.nil? || (@read_so_far + length) > decompressed_size
length = decompressed_size - @read_so_far