Class: Rszr::Stream

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, start: 0) ⇒ Stream

Returns a new instance of Stream.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rszr/stream.rb', line 6

def initialize(data, start: 0)
  raise ArgumentError, 'start must be > 0' if start < 0
  @data = case data
    when IO then data
    when String then StringIO.new(data)
    when Stream then data.data
  else
    raise ArgumentError, "data must be File or String, got #{data.class}"
  end
  @data.binmode
  @data.seek(start)
  @pos = 0
end

Instance Attribute Details

#posObject (readonly)

Returns the value of attribute pos.



3
4
5
# File 'lib/rszr/stream.rb', line 3

def pos
  @pos
end

Instance Method Details

#fast_forwardObject



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

def fast_forward
  @pos = 0
  self
end

#peek(n) ⇒ Object



24
25
26
27
28
29
# File 'lib/rszr/stream.rb', line 24

def peek(n)
  old_pos = @data.pos
  @data.read(n)
ensure
  @data.pos = old_pos
end

#read(n) ⇒ Object



20
21
22
# File 'lib/rszr/stream.rb', line 20

def read(n)
  @data.read(n).tap { @pos += n }
end

#read_byteObject



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

def read_byte
  read(1)[0].ord
end

#read_intObject



48
49
50
# File 'lib/rszr/stream.rb', line 48

def read_int
  read(2).unpack('n')[0]
end

#read_string_intObject



52
53
54
55
56
57
58
# File 'lib/rszr/stream.rb', line 52

def read_string_int
  value = []
  while read(1) =~ /(\d)/
    value << $1
  end
  value.join.to_i
end

#skip(n) ⇒ Object



31
32
33
# File 'lib/rszr/stream.rb', line 31

def skip(n)
  @data.seek(n, IO::SEEK_CUR).tap { @pos += n }
end

#substreamObject



35
36
37
# File 'lib/rszr/stream.rb', line 35

def substream
  self.class.new(self, @data.pos)
end