lib/symmetric_encryption/writer.rb in symmetric-encryption-3.7.1 vs lib/symmetric_encryption/writer.rb in symmetric-encryption-3.7.2
- old
+ new
@@ -107,11 +107,11 @@
begin
file = self.new(ios, options)
file = Zlib::GzipWriter.new(file) if 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
# Encrypt data before writing to the supplied stream
def initialize(ios,options={})
@@ -150,10 +150,11 @@
random_iv ? iv : nil,
random_key ? key : nil,
cipher_name))
end
@size = 0
+ @closed = false
end
# Close the IO Stream
# Flushes any unwritten data
#
@@ -165,15 +166,17 @@
#
# It is recommended to call Symmetric::EncryptedStream.open
# 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?
if size > 0
final = @stream_cipher.final
@ios.write(final) if final.length > 0
end
@ios.close if close_child_stream
+ @closed = true
end
# Write to the IO Stream as encrypted data
# Returns the number of bytes written
def write(data)
@@ -200,9 +203,13 @@
# Does not flush internal buffers since encryption requires all data to
# be written following the encryption block size
# Needed by XLS gem
def flush
@ios.flush
+ end
+
+ def closed?
+ @closed || @ios.respond_to?(:closed?) && @ios.closed?
end
# Returns [Integer] the number of unencrypted and uncompressed bytes
# written to the file so far
attr_reader :size