Class: Pdfrb::Model::Cos::Stream

Inherits:
Dictionary show all
Defined in:
lib/pdfrb/model/cos/stream.rb

Overview

A Dictionary with an associated raw byte payload (s7.3.8.1). Filters (s7.4) are applied on read via decoded_stream and on write via encoded_stream=.

Instance Attribute Summary collapse

Attributes inherited from Object

#document, #gen, #oid, #value

Instance Method Summary collapse

Methods inherited from Dictionary

#[], #[]=, arlington_available?, arlington_default, arlington_key_to_symbol, arlington_loaded_for?, arlington_mark_loaded, arlington_object, arlington_one_type_to_ruby, arlington_required?, arlington_types_to_ruby, arlington_version, define_field, define_field_from_arlington, #delete, #each, each_field, #each_raw, #empty?, field, #key?, #keys, lookup_type, own_fields, #pdf_type, register_type, type_map, #validate

Methods inherited from Object

#==, define_type, #deref, #indirect?, #must_be_indirect?, #pdf_type, pdf_type

Constructor Details

#initialize(value = {}, stream: "".b, oid: 0, gen: 0, document: nil) ⇒ Stream

Returns a new instance of Stream.



10
11
12
13
# File 'lib/pdfrb/model/cos/stream.rb', line 10

def initialize(value = {}, stream: "".b, oid: 0, gen: 0, document: nil)
  super(value, oid: oid, gen: gen, document: document)
  @stream = stream.to_s.dup.force_encoding(Encoding::BINARY)
end

Instance Attribute Details

#streamObject

Returns the value of attribute stream.



15
16
17
# File 'lib/pdfrb/model/cos/stream.rb', line 15

def stream
  @stream
end

Instance Method Details

#decoded_streamObject

Apply /Filter pipeline (if any) to decode the raw stream. Returns raw bytes when no filter or /Filter /Identity.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pdfrb/model/cos/stream.rb', line 24

def decoded_stream
  filter = raw_filter
  return @stream if filter.nil? || filter == :Identity

  unless Pdfrb.const_defined?(:Filter)
    raise Pdfrb::FilterError, "Filter layer not loaded"
  end

  Pdfrb::Filter.apply(
    @stream,
    filters: as_filter_list(filter),
    parms: as_parms_list(raw_decode_parms),
    direction: :decode,
    document: document
  )
end

#encoded_stream=(bytes) ⇒ Object

Apply /Filter pipeline in reverse to encode bytes. Sets /Length to the encoded byte count.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pdfrb/model/cos/stream.rb', line 43

def encoded_stream=(bytes)
  filter = raw_filter
  if filter.nil?
    @stream = bytes.to_s.dup.force_encoding(Encoding::BINARY)
  else
    @stream = Pdfrb::Filter.apply(
      bytes,
      filters: as_filter_list(filter),
      parms: as_parms_list(raw_decode_parms),
      direction: :encode,
      document: document
    )
  end
  @value[:Length] = @stream.bytesize
end