Class: RUtilAnts::Archive::StringWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/rUtilAnts/Archive.rb

Overview

Class used to give the encoders a streaming interface

Constant Summary collapse

BUFFER_SIZE =

Buffer size used in bytes

8388608

Instance Method Summary collapse

Constructor Details

#initialize(iPassword, iSalt, oFile) ⇒ StringWriter

Constructor

Parameters
  • iPassword (String): Password encrypting data

  • iSalt (String): Salt encoding data

  • oFile (IO): The IO that will receive encrypted data



21
22
23
24
# File 'lib/rUtilAnts/Archive.rb', line 21

def initialize(iPassword, iSalt, oFile)
  @Password, @Salt, @File = iPassword, iSalt, oFile
  @Buffer = ''
end

Instance Method Details

#<<(iData) ⇒ Object

Add a string to write

Parameters
  • iData (String): The data to encrypt and write



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rUtilAnts/Archive.rb', line 30

def <<(iData)
  # Add to the buffer
  if (@Buffer.size + iData.size < BUFFER_SIZE)
    @Buffer.concat(iData)
  else
    # Flush the completed buffer
    lIdxData = BUFFER_SIZE-@Buffer.size
    @Buffer.concat(iData[0..lIdxData-1])
    flush
    # And now flush new data buffers
    @Buffer = iData[lIdxData..lIdxData+BUFFER_SIZE-1]
    while (@Buffer.size == BUFFER_SIZE)
      flush
      lIdxData += BUFFER_SIZE
      @Buffer = iData[lIdxData..lIdxData+BUFFER_SIZE-1]
    end
  end
end

#flushObject

Flush current data in the file



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rUtilAnts/Archive.rb', line 50

def flush
  if (!@Buffer.empty?)
    # Compress
    lZippedData = Zlib::Deflate.new.deflate(@Buffer, Zlib::FINISH)
    # Encrypt
    lEncryptedData = EzCrypto::Key.encrypt_with_password(@Password, @Salt, lZippedData)
    # Write
    @File.write([lEncryptedData.size].pack('l'))
    @File.write(lEncryptedData)
    @Buffer = ''
  end
end