class IOP::RandomAccessReader
Abstract reader class for seekable streams which can read with blocks of specified size.
Sequentially reads specified number of bytes starting at specified byte offset.
@since 0.2
Public Class Methods
new(size: nil, offset: nil, block_size: DEFAULT_BLOCK_SIZE)
click to toggle source
Sets up the reader parameters.
@param size [Integer] total number of bytes to read; nil
value instructs to read until end-of-data is reached
@param offset [Integer] offset in bytes from the stream start to seek to; nil
value means no seeking is performed
@param block_size [Integer] size of blocks to read data with
# File lib/iop.rb, line 212 def initialize(size: nil, offset: nil, block_size: DEFAULT_BLOCK_SIZE) @block_size = size.nil? ? block_size : IOP.min(size, block_size) @left = @size = size @offset = offset end
Public Instance Methods
process!()
click to toggle source
# File lib/iop.rb, line 218 def process! seek! unless @offset.nil? buffer = IOP.allocate_string(@block_size) loop do read_size = @size.nil? ? @block_size : IOP.min(@left, @block_size) break if read_size.zero? if (data = read!(read_size, buffer)).nil? if @size.nil? break else raise EOFError, INSUFFICIENT_DATA end else unless @left.nil? @left -= data.size raise IOError, EXTRA_DATA if @left < 0 end end process(data) unless data.size.zero? end process end
Protected Instance Methods
read!(read_size, buffer)
click to toggle source
@abstract
# File lib/iop.rb, line 248 def read!(read_size, buffer) nil end
seek!()
click to toggle source
@abstract
# File lib/iop.rb, line 244 def seek!() end