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.



250
251
252
253
254
255
# File 'lib/fastimage.rb', line 250

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.



248
249
250
# File 'lib/fastimage.rb', line 248

def pos
  @pos
end

Instance Method Details

#peek(n) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/fastimage.rb', line 257

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



273
274
275
276
277
278
# File 'lib/fastimage.rb', line 273

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

#skip(n) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/fastimage.rb', line 280

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