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::DEFINED_FILTERS, 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

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

Methods included from TypeGuessing

#guess_type

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, #numbered?, 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.



488
489
490
491
492
# File 'lib/origami/stream.rb', line 488

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.



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/origami/stream.rb', line 526

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.



557
558
559
560
561
# File 'lib/origami/stream.rb', line 557

def delete(no)
    load!

    @objects.delete(no)
end

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

Iterates over each object in the stream.



606
607
608
609
610
# File 'lib/origami/stream.rb', line 606

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.



574
575
576
577
578
# File 'lib/origami/stream.rb', line 574

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)


584
585
586
587
588
589
590
591
# File 'lib/origami/stream.rb', line 584

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:



597
598
599
600
601
# File 'lib/origami/stream.rb', line 597

def include?(no)
    load!

    @objects.include?(no)
end

#index(no) ⇒ Object

Returns the index of Object no.



566
567
568
# File 'lib/origami/stream.rb', line 566

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

#lengthObject

Returns the number of objects contained in the stream.



616
617
618
619
620
# File 'lib/origami/stream.rb', line 616

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.



625
626
627
628
629
# File 'lib/origami/stream.rb', line 625

def objects
    load!

    @objects.values
end

#pre_buildObject

:nodoc:



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/origami/stream.rb', line 494

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