Class: BlockCipherKit::WriteWindowIO

Inherits:
Object
  • Object
show all
Defined in:
lib/block_cipher_kit/write_window_io.rb

Overview

Allows you to pass through the writes of a particular byte range only, discarding the rest :nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(io, offset, size) ⇒ WriteWindowIO

Returns a new instance of WriteWindowIO.



6
7
8
9
10
# File 'lib/block_cipher_kit/write_window_io.rb', line 6

def initialize(io, offset, size)
  @range = Range.new(offset, offset + size - 1)
  @io = io
  @pos = 0
end

Instance Method Details

#write(bytes) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/block_cipher_kit/write_window_io.rb', line 12

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