Class: FastImageParsing::FiberStream

Inherits:
Object
  • Object
show all
Includes:
StreamUtil
Defined in:
lib/fastimage/fastimage_parsing/fiber_stream.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StreamUtil

#read_byte, #read_int, #read_string_int

Constructor Details

#initialize(read_fiber) ⇒ FiberStream

read_fiber should return nil if it no longer has anything to return when resumed so the result of the whole Fiber block should be set to be nil in case yield is no longer called



9
10
11
12
13
14
# File 'lib/fastimage/fastimage_parsing/fiber_stream.rb', line 9

def initialize(read_fiber)
  @read_fiber = read_fiber
  @pos = 0
  @strpos = 0
  @str = ''
end

Instance Attribute Details

#posObject (readonly)

Returns the value of attribute pos.



4
5
6
# File 'lib/fastimage/fastimage_parsing/fiber_stream.rb', line 4

def pos
  @pos
end

Instance Method Details

#peek(n) ⇒ Object

Peeking beyond the end of the input will raise



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fastimage/fastimage_parsing/fiber_stream.rb', line 17

def peek(n)
  while @strpos + n > @str.size
    unused_str = @str[@strpos..-1]
  
    new_string = @read_fiber.resume
    raise FastImage::CannotParseImage if !new_string
    # we are dealing with bytes here, so force the encoding
    new_string.force_encoding("ASCII-8BIT") if new_string.respond_to? :force_encoding
  
    @str = unused_str + new_string
    @strpos = 0
  end
  
  @str[@strpos, n]
end

#read(n) ⇒ Object



33
34
35
36
37
38
# File 'lib/fastimage/fastimage_parsing/fiber_stream.rb', line 33

def read(n)
  result = peek(n)
  @strpos += n
  @pos += n
  result
end

#skip(n) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fastimage/fastimage_parsing/fiber_stream.rb', line 40

def skip(n)
  discarded = 0
  fetched = @str[@strpos..-1].size
  while n > fetched
    discarded += @str[@strpos..-1].size
    new_string = @read_fiber.resume
    raise FastImage::CannotParseImage if !new_string
  
    new_string.force_encoding("ASCII-8BIT") if new_string.respond_to? :force_encoding
  
    fetched += new_string.size
    @str = new_string
    @strpos = 0
  end
  @strpos = @strpos + n - discarded
  @pos += n
end