Class: BlockCipherKit::IOLens
- Inherits:
-
Object
- Object
- BlockCipherKit::IOLens
- Defined in:
- lib/block_cipher_kit/io_lens.rb
Overview
Allows you to pass through the writes of a particular byte range only, discarding the rest
Instance Method Summary collapse
-
#initialize(io, range) ⇒ IOLens
constructor
A new instance of IOLens.
- #write(bytes) ⇒ Object
Constructor Details
#initialize(io, range) ⇒ IOLens
Returns a new instance of IOLens.
5 6 7 8 9 |
# File 'lib/block_cipher_kit/io_lens.rb', line 5 def initialize(io, range) @io = io @range = range @pos = 0 end |
Instance Method Details
#write(bytes) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/block_cipher_kit/io_lens.rb', line 11 def write(bytes) previous_pos, @pos = @pos, @pos + bytes.bytesize return 0 if bytes.bytesize == 0 location_in_output = Range.new(previous_pos, previous_pos + bytes.bytesize - 1) overlap = intersection_of(@range, location_in_output) if overlap at = overlap.begin - previous_pos n = overlap.end - overlap.begin + 1 @io.write(bytes.byteslice(at, n)) end bytes.bytesize end |