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.



372
373
374
375
376
377
# File 'lib/fastimage.rb', line 372

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.



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

def pos
  @pos
end

Instance Method Details

#peek(n) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/fastimage.rb', line 379

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

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

#read(n) ⇒ Object



395
396
397
398
399
400
# File 'lib/fastimage.rb', line 395

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