Example #1
0
// string
func encodeString(v string) (b []byte, err error) {
	var (
		lenB []byte
		sBuf = *bytes.NewBufferString(v)
		rLen = utf8.RuneCountInString(v)

		sChunk = func(_len int) {
			for i := 0; i < _len; i++ {
				if r, s, err := sBuf.ReadRune(); s > 0 && err == nil {
					b = append(b, []byte(string(r))...)
				}
			}
		}
	)

	if v == "" {
		if lenB, err = util.PackUint16(uint16(rLen)); err != nil {
			b = nil
			return
		}
		b = append(b, 'S')
		b = append(b, lenB...)
		b = append(b, []byte{}...)
		return
	}

	for {
		rLen = utf8.RuneCount(sBuf.Bytes())
		if rLen == 0 {
			break
		}
		if rLen > CHUNK_SIZE {
			if lenB, err = util.PackUint16(uint16(CHUNK_SIZE)); err != nil {
				b = nil
				return
			}
			b = append(b, 's')
			b = append(b, lenB...)
			sChunk(CHUNK_SIZE)
		} else {
			if lenB, err = util.PackUint16(uint16(rLen)); err != nil {
				b = nil
				return
			}
			b = append(b, 'S')
			b = append(b, lenB...)
			sChunk(rLen)
		}
	}
	return
}
Example #2
0
// binary
func encodeBinary(v []byte) (b []byte, err error) {
	var (
		tag  byte
		lenB []byte
		lenN int
	)

	if len(v) == 0 {
		if lenB, err = util.PackUint16(0); err != nil {
			b = nil
			return
		}
		b = append(b, 'B')
		b = append(b, lenB...)
		return
	}

	rBuf := *bytes.NewBuffer(v)

	for rBuf.Len() > 0 {
		if rBuf.Len() > CHUNK_SIZE {
			tag = 'b'
			if lenB, err = util.PackUint16(uint16(CHUNK_SIZE)); err != nil {
				b = nil
				return
			}
			lenN = CHUNK_SIZE
		} else {
			tag = 'B'
			if lenB, err = util.PackUint16(uint16(rBuf.Len())); err != nil {
				b = nil
				return
			}
			lenN = rBuf.Len()
		}
		b = append(b, tag)
		b = append(b, lenB...)
		b = append(b, rBuf.Next(lenN)...)
	}
	return
}
Example #3
0
//封装  hessian 请求头
func (h *hessianRequest) packHead(method string) {
	tmp_b, _ := util.PackUint16(uint16(len(method)))
	h.body = append(h.body, []byte{99, 0, 1, 109}...)
	h.body = append(h.body, tmp_b...)
	h.body = append(h.body, []byte(method)...)
}