// Encode writes the packet bytes into the byte slice from the argument. It
// returns the number of bytes encoded and whether there's any errors along
// the way. If there is an error, the byte slice should be considered invalid.
962970
func (cp *Connack) Encode(dst []byte) (int, error) {
	total := 0

	// encode header
	n, err := headerEncode(dst[total:], 0, 2, cp.Len(), CONNACK)
	total += n
	if err != nil {
		return total, err
	}

	// set session present flag
	if cp.SessionPresent {
		dst[total] = 1 // 00000001
	} else {
		dst[total] = 0 // 00000000
	}
	total++

	// check return code
	if !cp.ReturnCode.Valid() {
		return total, makeError(cp.Type(), "invalid return code (%d)", cp.ReturnCode)
	}

	// set return code
	dst[total] = byte(cp.ReturnCode)
	total++

	return total, nil
}
958152
func Encode(buf []byte, code uint64) ([]byte, error) {

	if !ValidCode(code) {
		return nil, ErrUnknownCode
	}

	start := make([]byte, 2*binary.MaxVarintLen64, 2*binary.MaxVarintLen64+len(buf))
	spot := start
	n := binary.PutUvarint(spot, code)
	spot = start[n:]
	n += binary.PutUvarint(spot, uint64(len(buf)))

	return append(start[:n], buf...), nil
}
1322505
func Encode(v interface{}) []byte {
	encoder := encoder{}
	encoder.writeInterfaceType(v)
	return encoder.Bytes()
}
1224457
func (enc *encoder) Encode(vs ...interface{}) (err error) {
	defer func() {
		err, _ = recover().(error)
	}()
	for _, v := range vs {
		enc.encode(reflect.ValueOf(v), 0)
	}
	return nil
}
863515
func (pp *Publish) Encode(dst []byte) (int, error) {
	total := 0

	// check topic length
	if len(pp.Message.Topic) == 0 {
		return total, makeError(pp.Type(), "topic name is empty")
	}

	flags := byte(0)

	// set dup flag
	if pp.Dup {
		flags |= 0x8 // 00001000
	} else {
		flags &= 247 // 11110111
	}

	// set retain flag
	if pp.Message.Retain {
		flags |= 0x1 // 00000001
	} else {
		flags &= 254 // 11111110
	}

	// check qos
	if !pp.Message.QOS.Successful() {
		return 0, makeError(pp.Type(), "invalid QOS level %d", pp.Message.QOS)
	}

	// check packet id
	if pp.Message.QOS > 0 && !pp.ID.Valid() {
		return total, makeError(pp.Type(), "packet id must be grater than zero")
	}

	// set qos
	flags = (flags & 249) | (byte(pp.Message.QOS) << 1) // 249 = 11111001

	// encode header
	n, err := headerEncode(dst[total:], flags, pp.len(), pp.Len(), PUBLISH)
	total += n
	if err != nil {
		return total, err
	}

	// write topic
	n, err = writeLPString(dst[total:], pp.Message.Topic, pp.Type())
	total += n
	if err != nil {
		return total, err
	}

	// write packet id
	if pp.Message.QOS != 0 {
		binary.BigEndian.PutUint16(dst[total:], uint16(pp.ID))
		total += 2
	}

	// write payload
	copy(dst[total:], pp.Message.Payload)
	total += len(pp.Message.Payload)

	return total, nil
}
505536
func encode(b []byte) []byte {
	var buf bytes.Buffer
	mw := meta.NewWriter(&buf)
	mw.FinalMode = meta.FinalMeta
	if _, err := io.Copy(mw, bytes.NewReader(b)); err != nil {
		log.Fatal(err)
	}
	if err := mw.Close(); err != nil {
		log.Fatal(err)
	}
	return buf.Bytes()
}
328542
func (encoder *IntLengthFieldBasedEncoder) Encode(data interface{}, out *ByteBuf) error {
	buf := NewByteBuf(32)

	if encoder.prepare != nil {
		err := encoder.prepare(data, out)
		if err != nil {
			return err
		}
	}

	err := encoder.base.Encode(data, buf)
	if err != nil {
		return err
	}

	out.WriteInt(buf.Readable())
	out.WriteByteBuf(buf)
	return nil
}
866235
func (d *dataEncoder) encode(data []byte) (*bitset.Bitset, error) {
	d.data = data
	d.actual = nil
	d.optimised = nil

	if len(data) == 0 {
		return nil, errors.New("no data to encode")
	}

	// Classify data into unoptimised segments.
	d.classifyDataModes()

	// Optimise segments.
	err := d.optimiseDataModes()
	if err != nil {
		return nil, err
	}

	// Encode data.
	encoded := bitset.New()
	for _, s := range d.optimised {
		d.encodeDataRaw(s.data, s.dataMode, encoded)
	}

	return encoded, nil
}
764093
func (s ISO88595) Encode() []byte {
	e := charmap.ISO8859_5.NewEncoder()
	es, _, err := transform.Bytes(e, s)
	if err != nil {
		return s
	}
	return es
}
1248936
func Encode(value uint64) string {
	var res [16]byte
	var i int
	for i = len(res) - 1; value != 0; i-- {
		res[i] = base36[value%36]
		value /= 36
	}
	return string(res[i+1:])
}
27561
func (r *ServerResponse) Encode(w io.Writer) error {
	if len(r.ACKs) > 1 {
		return errors.New("multi_ack and multi_ack_detailed are not supported")
	}

	e := pktline.NewEncoder(w)
	if len(r.ACKs) == 0 {
		return e.Encodef("%s\n", nak)
	}

	return e.Encodef("%s %s\n", ack, r.ACKs[0].String())
}
908520
func (sp *Subscribe) Encode(dst []byte) (int, error) {
	total := 0

	// check packet id
	if !sp.ID.Valid() {
		return total, makeError(sp.Type(), "packet id must be grater than zero")
	}

	// encode header
	n, err := headerEncode(dst[total:], 0, sp.len(), sp.Len(), SUBSCRIBE)
	total += n
	if err != nil {
		return total, err
	}

	// write packet id
	binary.BigEndian.PutUint16(dst[total:], uint16(sp.ID))
	total += 2

	for _, t := range sp.Subscriptions {
		// write topic
		n, err := writeLPString(dst[total:], t.Topic, sp.Type())
		total += n
		if err != nil {
			return total, err
		}

		// check qos
		if !t.QOS.Successful() {
			return total, makeError(sp.Type(), "invalid QOS level (%d)", t.QOS)
		}

		// write qos
		dst[total] = byte(t.QOS)

		total++
	}

	return total, nil
}
1233717
func Encode(dst, src []byte) int {
	for i, v := range src {
		dst[i*2] = hextable[v>>4]
		dst[i*2+1] = hextable[v&0x0f]
	}

	return len(src) * 2
}
70158
func (mc *MessageCodec) Encode(m Message, buffer []byte) ([]byte, error) {
	pn := mc.pnMessage()
	m.(*message).put(pn)
	encode := func(buf []byte) ([]byte, error) {
		len := cLen(buf)
		result := C.pn_message_encode(pn, cPtr(buf), &len)
		switch {
		case result == C.PN_OVERFLOW:
			return buf, overflow
		case result < 0:
			return buf, fmt.Errorf("cannot encode message: %s", PnErrorCode(result))
		default:
			return buf[:len], nil
		}
	}
	return encodeGrow(buffer, encode)
}
238535
func (enc *Encoder) Encode(v interface{}) (int, error) {
	if v == nil {
		msg := "can't marshal nil interface"
		err := marshalError("Marshal", ErrNilInterface, msg, nil, nil)
		return 0, err
	}

	vv := reflect.ValueOf(v)
	vve := vv
	for vve.Kind() == reflect.Ptr {
		if vve.IsNil() {
			msg := fmt.Sprintf("can't marshal nil pointer '%v'",
				vv.Type().String())
			err := marshalError("Marshal", ErrBadArguments, msg,
				nil, nil)
			return 0, err
		}
		vve = vve.Elem()
	}

	return enc.encode(vve)
}
425650
func (v BytesValue) encode(w io.Writer) error {
	raw := rawValue{
		Type: v.valueType(),
	}

	return raw.encodeBytes(w, []byte(v))
}
495507
func (codec *StringCodec) Encode(data interface{}, out *ByteBuf) error {
	if msg, ok := data.(string); ok {
		return out.WriteString(msg)
	}

	return fmt.Errorf("not string: %+v", data)
}