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 @buffer = ''.dup
@zlib_inflater = ::Zlib::Inflate.new(-Zlib::MAX_WBITS) @zlib_inflater = ::Zlib::Inflate.new(-Zlib::MAX_WBITS)
@has_returned_empty_string = false
end end
def read(length = nil, outbuf = '') def read(length = nil, outbuf = '')
return ((length.nil? || length.zero?) ? "" : nil) if eof
while length.nil? || (@buffer.bytesize < length) while length.nil? || (@buffer.bytesize < length)
break if input_finished? break if input_finished?
@buffer << produce_input @buffer << produce_input
end end
return value_when_finished if eof?
outbuf.replace(@buffer.slice!(0...(length || @buffer.bytesize))) outbuf.replace(@buffer.slice!(0...(length || @buffer.bytesize)))
end end
@ -43,12 +42,6 @@ module Zip
def input_finished? def input_finished?
@zlib_inflater.finished? @zlib_inflater.finished?
end 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
end end

View File

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