Encode the binary type

 @example Encode the binary.
   binary.to_bson

 @return [ BSON::ByteBuffer ] The buffer with the encoded object.

 @see http://bsonspec.org/#/specification

 @since 2.0.0
961082
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
11871
def _encode_to_binary_string(message, on, off):
    
    def to_string(i, s):
        if i == 0 and s == off:
            return off * 4
        return s
    return ''.join(to_string(i, s) for i, s in enumerate(_encode_binary(message, on=on, off=off)))
239134
protected ByteBuffer inputStreamToByteBuffer() throws IOException {
        return byteBuffer == null ? byteBuffer = ByteBuffer.wrap(inputStreamToByteArray()) : byteBuffer;
    }
519864
function encodeEmbeddedFileBuffer( encoder, buffer_type, filename, mime_type ) {
	// Get MIME And payload
	var mime = mime_type || mimeTypeFromFilename( filename ),
		mime_id = encoder.stringID( mime ),
		buffer = bufferFromFile( filename );

	// Write buffer header
	encoder.log(LOG.EMB,"file='"+filename+"', mime="+mime+", len="+buffer.length);
	encodeBuffer( encoder, buffer_type, mime_id, buffer );

}
373185
def reverse_byte_nibbles!
    w = ""
    w.force_encoding("BINARY") if w.respond_to?(:force_encoding)
    @bytes.each_byte do |b|
      w << ((b >> 4)|((b&0xF)<<4))
    end
    @bytes = w
    __setobj__ @bytes
    self
  end
809811
function bnToBuffer( bn ) {
  var hex = bn.toString( 16 )
  hex = hex.length % 2 === 1 ? '0' + hex : hex
  return Buffer.from( hex, 'hex' )
}
384745
def decode_buffer(buffer: dict) -> np.ndarray:
    
    buf = np.frombuffer(buffer['data'], dtype=buffer['dtype'])
    return buf.reshape(buffer['shape'])
397273
def encode(self, value):
        '''
        
        '''
        kassert.is_of_types(value, Bits)
        result = BitArray(value)
        result.reverse()
        return result
75157
def _byte_buffer_md5(buffer_):
    
    md5 = hashlib.md5(buffer_)
    byte_digest = md5.digest()
    return base64.b64encode(byte_digest).decode()
975033
def stdout():
    

    # We write all of the data to stdout with bytes, typically io.BytesIO. stdout in Python2
    # accepts bytes but Python3 does not. This is due to a type change on the attribute. To keep
    # this consistent, we leave Python2 the same and get the .buffer attribute on stdout in Python3
    byte_stdout = sys.stdout

    if sys.version_info.major > 2:
        byte_stdout = sys.stdout.buffer  # pylint: disable=no-member

    return byte_stdout
1056412
def encode_contents(self):
        

        header = struct.pack("<LL8sBxxx", self.offset, len(self.raw_data),
                             _create_target(slot=self.slot), self.hardware_type)
        return bytearray(header) + self.raw_data
555526
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
      buffer.put_int64((jd - DATE_REFERENCE) * MILLISECONDS_IN_DAY)
    end
816674
def to_s
      # Encode the fixed header
      header = [
        ((type_id.to_i & 0x0F) << 4) |
          (flags[3] ? 0x8 : 0x0) |
          (flags[2] ? 0x4 : 0x0) |
          (flags[1] ? 0x2 : 0x0) |
          (flags[0] ? 0x1 : 0x0)
      ]

      # Get the packet's variable header and payload
      body = encode_body

      # Check that that packet isn't too big
      body_length = body.bytesize
      if body_length > 268_435_455
        raise 'Error serialising packet: body is more than 256MB'
      end

      # Build up the body length field bytes
      loop do
        digit = (body_length % 128)
        body_length = body_length.div(128)
        # if there are more digits to encode, set the top bit of this digit
        digit |= 0x80 if body_length > 0
        header.push(digit)
        break if body_length <= 0
      end

      # Convert header to binary and add on body
      header.pack('C*') + body
    end
722022
def encode(msg):
    
    msg = bytes(msg)
    return END + msg.replace(ESC, ESC + ESC_ESC).replace(END, ESC + ESC_END) + END
1184118
public ByteBuffer getByteBuffer() {
		final ByteBuffer bb = ByteBuffer.wrap(buf);
		bb.limit(bufLimit);
		bb.position(bufPosition);
		return bb;
	}
1296142
def encode_headers(frame)
      payload = frame[:payload]
      payload = @compressor.encode(payload) unless payload.is_a? Buffer

      frames = []

      while payload.bytesize > 0
        cont = frame.dup
        cont[:type] = :continuation
        cont[:flags] = []
        cont[:payload] = payload.slice!(0, @remote_settings[:settings_max_frame_size])
        frames << cont
      end
      if frames.empty?
        frames = [frame]
      else
        frames.first[:type]  = frame[:type]
        frames.first[:flags] = frame[:flags] - [:end_headers]
        frames.last[:flags] << :end_headers
      end

      frames

    rescue StandardError => e
      connection_error(:compression_error, e: e)
      nil
    end
950185
def encode(self, value):
        '''
        
        '''
        encoded = strToBytes(value) + b'\x00'
        return Bits(bytes=encoded)