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, #read_string_int

Constructor Details

#initialize(read_fiber) ⇒ FiberStream

Returns a new instance of FiberStream.


391
392
393
394
395
396
# File 'lib/fastimage.rb', line 391

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


389
390
391
# File 'lib/fastimage.rb', line 389

def pos
  @pos
end

Instance Method Details

#peek(n) ⇒ Object


398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/fastimage.rb', line 398

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


414
415
416
417
418
419
# File 'lib/fastimage.rb', line 414

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

#skip(n) ⇒ Object


421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/fastimage.rb', line 421

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

    new_string.force_encoding("ASCII-8BIT") if String.method_defined? :force_encoding

    fetched += new_string.size
    @str = new_string
    @strpos = 0
  end
  @strpos = @strpos + n - discarded
  @pos += n
end