class Origami::ObjectStream

Class representing a Stream containing other Objects.

Public Class Methods

new(raw_data = "", dictionary = {}) click to toggle source

Creates a new Object Stream.

dictionary

A hash of attributes to set to the Stream.

raw_data

The Stream data.

Calls superclass method Origami::Stream::new
# File lib/origami/stream.rb, line 522
def initialize(raw_data = "", dictionary = {})
    super

    @objects = nil
end

Public Instance Methods

<<(object) click to toggle source

Adds a new Object to this Stream.

object

The Object to append.

# 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
Also aliased as: insert
delete(no) click to toggle source

Deletes Object no.

# File lib/origami/stream.rb, line 591
def delete(no)
    load!

    @objects.delete(no)
end
each(&b) click to toggle source

Iterates over each object in the stream.

# File lib/origami/stream.rb, line 640
def each(&b)
    load!

    @objects.values.each(&b)
end
Also aliased as: each_object
each_object(&b)
Alias for: each
extract(no) click to toggle source

Returns a given decompressed object contained in the Stream.

no

The Object number.

# File lib/origami/stream.rb, line 608
def extract(no)
    load!

    @objects[no]
end
extract_by_index(index) click to toggle source

Returns a given decompressed object by index.

index

The Object index in the ObjectStream.

# 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) click to toggle source

Returns whether a specific object is contained in this stream.

no

The Object number.

# File lib/origami/stream.rb, line 631
def include?(no)
    load!

    @objects.include?(no)
end
index(no) click to toggle source

Returns the index of Object no.

# File lib/origami/stream.rb, line 600
def index(no)
    @objects.to_a.sort.index { |num, _| num == no }
end
insert(object)
Alias for: <<
length() click to toggle source

Returns the number of objects contained in the stream.

# 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
objects() click to toggle source

Returns the array of inner objects.

# File lib/origami/stream.rb, line 659
def objects
    load!

    @objects.values
end

Private Instance Methods

import_object_from_document(object) click to toggle source

Preprocess the object in case it already belongs to a document. If the document is the same as the current object stream, remove the duplicate object from our document. If the object comes from another document, use the export method to create a version without references.

# File lib/origami/stream.rb, line 672
def import_object_from_document(object)
    obj_doc = object.document

    # Remove the previous instance if the object is indirect to avoid duplicates.
    if obj_doc.equal?(@document)
        @document.delete_object(object.reference) if object.indirect?

    # Otherwise, create a exported version of the object.
    else
        object = object.export
    end

    object
end