Class: IOP::RandomAccessReader

Inherits:
Object
  • Object
show all
Includes:
Feed
Defined in:
lib/iop.rb

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.

Since:

  • 0.2

Direct Known Subclasses

IOReader, SFTPFileReader

Instance Attribute Summary

Attributes included from Feed

#downstream

Instance Method Summary collapse

Methods included from Feed

#process, #|

Constructor Details

#initialize(size: nil, offset: nil, block_size: DEFAULT_BLOCK_SIZE) ⇒ RandomAccessReader

Sets up the reader parameters.

Parameters:

  • size (Integer) (defaults to: nil)

    total number of bytes to read; +nil+ value instructs to read until end-of-data is reached

  • offset (Integer) (defaults to: nil)

    offset in bytes from the stream start to seek to; +nil+ value means no seeking is performed

  • block_size (Integer) (defaults to: DEFAULT_BLOCK_SIZE)

    size of blocks to read data with

Since:

  • 0.2



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

Since:

  • 0.2



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.

Since:

  • 0.2



248
# File 'lib/iop.rb', line 248

def read!(read_size, buffer) nil end

#seek!Object (protected)

This method is abstract.

Since:

  • 0.2



244
# File 'lib/iop.rb', line 244

def seek!() end