本文整理汇总了Golang中encoding/binary.ReadUvarint函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadUvarint函数的具体用法?Golang ReadUvarint怎么用?Golang ReadUvarint使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadUvarint函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: ParseKey
func (sr *StoredRow) ParseKey(key []byte) error {
buf := bytes.NewBuffer(key)
_, err := buf.ReadByte() // type
if err != nil {
return err
}
sr.docID, err = buf.ReadBytes(ByteSeparator)
if len(sr.docID) < 2 { // 1 for min doc id length, 1 for separator
err = fmt.Errorf("invalid doc length 0")
return err
}
sr.docID = sr.docID[:len(sr.docID)-1] // trim off separator byte
sr.docNum, err = binary.ReadUvarint(buf)
if err != nil {
return err
}
err = binary.Read(buf, binary.LittleEndian, &sr.field)
if err != nil {
return err
}
sr.arrayPositions = make([]uint64, 0)
nextArrayPos, err := binary.ReadUvarint(buf)
for err == nil {
sr.arrayPositions = append(sr.arrayPositions, nextArrayPos)
nextArrayPos, err = binary.ReadUvarint(buf)
}
return nil
}
开发者ID:mitra-varuna,项目名称:bleve,代码行数:34,代码来源:stored.go
示例2: NewStoredRowK
func NewStoredRowK(key []byte) (*StoredRow, error) {
rv := StoredRow{}
buf := bytes.NewBuffer(key)
_, err := buf.ReadByte() // type
if err != nil {
return nil, err
}
rv.doc, err = buf.ReadBytes(ByteSeparator)
if len(rv.doc) < 2 { // 1 for min doc id length, 1 for separator
err = fmt.Errorf("invalid doc length 0")
return nil, err
}
rv.doc = rv.doc[:len(rv.doc)-1] // trim off separator byte
err = binary.Read(buf, binary.LittleEndian, &rv.field)
if err != nil {
return nil, err
}
rv.arrayPositions = make([]uint64, 0)
nextArrayPos, err := binary.ReadUvarint(buf)
for err == nil {
rv.arrayPositions = append(rv.arrayPositions, nextArrayPos)
nextArrayPos, err = binary.ReadUvarint(buf)
}
return &rv, nil
}
开发者ID:vseledkin,项目名称:bleve,代码行数:30,代码来源:row.go
示例3: recv
func (p *Player) recv() {
for {
length, err := ReadVarint(p.conn)
if err != nil {
p.Game.RemovePlayer(p.Name)
fmt.Println("Player disconnected")
return
}
buf := make([]byte, length)
io.ReadFull(p.conn, buf)
r := bytes.NewReader(buf)
binary.ReadUvarint(r)
id, _ := binary.ReadUvarint(r)
switch id {
case 0x01:
message := ReadString(r)
if strings.HasPrefix(message, "/") {
// Commands
} else {
WriteChatMessage(p.Game, fmt.Sprintf("<%s> %s", p.Name, message))
fmt.Printf("<%s> %s\n", p.Name, message)
}
}
}
}
开发者ID:cengizIO,项目名称:GoCraft,代码行数:30,代码来源:player.go
示例4: readEntry
func readEntry(reader *dirIndexReader, offset uint64) (*indexEntry, error) {
seekErr := seekIndex(reader, offset)
if seekErr != nil {
return nil, seekErr
}
n, err := binary.ReadUvarint(reader.indexReader)
if err != nil {
return nil, err
}
entries := []indexEntryElement{}
for i := uint64(0); i < n; i++ {
docId, derr := binary.ReadUvarint(reader.indexReader)
if derr != nil {
return nil, derr
}
var freq float64
ferr := binary.Read(reader.indexReader, binary.LittleEndian, &freq)
if ferr != nil {
return nil, ferr
}
entries = append(entries, indexEntryElement{docId, freq})
}
tail, terr := binary.ReadUvarint(reader.indexReader)
if terr != nil {
return nil, terr
}
return &indexEntry{entries, tail}, nil
}
开发者ID:vspy,项目名称:gosearch,代码行数:32,代码来源:search.go
示例5: UnserializeSimpleTask
// Format of a simple task:
// 2 bytes : magic number
// 8 bytes : id length : l
// 8 bytes : data length : n
// 32 bytes : target
// 32 bytes : action
// l bytes : id
// n bytes : data
func UnserializeSimpleTask(data []byte) *SimpleTask {
// Magic number 01
if data[0] != 0 || data[1] != 1 {
return nil
}
// ID Length
idLength, err := binary.ReadUvarint(bytes.NewBuffer(data[2:10]))
if err != nil {
return nil
}
// Data Length
length, err := binary.ReadUvarint(bytes.NewBuffer(data[10:18]))
if err != nil {
return nil
}
// Target
target := string(data[18:50])
// Action
action := string(data[50:82])
// ID
readId := string(data[82 : 82+idLength])
// Data
readData := data[82+idLength : 82+idLength+length]
return NewSimpleTask(readId, target, action, readData)
}
开发者ID:remeh,项目名称:gost,代码行数:41,代码来源:task.go
示例6: readUvarint
func (bin *mBinaryProto) readUvarint(reader io.Reader) (uint64, error) {
if br, ok := reader.(io.ByteReader); ok {
return binary.ReadUvarint(br)
}
br := newByteReader(reader, bin.readBuffer)
return binary.ReadUvarint(br)
}
开发者ID:msglib,项目名称:msglib-go,代码行数:7,代码来源:msglib.go
示例7: ReadHandshake
func ReadHandshake(r *bytes.Reader) Handshake {
handshake := Handshake{}
handshake.Version, _ = binary.ReadUvarint(r)
handshake.Address = ReadString(r)
binary.Read(r, binary.BigEndian, &handshake.Port)
handshake.State, _ = binary.ReadUvarint(r)
return handshake
}
开发者ID:cengizIO,项目名称:GoCraft,代码行数:9,代码来源:protocol.go
示例8: readMsgDial
func readMsgDial(r io.ByteReader) (*msgDial, error) {
id, err := binary.ReadUvarint(r)
if err != nil {
return nil, err
}
seqno, err := binary.ReadUvarint(r)
if err != nil {
return nil, err
}
return &msgDial{ID: chainID(id), SeqNo: SeqNo(seqno)}, nil
}
开发者ID:prodigeni,项目名称:circuit,代码行数:11,代码来源:msg.go
示例9: ReadDHCPOptions
func ReadDHCPOptions(payload []byte) ([]DHCPOption, error) {
magicCookieStart := 236
magicCookieEnd := 240
if len(payload) < magicCookieEnd {
return []DHCPOption{}, PayloadError
}
magicCookie := []byte{byte(99), byte(130), byte(83), byte(99)}
if !bytes.Equal(payload[magicCookieStart:magicCookieEnd], magicCookie) {
return []DHCPOption{}, errors.New("Cannot find magic cookie")
}
opts := []DHCPOption{}
pR := bytes.NewReader(payload[magicCookieEnd:])
// End Loop when only 255 end DHCP option is left
for {
buf := make([]byte, 1)
if _, err := pR.Read(buf); err != nil {
return []DHCPOption{}, err
}
// break if the end is reached
if bytes.Equal(buf, []byte{byte(255)}) {
break
}
code, err := binary.ReadUvarint(bytes.NewReader(buf))
if err != nil {
return []DHCPOption{}, err
}
buf = make([]byte, 1)
if _, err := pR.Read(buf); err != nil {
return []DHCPOption{}, err
}
size, err := binary.ReadUvarint(bytes.NewReader(buf))
if err != nil {
return []DHCPOption{}, err
}
buf = make([]byte, size)
if _, err := pR.Read(buf); err != nil {
return []DHCPOption{}, err
}
opts = append(opts, DHCPOption{code, buf, size})
}
return opts, nil
}
开发者ID:rrawrriw,项目名称:ite,代码行数:54,代码来源:client.go
示例10: next
func (r *restartRange) next() (err error) {
if r.cached && len(r.cache) > r.pos {
r.kv = r.cache[r.pos]
r.pos++
return
}
if r.buf.Len() == 0 {
return io.EOF
}
var nkey []byte
// Read header
var shared, nonShared, valueLen uint64
shared, err = binary.ReadUvarint(r.buf)
if err != nil || shared > uint64(len(r.kv.key)) {
goto corrupt
}
nonShared, err = binary.ReadUvarint(r.buf)
if err != nil {
goto corrupt
}
valueLen, err = binary.ReadUvarint(r.buf)
if err != nil {
goto corrupt
}
if nonShared+valueLen > uint64(r.buf.Len()) {
goto corrupt
}
if r.cached && r.pos > 0 {
r.cache = append(r.cache, r.kv)
}
// Read content
nkey = r.buf.Next(int(nonShared))
if shared == 0 {
r.kv.key = nkey
} else {
pkey := r.kv.key[:shared]
key := make([]byte, shared+nonShared)
copy(key, pkey)
copy(key[shared:], nkey)
r.kv.key = key
}
r.kv.value = r.buf.Next(int(valueLen))
r.pos++
return
corrupt:
return errors.ErrCorrupt("bad entry in block")
}
开发者ID:yufeng108,项目名称:goleveldb,代码行数:53,代码来源:reader.go
示例11: readHeader
func (conn *conn) readHeader() (uint64, uint64, error) {
seq, err := binary.ReadUvarint(conn.buf)
if err != nil {
return 0, 0, err
}
size, err := binary.ReadUvarint(conn.buf)
if err != nil {
return 0, 0, err
}
return seq, size, nil
}
开发者ID:jimenezrick,项目名称:goa,代码行数:13,代码来源:conn.go
示例12: readUint64
func readUint64(r Reader) uint64 {
x, err := binary.ReadUvarint(r)
if err != nil {
log.Fatal(err)
}
return x
}
开发者ID:lamproae,项目名称:hprof,代码行数:7,代码来源:parser.go
示例13: decodeUint
func decodeUint(buf *bytes.Reader) (uint32, error) {
v, err := binary.ReadUvarint(buf)
if v > 0xffffffff {
return 0, errors.New("Something wrong, this number looks too big")
}
return uint32(v), err
}
开发者ID:caio,项目名称:go-tdigest,代码行数:7,代码来源:serialization.go
示例14: NewStreamReader
// NewStreamReader returns a reader that will decode the supplied data stream.
//
// This is compatible content from the NewStreamWriter function.
//
// When you are done with the Reader, use Close to release resources.
func NewStreamReader(in io.Reader) (Reader, error) {
f := &streamReader{
ready: make(chan *rblock, 8), // Read up to 8 blocks ahead
closeReader: make(chan struct{}, 0),
readerClosed: make(chan struct{}, 0),
curBlock: 0,
}
br := bufio.NewReader(in)
format, err := binary.ReadUvarint(br)
if err != nil {
return nil, err
}
switch format {
case 2:
err = f.readFormat2(br)
if err != nil {
return nil, err
}
default:
return nil, ErrUnknownFormat
}
go f.streamReader(br)
return f, nil
}
开发者ID:roger2000hk,项目名称:dedup,代码行数:32,代码来源:reader.go
示例15: NewSeekReader
// NewSeekRead returns a reader that will decode the supplied index and data stream.
//
// This is compatible content from the NewWriter function.
//
// No blocks will be kept in memory, but the block data input must be seekable.
// The function will decode the index before returning.
//
// When you are done with the Reader, use Close to release resources.
func NewSeekReader(index io.Reader, blocks io.ReadSeeker) (IndexedReader, error) {
f := &reader{streamReader: streamReader{
ready: make(chan *rblock, 8), // Read up to 8 blocks ahead
closeReader: make(chan struct{}, 0),
readerClosed: make(chan struct{}, 0),
curBlock: 0,
maxLength: 8, // We have 8 blocks readahead.
}}
idx := bufio.NewReader(index)
format, err := binary.ReadUvarint(idx)
if err != nil {
return nil, err
}
switch format {
case 1:
err = f.readFormat1(idx)
default:
err = ErrUnknownFormat
}
go f.seekReader(blocks)
return f, err
}
开发者ID:roger2000hk,项目名称:dedup,代码行数:33,代码来源:reader.go
示例16: ReadMessage
// Read a message from io.ByteReader by first reading a varint size,
// and then reading and decoding the message object.
// If buf is not big enough a new buffer will be allocated to replace buf.
func ReadMessage(buf *[]byte, r ByteReadReader, msg proto.Message) error {
size, err := binary.ReadUvarint(r)
if err != nil {
return err
}
if cap(*buf) < int(size) {
*buf = make([]byte, size)
}
b := (*buf)[:size]
read := uint64(0)
for read != size {
n, err := r.Read(b[read:])
if err == io.EOF {
return fmt.Errorf("unexpected EOF, expected %d more bytes", size)
}
if err != nil {
return err
}
read += uint64(n)
}
err = proto.Unmarshal(b, msg)
if err != nil {
return err
}
return nil
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:30,代码来源:io.go
示例17: recvProto
func (c *baseConn) recvProto(m proto.Message,
uncompressedSize uint32, decompressor decompressFunc) error {
size, err := binary.ReadUvarint(c.r)
if err != nil {
return err
}
if size == 0 {
return nil
}
if c.r.Buffered() >= int(size) {
// Parse proto directly from the buffered data.
data, err := c.r.Peek(int(size))
if err != nil {
return err
}
if err := decompressor(data, uncompressedSize, m); err != nil {
return err
}
// TODO(pmattis): This is a hack to advance the bufio pointer by
// reading into the same slice that bufio.Reader.Peek
// returned. In Go 1.5 we'll be able to use
// bufio.Reader.Discard.
_, err = io.ReadFull(c.r, data)
return err
}
data := make([]byte, size)
if _, err := io.ReadFull(c.r, data); err != nil {
return err
}
return decompressor(data, uncompressedSize, m)
}
开发者ID:kangxinrong,项目名称:cockroach,代码行数:32,代码来源:conn.go
示例18: ReadNextToken
func ReadNextToken(rw io.ReadWriter) (string, error) {
br := &byteReader{rw}
length, err := binary.ReadUvarint(br)
if err != nil {
return "", err
}
if length > 64*1024 {
err := delimWrite(rw, []byte("messages over 64k are not allowed"))
if err != nil {
return "", err
}
return "", ErrTooLarge
}
buf := make([]byte, length)
_, err = io.ReadFull(rw, buf)
if err != nil {
return "", err
}
if len(buf) == 0 || buf[length-1] != '\n' {
return "", errors.New("message did not have trailing newline")
}
// slice off the trailing newline
buf = buf[:length-1]
return string(buf), nil
}
开发者ID:noffle,项目名称:go-ipfs,代码行数:30,代码来源:multistream.go
示例19: decodeArrayish
func (t *Transport) decodeArrayish(r reader, val reflect.Value) error {
usize, err := binary.ReadUvarint(r)
if err != nil {
return err
}
size := int(usize)
// Special cases: []byte, string
isByteArr := val.Kind() == reflect.Array && val.Type().Elem().Kind() == reflect.Uint8
isString := val.Kind() == reflect.String
if isByteArr || isString {
raw := make([]byte, size)
if _, err := io.ReadFull(r, raw); err != nil {
return err
}
switch {
case isString:
val.SetString(string(raw))
case isByteArr:
val.SetBytes(raw)
}
return nil
}
slice := reflect.MakeSlice(val.Type(), size, size)
for i := 0; i < size; i++ {
if err := t.decodeValue(r, slice.Index(i)); err != nil {
return err
}
}
val.Set(slice)
return nil
}
开发者ID:rjmcguire,项目名称:fatchan,代码行数:33,代码来源:fatchan.go
示例20: NewLevelDbDatastore
func NewLevelDbDatastore(dbDir string) (Datastore, error) {
opts := levigo.NewOptions()
opts.SetCache(levigo.NewLRUCache(ONE_GIGABYTE))
opts.SetCreateIfMissing(true)
opts.SetBlockSize(TWO_FIFTY_SIX_KILOBYTES)
filter := levigo.NewBloomFilter(BLOOM_FILTER_BITS_PER_KEY)
opts.SetFilterPolicy(filter)
db, err := levigo.Open(dbDir, opts)
if err != nil {
return nil, err
}
ro := levigo.NewReadOptions()
lastIdBytes, err2 := db.Get(ro, NEXT_ID_KEY)
if err2 != nil {
return nil, err2
}
lastId := uint64(0)
if lastIdBytes != nil {
lastId, err2 = binary.ReadUvarint(bytes.NewBuffer(lastIdBytes))
if err2 != nil {
return nil, err2
}
}
wo := levigo.NewWriteOptions()
return &LevelDbDatastore{db: db, lastIdUsed: lastId, readOptions: ro, writeOptions: wo}, nil
}
开发者ID:jondot,项目名称:influxdb,代码行数:31,代码来源:leveldb_datastore.go
注:本文中的encoding/binary.ReadUvarint函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论