Class: PropertyList::BinaryGenerator

Inherits:
Object
  • Object
show all
Includes:
BinaryMarkers
Defined in:
lib/property-list/binary_generator.rb

Overview

Modified from:

https://github.com/jarib/plist/blob/master/lib/plist/binary.rb

With improved performance

Constant Summary

Constants included from BinaryMarkers

PropertyList::BinaryMarkers::MARKER_ARRAY, PropertyList::BinaryMarkers::MARKER_ASCII_STRING, PropertyList::BinaryMarkers::MARKER_DATA, PropertyList::BinaryMarkers::MARKER_DATE, PropertyList::BinaryMarkers::MARKER_DICT, PropertyList::BinaryMarkers::MARKER_FALSE, PropertyList::BinaryMarkers::MARKER_FILL, PropertyList::BinaryMarkers::MARKER_INT, PropertyList::BinaryMarkers::MARKER_NO_BASE_URL, PropertyList::BinaryMarkers::MARKER_NULL, PropertyList::BinaryMarkers::MARKER_ORD_SET, PropertyList::BinaryMarkers::MARKER_REAL, PropertyList::BinaryMarkers::MARKER_SET, PropertyList::BinaryMarkers::MARKER_TRUE, PropertyList::BinaryMarkers::MARKER_UID, PropertyList::BinaryMarkers::MARKER_UTF16BE_STRING, PropertyList::BinaryMarkers::MARKER_UTF8_STRING, PropertyList::BinaryMarkers::MARKER_UUID, PropertyList::BinaryMarkers::MARKER_WITH_BASE_URL, PropertyList::BinaryMarkers::TIME_INTERVAL_SINCE_1970

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ BinaryGenerator

Returns a new instance of BinaryGenerator.



18
19
20
21
# File 'lib/property-list/binary_generator.rb', line 18

def initialize opts
  @output = []
  @offset = 0
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



22
23
24
# File 'lib/property-list/binary_generator.rb', line 22

def output
  @output
end

Instance Method Details

#generate(object) ⇒ Object

Encodes obj as a binary property list. If obj is an Array, Hash, or Set, the property list includes its contents.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/property-list/binary_generator.rb', line 26

def generate object
  flatten_objects = flatten_collection object
  ref_byte_size = min_byte_size flatten_objects.size - 1

  # Write header and encoded objects.
  # TODO use bplist10 when there are version 1x elements
  add_output "bplist00"
  offset_table = []
  flatten_objects.each do |o|
    offset_table << @offset
    binary_object o, ref_byte_size
  end

  # Write offset table.
  offset_table_addr = @offset
  offset_byte_size = min_byte_size @offset
  offset_table.each do |offset|
    binary_integer offset, offset_byte_size
  end

  # Write trailer. (6 + 2 + 24 = 32 bytes)
  add_output [
    "\0\0\0\0\0\0", # padding
    offset_byte_size, ref_byte_size,
    flatten_objects.size,
    0, # index of root object
    offset_table_addr
  ].pack("a*C2Q>3")
end