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.



316
317
318
319
320
321
# File 'lib/fastimage.rb', line 316

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.



314
315
316
# File 'lib/fastimage.rb', line 314

def pos
  @pos
end

Instance Method Details

#peek(n) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/fastimage.rb', line 323

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



339
340
341
342
343
344
# File 'lib/fastimage.rb', line 339

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