class HexaPDF::StreamData

Container for stream data that is more complex than a string.

This helper class wraps all information necessary to read stream data by using a Fiber object (see HexaPDF::Filter). The underlying data either comes from an IO object, a file represented by its file name or a Fiber defined via a Proc object.

Additionally, the filter and decode_parms can be set to indicate that the data returned from the Fiber needs to be post-processed. The filter and decode_parms are automatically normalized to arrays on assignment to ease further processing.

Attributes

decode_parms[R]

The decoding parameters associated with the filter(s).

filter[R]

The filter(s) that need to be applied for getting the decoded stream data.

length[R]

The optional number of bytes to use starting from offset.

offset[R]

The optional offset into the bytes provided by source.

source[R]

The source.

Public Class Methods

new(io) → stream_data click to toggle source
new(str) → stream_data
new(proc) → stream_data
new { block } → stream_data

Creates a new StreamData object for the given source and with the given options.

The source can be:

  • An IO stream which is read starting from a specific offset for a specific length

  • A string which is interpreted as a file name and read starting from a specific offset

  • and for a specific length

  • A Proc object (that is converted to a Fiber when needed) in which case the offset and value is ignored. The Proc object can also be passed by using a block.

# File lib/hexapdf/stream.rb, line 76
def initialize(source = nil, offset: nil, length: nil, filter: nil, decode_parms: nil, &block)
  if source.nil? && !block_given?
    raise ArgumentError, "Either a source object or a block must be given"
  end
  @source = source || block
  @offset = offset
  @length = length
  @filter = [filter].flatten.compact
  @decode_parms = [decode_parms].flatten
  freeze
end

Public Instance Methods

==(other) click to toggle source

Returns whether this stream data object is equal to the other stream data object.

# File lib/hexapdf/stream.rb, line 102
def ==(other)
  other.kind_of?(StreamData) &&
    source == other.source && offset == other.offset && length == other.length &&
    filter == other.filter && decode_parms == other.decode_parms
end
fiber(chunk_size = 0) click to toggle source

Returns a Fiber for getting at the data of the stream represented by this object.

# File lib/hexapdf/stream.rb, line 89
def fiber(chunk_size = 0)
  if @source.kind_of?(Proc)
    FiberWithLength.new(@length, &@source)
  elsif @source.kind_of?(String)
    HexaPDF::Filter.source_from_file(@source, pos: @offset || 0, length: @length || -1,
                                     chunk_size: chunk_size)
  else
    HexaPDF::Filter.source_from_io(@source, pos: @offset || 0, length: @length || -1,
                                   chunk_size: chunk_size)
  end
end