Class: Pack::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/pack/stream.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, buffer = "") ⇒ Stream

Returns a new instance of Stream.



8
9
10
11
12
13
14
# File 'lib/pack/stream.rb', line 8

def initialize(input, buffer = "")
  @input   = input
  @digest  = Digest::SHA1.new
  @offset  = 0
  @buffer  = new_byte_string.concat(buffer)
  @capture = nil
end

Instance Attribute Details

#digestObject (readonly)

Returns the value of attribute digest.



6
7
8
# File 'lib/pack/stream.rb', line 6

def digest
  @digest
end

#offsetObject (readonly)

Returns the value of attribute offset.



6
7
8
# File 'lib/pack/stream.rb', line 6

def offset
  @offset
end

Instance Method Details

#captureObject



16
17
18
19
20
21
22
23
24
# File 'lib/pack/stream.rb', line 16

def capture
  @capture = new_byte_string
  result   = [yield, @capture]

  @digest.update(@capture)
  @capture = nil

  result
end

#read(size) ⇒ Object



32
33
34
35
36
# File 'lib/pack/stream.rb', line 32

def read(size)
  data = read_buffered(size)
  update_state(data)
  data
end

#read_nonblock(size) ⇒ Object



38
39
40
41
42
# File 'lib/pack/stream.rb', line 38

def read_nonblock(size)
  data = read_buffered(size, false)
  update_state(data)
  data
end

#readbyteObject



44
45
46
# File 'lib/pack/stream.rb', line 44

def readbyte
  read(1).bytes.first
end

#seek(amount, whence = IO::SEEK_SET) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/pack/stream.rb', line 48

def seek(amount, whence = IO::SEEK_SET)
  return unless amount < 0

  data = @capture.slice!(amount .. -1)
  @buffer.prepend(data)
  @offset += amount
end

#verify_checksumObject



26
27
28
29
30
# File 'lib/pack/stream.rb', line 26

def verify_checksum
  unless read_buffered(20) == @digest.digest
    raise InvalidPack, "Checksum does not match value read from pack"
  end
end