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

#[], #[]=, add_type_signature, #cast_to, #data, #data=, #decode!, #each_filter, #each_key, #encode!, #encoded_data, #encoded_data=, #filters, guess_type, #key?, parse, #post_build, #set_predictor, #to_obfuscated_str, #to_s, #value

Methods included from FieldAccessor

#method_missing, #respond_to_missing?

Methods included from StandardObject

included, #version_required

Methods included from Object

#<=>, #cast_to, #copy, #document, #export, included, #indirect?, #indirect_parent, #logicalize, #logicalize!, #native_type, parse, #post_build, #reference, #set_document, #set_indirect, skip_until_next_obj, #solve, #to_o, #to_s, #type, typeof, #version_required, #xrefs

Constructor Details

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

Creates a new Object Stream.

dictionary

A hash of attributes to set to the Stream.

raw_data

The Stream data.



524
525
526
527
528
# File 'lib/origami/stream.rb', line 524

def initialize(raw_data = "", dictionary = {})
    super

    @objects = nil
end

Dynamic Method Handling

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

Instance Method Details

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

Adds a new Object to this Stream.

object

The Object to append.



562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/origami/stream.rb', line 562

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

    # We must have an associated document to generate new object numbers.
    if @document.nil?
        raise InvalidObjectError, "The ObjectStream must be added to a document before inserting objects"
    end

    # The object already belongs to a document.
    unless object.document.nil?
        object = import_object_from_document(object)
    end

    load!

    object.no, object.generation = @document.allocate_new_object_number if object.no == 0
    store_object(object)

    Reference.new(object.no, 0)
end

#delete(no) ⇒ Object

Deletes Object no.



593
594
595
596
597
# File 'lib/origami/stream.rb', line 593

def delete(no)
    load!

    @objects.delete(no)
end

#each(&b) ⇒ Object Also known as: each_object

Iterates over each object in the stream.



642
643
644
645
646
# File 'lib/origami/stream.rb', line 642

def each(&b)
    load!

    @objects.values.each(&b)
end

#extract(no) ⇒ Object

Returns a given decompressed object contained in the Stream.

no

The Object number.



610
611
612
613
614
# File 'lib/origami/stream.rb', line 610

def extract(no)
    load!

    @objects[no]
end

#extract_by_index(index) ⇒ Object

Returns a given decompressed object by index.

index

The Object index in the ObjectStream.

Raises:

  • (TypeError)


620
621
622
623
624
625
626
627
# File 'lib/origami/stream.rb', line 620

def extract_by_index(index)
    load!

    raise TypeError, "index must be an integer" unless index.is_a?(::Integer)
    raise IndexError, "index #{index} out of range" if index < 0 or index >= @objects.size

    @objects.to_a.sort[index][1]
end

#include?(no) ⇒ Boolean

Returns whether a specific object is contained in this stream.

no

The Object number.

Returns:



633
634
635
636
637
# File 'lib/origami/stream.rb', line 633

def include?(no)
    load!

    @objects.include?(no)
end

#index(no) ⇒ Object

Returns the index of Object no.



602
603
604
# File 'lib/origami/stream.rb', line 602

def index(no)
    @objects.to_a.sort.index { |num, _| num == no }
end

#lengthObject

Returns the number of objects contained in the stream.



652
653
654
655
656
# File 'lib/origami/stream.rb', line 652

def length
    raise InvalidObjectStreamObjectError, "Invalid number of objects" unless self.N.is_a?(Integer)

    self.N.to_i
end

#objectsObject

Returns the array of inner objects.



661
662
663
664
665
# File 'lib/origami/stream.rb', line 661

def objects
    load!

    @objects.values
end

#pre_buildObject

:nodoc:



530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/origami/stream.rb', line 530

def pre_build #:nodoc:
    load!

    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} "

        objoff += objdata.size
        data << objdata
        obj.set_indirect(true)
        obj.no = num
    end

    self.data = prolog + data

    @dictionary[:N] = @objects.size
    @dictionary[:First] = prolog.size

    super
end