Class: HexaPDF::StreamData

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/stream.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = nil, offset: nil, length: nil, filter: nil, decode_parms: nil, &block) ⇒ StreamData

:call-seq:

StreamData.new(io)          -> stream_data
StreamData.new(str)         -> stream_data
StreamData.new(proc)        -> stream_data
StreamData.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.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hexapdf/stream.rb', line 80

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

Instance Attribute Details

#decode_parmsObject (readonly)

The decoding parameters associated with the filter(s).



61
62
63
# File 'lib/hexapdf/stream.rb', line 61

def decode_parms
  @decode_parms
end

#filterObject (readonly)

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



58
59
60
# File 'lib/hexapdf/stream.rb', line 58

def filter
  @filter
end

#sourceObject (readonly)

The source.



55
56
57
# File 'lib/hexapdf/stream.rb', line 55

def source
  @source
end

Instance Method Details

#==(other) ⇒ Object

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



108
109
110
111
112
# File 'lib/hexapdf/stream.rb', line 108

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) ⇒ Object

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



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/hexapdf/stream.rb', line 93

def fiber(chunk_size = 0)
  if @source.kind_of?(FiberDoubleForString)
    @source.dup
  elsif @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