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.


522
523
524
525
526
# File 'lib/origami/stream.rb', line 522

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.


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

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.


591
592
593
594
595
# File 'lib/origami/stream.rb', line 591

def delete(no)
    load!

    @objects.delete(no)
end

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

Iterates over each object in the stream.


640
641
642
643
644
# File 'lib/origami/stream.rb', line 640

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.


608
609
610
611
612
# File 'lib/origami/stream.rb', line 608

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)

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

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:


631
632
633
634
635
# File 'lib/origami/stream.rb', line 631

def include?(no)
    load!

    @objects.include?(no)
end

#index(no) ⇒ Object

Returns the index of Object no.


600
601
602
# File 'lib/origami/stream.rb', line 600

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

#lengthObject

Returns the number of objects contained in the stream.


650
651
652
653
654
# File 'lib/origami/stream.rb', line 650

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.


659
660
661
662
663
# File 'lib/origami/stream.rb', line 659

def objects
    load!

    @objects.values
end

#pre_buildObject

:nodoc:


528
529
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
# File 'lib/origami/stream.rb', line 528

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