package spec import ( "bytes" "errors" "io" "git.hpds.cc/Component/mq_coder/encoding" //"mq_coder/encoding" ) // L is the Length in a TLV structure type L struct { buf []byte size int len int } // NewL will take an int type len as parameter and return L to // represent the size of V in a TLV. an integer will be encoded as // a PVarInt32 type to represent the value. func NewL(len int) (L, error) { var l = L{} if len < -1 { return l, errors.New("y3.L: len can't less than -1") } valLen := int32(len) l.size = encoding.SizeOfPVarInt32(valLen) codec := encoding.VarCodec{Size: l.size} tmp := make([]byte, l.size) err := codec.EncodePVarInt32(tmp, valLen) if err != nil { panic(err) } l.buf = make([]byte, l.size) copy(l.buf, tmp) l.len = len return l, nil } // Bytes will return the raw bytes of L. func (l L) Bytes() []byte { return l.buf } // Size returns how many bytes used to represent this L. func (l L) Size() int { return l.size } // VSize returns the size of V. func (l L) VSize() int { return int(l.len) } // ReadL read L from bufio.Reader func ReadL(r io.Reader) (*L, error) { lenBuf := bytes.Buffer{} for { b, err := readByte(r) if err != nil { return nil, err } lenBuf.WriteByte(b) if b&msb != msb { break } } buf := lenBuf.Bytes() // decode to L length, err := decodeL(buf) if err != nil { return nil, err } return &L{ buf: buf, len: int(length), size: len(buf), }, nil } func decodeL(buf []byte) (length int32, err error) { codec := encoding.VarCodec{} err = codec.DecodePVarInt32(buf, &length) return length, err }