Class: Origami::ObjectStream

Inherits:
Stream
  • Object
show all
Includes:
Enumerable
Defined in:
lib/origami/stream.rb

Overview

Class representing a Stream containing other Objects.

Constant Summary collapse

NUM =

:nodoc:

0
OBJ =

:nodoc:

1

Constants inherited from Stream

Stream::TOKENS

Constants included from StandardObject

StandardObject::DEFAULT_ATTRIBUTES

Constants included from Object

Origami::Object::TOKENS

Instance Attribute Summary

Attributes inherited from Stream

#dictionary

Attributes included from Object

#file_offset, #generation, #no, #objstm_offset, #parent

Instance Method Summary collapse

Methods inherited from Stream

#[], #[]=, #data, #data=, #decode!, #each_key, #encode!, #method_missing, parse, #post_build, #rawdata, #rawdata=, #real_type, #set_predictor, #to_obfuscated_str, #to_s, #value

Methods included from StandardObject

#do_type_check, #has_field?, included, #pdf_version_required, #set_default_value, #set_default_values

Methods included from Object

#<=>, #copy, #indirect_parent, #is_indirect?, parse, #pdf, #pdf_version_required, #post_build, #reference, #set_indirect, #set_pdf, #size, skip_until_next_obj, #solve, #to_o, #to_s, #type, typeof, #xrefs

Constructor Details

#initialize(rawdata = "", dictionary = {}) ⇒ ObjectStream

Creates a new Object Stream.

dictionary

A hash of attributes to set to the Stream.

rawdata

The Stream data.



423
424
425
426
427
# File 'lib/origami/stream.rb', line 423

def initialize(rawdata = "", dictionary = {})
  @objects = nil
 
  super(rawdata, dictionary)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Origami::Stream

Instance Method Details

#<<(object) ⇒ Object Also known as: insert

Adds a new Object to this Stream.

object

The Object to append.



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/origami/stream.rb', line 460

def <<(object)
  unless object.generation == 0
    raise InvalidObjectError, "Cannot store an object with generation > 0 in an ObjectStream"
  end

  if object.is_a?(Stream)
    raise InvalidObjectError, "Cannot store a Stream in an ObjectStream"
  end

  load! if @objects.nil?
  
  object.no, object.generation = @pdf.alloc_new_object_number if object.no == 0
  
  object.set_indirect(true) # object is indirect
  object.parent = self      # set this stream as the parent
  object.set_pdf(@pdf)      # indirect objects need pdf information
  @objects[object.no] = object
 
  Reference.new(object.no, 0)
end

#delete(no) ⇒ Object

Deletes Object no.



485
486
487
488
489
# File 'lib/origami/stream.rb', line 485

def delete(no)
  load! if @objects.nil?

  @objects.delete(no)
end

#each(&b) ⇒ Object

Iterates over each object in the stream.



538
539
540
541
542
# File 'lib/origami/stream.rb', line 538

def each(&b)
  load! if @objects.nil? 
  
  @objects.values.each(&b)
end

#extract(no) ⇒ Object

Returns a given decompressed object contained in the Stream.

no

The Object number.



509
510
511
512
513
# File 'lib/origami/stream.rb', line 509

def extract(no)
  load! if @objects.nil?

  @objects[no]
end

#extract_by_index(index) ⇒ Object

Returns a given decompressed object by index.

index

The Object index in the ObjectStream.



519
520
521
522
523
# File 'lib/origami/stream.rb', line 519

def extract_by_index(index)
  load! if @objects.nil?

  @objects.to_a.sort[index]
end

#include?(no) ⇒ Boolean

Returns whether a specific object is contained in this stream.

no

The Object number.

Returns:



529
530
531
532
533
# File 'lib/origami/stream.rb', line 529

def include?(no)
  load! if @objects.nil?

  @objects.include?(no)
end

#index(no) ⇒ Object

Returns the index of Object no.



494
495
496
497
498
499
500
501
502
503
# File 'lib/origami/stream.rb', line 494

def index(no)
  ind = 0
  @objects.to_a.sort.each { |num, obj|
    return ind if num == no

    ind = ind + 1
  }

  nil
end

#objectsObject

Returns the array of inner objects.



547
548
549
550
551
# File 'lib/origami/stream.rb', line 547

def objects
  load! if @objects.nil?

  @objects.values
end

#pre_buildObject

:nodoc:



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/origami/stream.rb', line 429

def pre_build #:nodoc:
  load! if @objects.nil?

  prolog = ""
  data = ""
  objoff = 0
  @objects.to_a.sort.each do |num,obj|
    
    obj.set_indirect(false)
    obj.objstm_offset = objoff

    prolog << "#{num} #{objoff} "
    objdata = "#{obj.to_s} "
    
    objoff += objdata.size
    data << objdata
    obj.set_indirect(true)
  end
  
  self.data = prolog + data
  
  @dictionary[:N] = @objects.size
  @dictionary[:First] = prolog.size
  
  super
end