Class: FastImage::FiberStream
- Inherits:
-
Object
- Object
- FastImage::FiberStream
- Includes:
- StreamUtil
- Defined in:
- lib/fastimage.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#pos ⇒ Object
readonly
Returns the value of attribute pos.
Instance Method Summary collapse
-
#initialize(read_fiber) ⇒ FiberStream
constructor
A new instance of FiberStream.
-
#peek(n) ⇒ Object
Peeking beyond the end of the input will raise.
- #read(n) ⇒ Object
- #skip(n) ⇒ Object
Methods included from StreamUtil
#read_byte, #read_int, #read_string_int
Constructor Details
#initialize(read_fiber) ⇒ FiberStream
418 419 420 421 422 423 |
# File 'lib/fastimage.rb', line 418 def initialize(read_fiber) @read_fiber = read_fiber @pos = 0 @strpos = 0 @str = '' end |
Instance Attribute Details
#pos ⇒ Object (readonly)
Returns the value of attribute pos.
416 417 418 |
# File 'lib/fastimage.rb', line 416 def pos @pos end |
Instance Method Details
#peek(n) ⇒ Object
Peeking beyond the end of the input will raise
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/fastimage.rb', line 426 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
442 443 444 445 446 447 |
# File 'lib/fastimage.rb', line 442 def read(n) result = peek(n) @strpos += n @pos += n result end |
#skip(n) ⇒ Object
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/fastimage.rb', line 449 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 |