module BinData::IO::Common::UnSeekableStream
Manually keep track of offset for unseekable streams.
Public Instance Methods
num_bytes_remaining()
click to toggle source
The number of bytes remaining in the input stream.
# File lib/bindata/io.rb, line 130 def num_bytes_remaining raise IOError, "stream is unseekable" end
offset_raw()
click to toggle source
# File lib/bindata/io.rb, line 125 def offset_raw @offset end
with_readahead() { || ... }
click to toggle source
All io calls in block
are rolled back after this method completes.
# File lib/bindata/io.rb, line 136 def with_readahead mark = @offset @read_data = "" @in_readahead = true class << self alias_method :read_raw_without_readahead, :read_raw alias_method :read_raw, :read_raw_with_readahead end begin yield ensure @offset = mark @in_readahead = false end end
Private Instance Methods
read_raw(n)
click to toggle source
# File lib/bindata/io.rb, line 161 def read_raw(n) data = @raw_io.read(n) @offset += data.size if data data end
read_raw_with_readahead(n)
click to toggle source
# File lib/bindata/io.rb, line 167 def read_raw_with_readahead(n) data = "" unless @read_data.empty? || @in_readahead bytes_to_consume = [n, @read_data.length].min data << @read_data.slice!(0, bytes_to_consume) n -= bytes_to_consume if @read_data.empty? class << self alias_method :read_raw, :read_raw_without_readahead end end end raw_data = @raw_io.read(n) data << raw_data if raw_data if @in_readahead @read_data << data end @offset += data.size data end
seek_raw(n)
click to toggle source
# File lib/bindata/io.rb, line 199 def seek_raw(n) raise IOError, "stream is unseekable" if n < 0 # NOTE: how do we seek on a writable stream? # skip over data in 8k blocks while n > 0 bytes_to_read = [n, 8192].min read_raw(bytes_to_read) n -= bytes_to_read end end
stream_init()
click to toggle source
# File lib/bindata/io.rb, line 157 def stream_init @offset = 0 end
write_raw(data)
click to toggle source
# File lib/bindata/io.rb, line 194 def write_raw(data) @offset += data.size @raw_io.write(data) end