lib/symmetric_encryption/reader.rb in symmetric-encryption-3.7.1 vs lib/symmetric_encryption/reader.rb in symmetric-encryption-3.7.2
- old
+ new
@@ -85,11 +85,11 @@
begin
file = self.new(ios, options)
file = Zlib::GzipReader.new(file) if !file.eof? && (file.compressed? || compress)
block ? block.call(file) : file
ensure
- file.close if block && file
+ file.close if block && file && (file.respond_to?(:closed?) && !file.closed?)
end
end
# Returns [true|false] whether the file or stream contains any data
# excluding the header should it have one
@@ -112,10 +112,11 @@
def initialize(ios,options={})
@ios = ios
@buffer_size = options.fetch(:buffer_size, 4096).to_i
@version = options[:version]
@header_present = false
+ @closed = false
raise(ArgumentError, 'Buffer size cannot be smaller than 128') unless @buffer_size >= 128
read_header
end
@@ -151,11 +152,13 @@
#
# It is recommended to call Symmetric::EncryptedStream.open or Symmetric::EncryptedStream.io
# rather than creating an instance of Symmetric::EncryptedStream directly to
# ensure that the encrypted stream is closed before the stream itself is closed
def close(close_child_stream = true)
+ return if closed?
@ios.close if close_child_stream
+ @closed = true
end
# Flush the read stream
# Needed by XLS gem
def flush
@@ -351,9 +354,13 @@
# Read a block of data and append the decrypted data in the read buffer
def read_block
buf = @ios.read(@buffer_size)
@read_buffer << @stream_cipher.update(buf) if buf && buf.length > 0
@read_buffer << @stream_cipher.final if @ios.eof?
+ end
+
+ def closed?
+ @closed || @ios.respond_to?(:closed?) && @ios.closed?
end
end
end