Class: Archive::Zip::Codec::NullEncryption::Encrypt

Inherits:
Object
  • Object
show all
Includes:
IO::Like
Defined in:
lib/archive/zip/codec/null_encryption.rb

Overview

Archive::Zip::Codec::NullEncryption::Encrypt is a writable, IO-like object which writes all data written to it directly to a delegate IO object. A close method is also provided which can optionally closed the delegate object.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Encrypt

Creates a new instance of this class using io as a data sink. io must be writable and must provide a write method as IO does or errors will be raised when performing write operations.

The flush_size attribute is set to 0 by default under the assumption that io is already buffered.



39
40
41
42
43
44
45
46
47
# File 'lib/archive/zip/codec/null_encryption.rb', line 39

def initialize(io)
  @io = io

  # Keep track of the total number of bytes written.
  @total_bytes_in = 0

  # Assume that the delegate IO object is already buffered.
  self.flush_size = 0
end

Class Method Details

.open(io) ⇒ Object

Creates a new instance of this class with the given argument using #new and then passes the instance to the given block. The #close method is guaranteed to be called after the block completes.

Equivalent to #new if no block is given.



22
23
24
25
26
27
28
29
30
31
# File 'lib/archive/zip/codec/null_encryption.rb', line 22

def self.open(io)
  encrypt_io = new(io)
  return encrypt_io unless block_given?

  begin
    yield(encrypt_io)
  ensure
    encrypt_io.close unless encrypt_io.closed?
  end
end

Instance Method Details

#close(close_delegate = true) ⇒ Object

Closes this object so that further write operations will fail. If close_delegate is true, the delegate object used as a data sink will also be closed using its close method.



52
53
54
55
# File 'lib/archive/zip/codec/null_encryption.rb', line 52

def close(close_delegate = true)
  super()
  @io.close if close_delegate
end

#unbuffered_seek(offset, whence = IO::SEEK_SET) ⇒ Object (private)

Allows resetting this object and the delegate object back to the beginning of the stream or reporting the current position in the stream.

Raises Errno::EINVAL unless offset is 0 and whence is either IO::SEEK_SET or IO::SEEK_CUR. Raises Errno::EINVAL if whence is IO::SEEK_SEK and the delegate object does not respond to the rewind method.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/archive/zip/codec/null_encryption.rb', line 66

def unbuffered_seek(offset, whence = IO::SEEK_SET)
  unless offset == 0 &&
         ((whence == IO::SEEK_SET && @io.respond_to?(:rewind)) ||
          whence == IO::SEEK_CUR) then
    raise Errno::EINVAL
  end

  case whence
  when IO::SEEK_SET
    @io.rewind
    @total_bytes_in = 0
  when IO::SEEK_CUR
    @total_bytes_in
  end
end

#unbuffered_write(string) ⇒ Object (private)

Writes string to the delegate IO object and returns the result.



83
84
85
86
87
# File 'lib/archive/zip/codec/null_encryption.rb', line 83

def unbuffered_write(string)
  bytes_written = @io.write(string)
  @total_bytes_in += bytes_written
  bytes_written
end