Class: Krypt::Hex::Decoder

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

Overview

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

Example: Reading and decoding hex-encoded data from a file

f = File.open("hex", "rb")
hex = Krypt::Hex::Decoder.new(f)
plain = hex.read # => result is decoded
hex.close

Example: Writing to a file while hex-decoding the data

f = File.open("document", "wb")
hex = Krypt::Hex::Decoder.new(f)
hexdata = ... #some hex-encoded data
hex << hexdata
hex.close # => contents in file will be decoded

Instance Method Summary collapse

Methods included from 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



116
117
118
119
# File 'lib/krypt/codec/hex.rb', line 116

def close
  generic_close
  super
end

#read(len = nil) ⇒ Object

call-seq:

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

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



99
100
101
102
# File 'lib/krypt/codec/hex.rb', line 99

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

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

call-seq:

out.write(string) -> Integer

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



111
112
113
# File 'lib/krypt/codec/hex.rb', line 111

def write(data)
  generic_write(data, 2) { |data| Krypt::Hex.decode(data) }
end