Class: Krypt::Hex::Encoder

Inherits:
IOFilter show all
Defined in:
lib/krypt/codec/hex.rb

Overview

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

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

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

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

f = File.open("document", "rb")
hex = Krypt::Hex::Encoder.new(f)
hexdata = hex.read # => result is encoded
hex.close

Instance Method Summary collapse

Methods included from BaseCodec

#generic_read, #generic_write, #update_buffer

Methods inherited from IOFilter

#close, #initialize

Constructor Details

This class inherits a constructor from Krypt::IOFilter

Instance Method Details

#read(len = nil) ⇒ Object

call-seq:

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

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



52
53
54
55
# File 'lib/krypt/codec/hex.rb', line 52

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

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

call-seq:

out.write(string) -> Integer

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



64
65
66
# File 'lib/krypt/codec/hex.rb', line 64

def write(data)
  generic_write(data, 1) { |data| Krypt::Hex.encode(data) }
end