class BSON::Binary
Represents binary data.
@see bsonspec.org/#/specification
@since 2.0.0
Constants
- BSON_TYPE
A binary is type 0x05 in the
BSON
spec.@since 2.0.0
- SUBTYPES
The mappings of subtypes to their single byte identifiers.
@since 2.0.0
- TYPES
The mappings of single byte subtypes to their symbol counterparts.
@since 2.0.0
Attributes
@!attribute data
@return [ Object ] The raw binary data. @since 2.0.0
@!attribute type
@return [ Symbol ] The binary type. @since 2.0.0
@!attribute data
@return [ Object ] The raw binary data. @since 2.0.0
@!attribute type
@return [ Symbol ] The binary type. @since 2.0.0
Public Class Methods
Deserialize the binary data from BSON
.
@param [ ByteBuffer
] buffer The byte buffer.
@return [ Binary
] The decoded binary data.
@see bsonspec.org/#/specification
@since 2.0.0
# 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
Instantiate the new binary object.
@example Instantiate a binary.
BSON::Binary.new(data, :md5)
@param [ Object
] data The raw binary data. @param [ Symbol
] type The binary type.
@since 2.0.0
# File lib/bson/binary.rb, line 106 def initialize(data = "", type = :generic) validate_type!(type) @data = data @type = type end
Public Instance Methods
Determine if this binary object is equal to another object.
@example Check the binary equality.
binary == other
@param [ Object
] other The object to compare against.
@return [ true, false ] If the objects are equal.
@since 2.0.0
# File lib/bson/binary.rb, line 68 def ==(other) return false unless other.is_a?(Binary) type == other.type && data == other.data end
Generates a Fixnum hash value for this object.
Allows using Binary
as hash keys.
@return [ Fixnum ]
@since 2.3.1
# File lib/bson/binary.rb, line 81 def hash data.hash + type.hash end
Get a nice string for use with object inspection.
@example Inspect the binary.
object_id.inspect
@return [ String
] The binary in form BSON::Binary:object_id
@since 2.3.0
# 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
Encode the binary type
@example Encode the binary.
binary.to_bson
@return [ BSON::ByteBuffer
] The buffer with the encoded object.
@see bsonspec.org/#/specification
@since 2.0.0
# 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
Private Instance Methods
Validate the provided type is a valid type.
@api private
@example Validate the type.
binary.validate_type!(:user)
@param [ Object
] type The provided type.
@raise [ InvalidType
] The the type is invalid.
@since 2.0.0
# File lib/bson/binary.rb, line 210 def validate_type!(type) raise InvalidType.new(type) unless SUBTYPES.has_key?(type) end