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.



490
491
492
493
494
# File 'lib/origami/stream.rb', line 490

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.



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

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.



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

def delete(no)
  load!

  @objects.delete(no)
end

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

Iterates over each object in the stream.



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

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.



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

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)


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

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) || (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:



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

def include?(no)
  load!

  @objects.include?(no)
end

#index(no) ⇒ Object

Returns the index of Object no.



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

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

#lengthObject

Returns the number of objects contained in the stream.



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

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.



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

def objects
  load!

  @objects.values
end

#pre_buildObject

:nodoc:



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

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