lib/bson/binary.js
function Binary(buffer, subType) {
  if(!(this instanceof Binary)) return new Binary(buffer, subType);
  
  this._bsontype = 'Binary';

  if(buffer instanceof Number) {
    this.sub_type = buffer;
    this.position = 0;
  } else {    
    this.sub_type = subType == null ? BSON_BINARY_SUBTYPE_DEFAULT : subType;
    this.position = 0;
  }

  if(buffer != null && !(buffer instanceof Number)) {
    // Only accept Buffer, Uint8Array or Arrays
    if(typeof buffer == 'string') {
      // Different ways of writing the length of the string for the different types
      if(typeof Buffer != 'undefined') {
        this.buffer = new Buffer(buffer);
      } else if(typeof Uint8Array != 'undefined' || (Object.prototype.toString.call(buffer) == '[object Array]')) {
        this.buffer = writeStringToArray(buffer);
      } else {
        throw new Error("only String, Buffer, Uint8Array or Array accepted");
      }
    } else {
      this.buffer = buffer;      
    }
    this.position = buffer.length;
  } else {
    if(typeof Buffer != 'undefined') {
      this.buffer =  new Buffer(Binary.BUFFER_SIZE);      
    } else if(typeof Uint8Array != 'undefined'){
      this.buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE));
    } else {
      this.buffer = new Array(Binary.BUFFER_SIZE);
    }
    // Set position to start of buffer
    this.position = 0;
  }
}
function Binary(buffer, subType) {
  if(!(this instanceof Binary)) return new Binary(buffer, subType);
  
  this._bsontype = 'Binary';

  if(buffer instanceof Number) {
    this.sub_type = buffer;
    this.position = 0;
  } else {    
    this.sub_type = subType == null ? BSON_BINARY_SUBTYPE_DEFAULT : subType;
    this.position = 0;
  }

  if(buffer != null && !(buffer instanceof Number)) {
    // Only accept Buffer, Uint8Array or Arrays
    if(typeof buffer == 'string') {
      // Different ways of writing the length of the string for the different types
      if(typeof Buffer != 'undefined') {
        this.buffer = new Buffer(buffer);
      } else if(typeof Uint8Array != 'undefined' || (Object.prototype.toString.call(buffer) == '[object Array]')) {
        this.buffer = writeStringToArray(buffer);
      } else {
        throw new Error("only String, Buffer, Uint8Array or Array accepted");
      }
    } else {
      this.buffer = buffer;      
    }
    this.position = buffer.length;
  } else {
    if(typeof Buffer != 'undefined') {
      this.buffer =  new Buffer(Binary.BUFFER_SIZE);      
    } else if(typeof Uint8Array != 'undefined'){
      this.buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE));
    } else {
      this.buffer = new Array(Binary.BUFFER_SIZE);
    }
    // Set position to start of buffer
    this.position = 0;
  }
}
function Binary ( buffer , subType ) { if ( ! ( this instanceof Binary ) ) return new Binary ( buffer , subType ) ; this . _bsontype = 'Binary' ; if ( buffer instanceof Number ) { this . sub_type = buffer ; this . position = 0 ; } else { this . sub_type = subType == null ? BSON_BINARY_SUBTYPE_DEFAULT : subType ; this . position = 0 ; } if ( buffer != null && ! ( buffer instanceof Number ) ) { // Only accept Buffer, Uint8Array or Arrays if ( typeof buffer == 'string' ) { // Different ways of writing the length of the string for the different types if ( typeof Buffer != 'undefined' ) { this . buffer = new Buffer ( buffer ) ; } else if ( typeof Uint8Array != 'undefined' || ( Object . prototype . toString . call ( buffer ) == '[object Array]' ) ) { this . buffer = writeStringToArray ( buffer ) ; } else { throw new Error ( "only String, Buffer, Uint8Array or Array accepted" ) ; } } else { this . buffer = buffer ; } this . position = buffer . length ; } else { if ( typeof Buffer != 'undefined' ) { this . buffer = new Buffer ( Binary . BUFFER_SIZE ) ; } else if ( typeof Uint8Array != 'undefined' ) { this . buffer = new Uint8Array ( new ArrayBuffer ( Binary . BUFFER_SIZE ) ) ; } else { this . buffer = new Array ( Binary . BUFFER_SIZE ) ; } // Set position to start of buffer this . position = 0 ; } }
A class representation of the BSON Binary type.

Sub types
- **BSON.BSON_BINARY_SUBTYPE_DEFAULT**, default BSON type.
- **BSON.BSON_BINARY_SUBTYPE_FUNCTION**, BSON function type.
- **BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY**, BSON byte array type.
- **BSON.BSON_BINARY_SUBTYPE_UUID**, BSON uuid type.
- **BSON.BSON_BINARY_SUBTYPE_MD5**, BSON md5 type.
- **BSON.BSON_BINARY_SUBTYPE_USER_DEFINED**, BSON user defined type.

@class
@param {Buffer} buffer a buffer object containing the binary data.
@param {Number} [subType] the option binary type.
@return {Binary}
A class representation of the BSON Binary type .