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
Returns a new instance of FiberStream.
474 475 476 477 478 479 |
# File 'lib/fastimage.rb', line 474 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.
472 473 474 |
# File 'lib/fastimage.rb', line 472 def pos @pos end |
Instance Method Details
#peek(n) ⇒ Object
Peeking beyond the end of the input will raise
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/fastimage.rb', line 482 def peek(n) while @strpos + n > @str.size unused_str = @str[@strpos..-1] new_string = @read_fiber.resume new_string = @read_fiber.resume if new_string.is_a? Net::ReadAdapter raise CannotParseImage if !new_string # we are dealing with bytes here, so force the encoding new_string.force_encoding("ASCII-8BIT") if new_string.respond_to? :force_encoding @str = unused_str + new_string @strpos = 0 end @str[@strpos, n] end |
#read(n) ⇒ Object
499 500 501 502 503 504 |
# File 'lib/fastimage.rb', line 499 def read(n) result = peek(n) @strpos += n @pos += n result end |
#skip(n) ⇒ Object
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
# File 'lib/fastimage.rb', line 506 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 new_string.respond_to? :force_encoding fetched += new_string.size @str = new_string @strpos = 0 end @strpos = @strpos + n - discarded @pos += n end |