Class: BSON::Binary

Inherits:
Object
  • Object
show all
Includes:
JSON
Defined in:
lib/bson/binary.rb

Overview

Represents binary data.

See Also:

Since:

  • 2.0.0

Defined Under Namespace

Classes: InvalidType

Constant Summary collapse

BSON_TYPE =

A binary is type 0x05 in the BSON spec.

Since:

  • 2.0.0

5.chr.force_encoding(BINARY).freeze
SUBTYPES =

The mappings of subtypes to their single byte identifiers.

Since:

  • 2.0.0

{
  :generic => 0.chr,
  :function => 1.chr,
  :old =>  2.chr,
  :uuid_old => 3.chr,
  :uuid => 4.chr,
  :md5 => 5.chr,
  :user => 128.chr
}.freeze
TYPES =

The mappings of single byte subtypes to their symbol counterparts.

Since:

  • 2.0.0

SUBTYPES.invert.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JSON

#to_json

Constructor Details

#initialize(data = "", type = :generic) ⇒ Binary

Instantiate the new binary object.

Examples:

Instantiate a binary.

BSON::Binary.new(data, :md5)

Parameters:

  • data (Object) (defaults to: "")

    The raw binary data.

  • type (Symbol) (defaults to: :generic)

    The binary type.

Since:

  • 2.0.0


106
107
108
109
110
# File 'lib/bson/binary.rb', line 106

def initialize(data = "", type = :generic)
  validate_type!(type)
  @data = data
  @type = type
end

Instance Attribute Details

#dataObject

Returns The raw binary data.

Returns:

  • (Object)

    The raw binary data.

Since:

  • 2.0.0


56
57
58
# File 'lib/bson/binary.rb', line 56

def data
  @data
end

#typeObject

Since:

  • 2.0.0


56
# File 'lib/bson/binary.rb', line 56

attr_reader :data, :type

Class Method Details

.from_bson(buffer) ⇒ Binary

Deserialize the binary data from BSON.

Parameters:

  • buffer (ByteBuffer)

    The byte buffer.

Returns:

  • (Binary)

    The decoded binary data.

See Also:

Since:

  • 2.0.0


152
153
154
155
156
157
158
# File 'lib/bson/binary.rb', line 152

def self.from_bson(buffer)
  length = buffer.get_int32
  type = TYPES[buffer.get_byte]
  length = buffer.get_int32 if type == :old
  data = buffer.get_bytes(length)
  new(data, type)
end

Instance Method Details

#==(other) ⇒ true, false Also known as: eql?

Determine if this binary object is equal to another object.

Examples:

Check the binary equality.

binary == other

Parameters:

  • other (Object)

    The object to compare against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.0.0


68
69
70
71
# File 'lib/bson/binary.rb', line 68

def ==(other)
  return false unless other.is_a?(Binary)
  type == other.type && data == other.data
end

#as_json(*args) ⇒ Hash

Get the binary as JSON hash data.

Examples:

Get the binary as a JSON hash.

binary.as_json

Returns:

  • (Hash)

    The binary as a JSON hash.

Since:

  • 2.0.0


93
94
95
# File 'lib/bson/binary.rb', line 93

def as_json(*args)
  { "$binary" => Base64.encode64(data), "$type" => type }
end

#hashFixnum

Generates a Fixnum hash value for this object.

Allows using Binary as hash keys.

Returns:

  • (Fixnum)

Since:

  • 2.3.1


81
82
83
# File 'lib/bson/binary.rb', line 81

def hash
  data.hash + type.hash
end

#inspectString

Get a nice string for use with object inspection.

Examples:

Inspect the binary.

object_id.inspect

Returns:

  • (String)

    The binary in form BSON::Binary:object_id

Since:

  • 2.3.0


120
121
122
# File 'lib/bson/binary.rb', line 120

def inspect
  "<BSON::Binary:0x#{object_id} type=#{type} data=0x#{data[0, 8].unpack('H*').first}...>"
end

#to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ BSON::ByteBuffer

Encode the binary type

Examples:

Encode the binary.

binary.to_bson

Returns:

  • (BSON::ByteBuffer)

    The buffer with the encoded object.

See Also:

Since:

  • 2.0.0


134
135
136
137
138
139
140
141
# File 'lib/bson/binary.rb', line 134

def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
  position = buffer.length
  buffer.put_int32(0)
  buffer.put_byte(SUBTYPES[type])
  buffer.put_int32(data.bytesize) if type == :old
  buffer.put_bytes(data.force_encoding(BINARY))
  buffer.replace_int32(position, buffer.length - position - 5)
end