Module: BinData::IO::Common::SeekableStream

Defined in:
lib/bindata/io.rb

Overview

Use #seek and #pos on seekable streams

Instance Method Summary collapse

Instance Method Details

#num_bytes_remainingObject

The number of bytes remaining in the input stream.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bindata/io.rb', line 71

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

  if @buffer_end_points
    if @buffer_end_points[1] < end_mark
      end_mark = @buffer_end_points[1]
    end
  end

  bytes_remaining = end_mark - start_mark
  @raw_io.seek(start_mark, ::IO::SEEK_SET)

  bytes_remaining
end

#with_readaheadObject

All io calls in block are rolled back after this method completes.



90
91
92
93
94
95
96
97
# File 'lib/bindata/io.rb', line 90

def with_readahead
  mark = @raw_io.pos
  begin
    yield
  ensure
    @raw_io.seek(mark, ::IO::SEEK_SET)
  end
end