Class: BinData::IO::RawIO

Inherits:
Object
  • Object
show all
Defined in:
lib/bindata/io.rb

Overview

API used to access the raw data stream.

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ RawIO

Returns a new instance of RawIO.



316
317
318
319
320
321
322
323
324
325
# File 'lib/bindata/io.rb', line 316

def initialize(io)
  @io = io
  @pos = 0

  if is_seekable?(io)
    @initial_pos = io.pos
  else
    singleton_class.prepend(UnSeekableIO)
  end
end

Instance Method Details

#is_seekable?(io) ⇒ Boolean

Returns:

  • (Boolean)


327
328
329
330
331
# File 'lib/bindata/io.rb', line 327

def is_seekable?(io)
  io.pos
rescue NoMethodError, Errno::ESPIPE, Errno::EPIPE, Errno::EINVAL
  nil
end

#num_bytes_remainingObject



337
338
339
340
341
342
343
344
# File 'lib/bindata/io.rb', line 337

def num_bytes_remaining
  start_mark = @io.pos
  @io.seek(0, ::IO::SEEK_END)
  end_mark = @io.pos
  @io.seek(start_mark, ::IO::SEEK_SET)

  end_mark - start_mark
end

#offsetObject



346
347
348
# File 'lib/bindata/io.rb', line 346

def offset
  @pos
end

#read(n) ⇒ Object



361
362
363
# File 'lib/bindata/io.rb', line 361

def read(n)
  @io.read(n).tap { |data| @pos += (data&.size || 0) }
end

#seek_abs(n) ⇒ Object



356
357
358
359
# File 'lib/bindata/io.rb', line 356

def seek_abs(n)
  @io.seek(n + @initial_pos, ::IO::SEEK_SET)
  @pos = n
end

#seekable?Boolean

Returns:

  • (Boolean)


333
334
335
# File 'lib/bindata/io.rb', line 333

def seekable?
  true
end

#skip(n) ⇒ Object

Raises:

  • (IOError)


350
351
352
353
354
# File 'lib/bindata/io.rb', line 350

def skip(n)
  raise IOError, "can not skip backwards" if n.negative?
  @io.seek(n, ::IO::SEEK_CUR)
  @pos += n
end

#write(data) ⇒ Object



365
366
367
# File 'lib/bindata/io.rb', line 365

def write(data)
  @io.write(data)
end