Class: IOP::RandomAccessReader
Overview
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.
Direct Known Subclasses
Instance Attribute Summary
Attributes included from Feed
Instance Method Summary collapse
-
#initialize(size: nil, offset: nil, block_size: DEFAULT_BLOCK_SIZE) ⇒ RandomAccessReader
constructor
Sets up the reader parameters.
- #process! ⇒ Object
- #read!(read_size, buffer) ⇒ Object protected abstract
- #seek! ⇒ Object protected abstract
Methods included from Feed
Constructor Details
#initialize(size: nil, offset: nil, block_size: DEFAULT_BLOCK_SIZE) ⇒ RandomAccessReader
Sets up the reader parameters.
212 213 214 215 216 |
# 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 |
Instance Method Details
#process! ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# 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 |
#read!(read_size, buffer) ⇒ Object (protected)
This method is abstract.
248 |
# File 'lib/iop.rb', line 248 def read!(read_size, buffer) nil end |
#seek! ⇒ Object (protected)
This method is abstract.
244 |
# File 'lib/iop.rb', line 244 def seek!() end |