func (b *Buffer) Len() int {
	return b.length
}
Len -- returns the buffer length.
package xbase
import (
	"fmt"
)
type Buffer struct {
	seek   int
	length int
	data   []byte
}
func NewBuffer() *Buffer {
	return &Buffer{}
}
func NewBufferReader(data []byte) *Buffer {
	return &Buffer{
		data:   data,
		length: len(data),
	}
}
func (b *Buffer) check(l int) error {
	if (b.seek + l) > b.length {
		return fmt.Errorf("error.seek[%v].required[%v].length[%v]", b.seek, l, b.length)
	}
	return nil
}
func (b *Buffer) WriteU8(v uint8) {
	b.data = append(b.data, v)
	b.length++
}
func (b *Buffer) ReadU8() (uint8, error) {
	if err := b.check(1); err != nil {
		return 0, err
	}
	v := b.data[b.seek]
	b.seek++
	return v, nil
}
func (b *Buffer) WriteU16(v uint32) {
	b.data = append(b.data, byte(v))
	b.data = append(b.data, byte(v>>8))
	b.length += 2
}
func (b *Buffer) ReadU16() (uint32, error) {
	if err := b.check(2); err != nil {
		return 0, err
	}
	v := uint32(b.data[b.seek]) |
		uint32(b.data[b.seek+1])<<8
	b.seek += 2
	return v, nil
}
func (b *Buffer) WriteU32(v uint32) {
	b.data = append(b.data, byte(v))
	b.data = append(b.data, byte(v>>8))
	b.data = append(b.data, byte(v>>16))
	b.data = append(b.data, byte(v>>24))
	b.length += 4
}
func (b *Buffer) ReadU32() (uint32, error) {
	if err := b.check(4); err != nil {
		return 0, err
	}
	v := uint32(b.data[b.seek]) |
		uint32(b.data[b.seek+1])<<8 |
		uint32(b.data[b.seek+2])<<16 |
		uint32(b.data[b.seek+3])<<24
	b.seek += 4
	return v, nil
}
func (b *Buffer) WriteU64(v uint64) {
	b.data = append(b.data, byte(v))
	b.data = append(b.data, byte(v>>8))
	b.data = append(b.data, byte(v>>16))
	b.data = append(b.data, byte(v>>24))
	b.data = append(b.data, byte(v>>32))
	b.data = append(b.data, byte(v>>40))
	b.data = append(b.data, byte(v>>48))
	b.data = append(b.data, byte(v>>56))
	b.length += 8
}
func (b *Buffer) ReadU64() (uint64, error) {
	if err := b.check(8); err != nil {
		return 0, err
	}
	v := uint64(b.data[b.seek]) |
		uint64(b.data[b.seek+1])<<8 |
		uint64(b.data[b.seek+2])<<16 |
		uint64(b.data[b.seek+3])<<24 |
		uint64(b.data[b.seek+4])<<32 |
		uint64(b.data[b.seek+5])<<40 |
		uint64(b.data[b.seek+6])<<48 |
		uint64(b.data[b.seek+7])<<56
	b.seek += 8
	return v, nil
}
func (b *Buffer) WriteVarInt(v uint64) {
	switch {
	case v < uint64(0xfd):
		b.WriteU8(uint8(v))
	case v <= 0xffff:
		b.WriteU8(0xfd)
		b.WriteU16(uint32(v))
	case v <= 0xffffffff:
		b.WriteU8(0xfe)
		b.WriteU32(uint32(v))
	default:
		b.WriteU8(0xff)
		b.WriteU64(v)
	}
}
func (b *Buffer) ReadVarInt() (uint64, error) {
	var v uint64
	first, err := b.ReadU8()
	if err != nil {
		return 0, err
	}
	switch first {
	case 0xfd:
		v1, err := b.ReadU16()
		if err != nil {
			return 0, err
		}
		v = uint64(v1)
	case 0xfe:
		v1, err := b.ReadU32()
		if err != nil {
			return 0, err
		}
		v = uint64(v1)
	case 0xff:
		v1, err := b.ReadU64()
		if err != nil {
			return 0, err
		}
		v = v1
	default:
		v = uint64(first)
	}
	return v, nil
}
func VarIntSerializeSize(val uint64) int {
	if val < 0xfd {
		return 1
	}
	if val <= 0xffff {
		return 3
	}
	if val <= 0xffffffff {
		return 5
	}
	return 9
}
func (b *Buffer) WriteBytes(v []byte) {
	b.data = append(b.data, v...)
	b.length += len(v)
}
func (b *Buffer) ReadBytes(l int) ([]byte, error) {
	if l == 0 {
		return nil, nil
	}
	if err := b.check(l); err != nil {
		return nil, err
	}
	v := make([]byte, l)
	copy(v, b.data[b.seek:b.seek+l])
	b.seek += l
	return v, nil
}
func (b *Buffer) WriteVarBytes(v []byte) {
	len := len(v)
	b.WriteVarInt(uint64(len))
	b.WriteBytes(v)
}
func (b *Buffer) ReadVarBytes() ([]byte, error) {
	len, err := b.ReadVarInt()
	if err != nil {
		return nil, err
	}
	return b.ReadBytes(int(len))
}
func (b *Buffer) WriteVarString(v string) {
	b.WriteVarBytes([]byte(v))
}
func (b *Buffer) ReadVarString() (string, error) {
	data, err := b.ReadVarBytes()
	if err != nil {
		return "", err
	}
	return string(data), nil
}
func (b *Buffer) Bytes() []byte {
	return b.data
}
func (b *Buffer) Remaining() []byte {
	size := b.length - b.seek
	remaining := make([]byte, size)
	copy(remaining[:], b.data[b.seek:b.length])
	return remaining
}
func (b *Buffer) Seek() int {
	return b.seek
}