Class: Krypt::Base64::Encoder

Inherits:
IOFilter
  • Object
show all
Defined in:
lib/krypt/codec/base64.rb

Overview

Base64-encodes any data written or read from it in the process.

Example: Base64-encode data and write it to a file

f = File.open("b64", "wb")
b64 = Krypt::Base64::Encoder.new(f)
b64 << "one"
b64 << "two"
b64.close # => contents in file will be encoded

Example: Reading from a file and Base64-encoding the data

f = File.open("document", "rb")
b64 = Krypt::Base64::Encoder.new(f)
b64data = b64.read # => result is encoded
b64.close

Instance Method Summary collapse

Methods included from Krypt::BaseCodec

#generic_read, #generic_write, #update_buffer

Methods inherited from IOFilter

#initialize

Constructor Details

This class inherits a constructor from Krypt::IOFilter

Instance Method Details

#closeObject



82
83
84
85
# File 'lib/krypt/codec/base64.rb', line 82

def close
  generic_close
  super
end

#read(len = nil) ⇒ Object

call-seq:

in.read([len=nil]) -> String or nil

Reads from the underlying IO and Base64-encodes the data. Please see IO#read for details. Note that in-place reading into a buffer is not supported.



65
66
67
68
# File 'lib/krypt/codec/base64.rb', line 65

def read(len=nil)
  read_len = len ? compute_encode_read_len(len) : nil
  generic_read(len, read_len) { |data| Krypt::Base64.encode(data) }
end

#write(data) ⇒ Object Also known as: <<

call-seq:

out.write(string) -> Integer

Base64-encodes string and writes it to the underlying IO. Please see IO#write for further details.



77
78
79
# File 'lib/krypt/codec/base64.rb', line 77

def write(data)
  generic_write(data, 3) { |data| Krypt::Base64.encode(data) }
end