Class: FastImage::FiberStream

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StreamUtil

#read_byte, #read_int

Constructor Details

#initialize(read_fiber) ⇒ FiberStream

Returns a new instance of FiberStream.



347
348
349
350
351
352
# File 'lib/fastimage.rb', line 347

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.



345
346
347
# File 'lib/fastimage.rb', line 345

def pos
  @pos
end

Instance Method Details

#peek(n) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/fastimage.rb', line 354

def peek(n)
  while @strpos + n - 1 >= @str.size
    unused_str = @str[@strpos..-1]
    new_string = @read_fiber.resume
    raise CannotParseImage if !new_string

    # we are dealing with bytes here, so force the encoding
    new_string.force_encoding("ASCII-8BIT") if String.method_defined? :force_encoding

    @str = unused_str + new_string
    @strpos = 0
  end

  result = @str[@strpos..(@strpos + n - 1)]
end

#read(n) ⇒ Object



370
371
372
373
374
375
# File 'lib/fastimage.rb', line 370

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